Skip to content

Commit

Permalink
fix(core): Remove insecure fallback from default price selection strat
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 15, 2023
1 parent 3538bee commit 6f34d06
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class DefaultProductVariantPriceSelectionStrategy implements ProductVaria
selectPrice(ctx: RequestContext, prices: ProductVariantPrice[]) {
const pricesInChannel = prices.filter(p => idsAreEqual(p.channelId, ctx.channelId));
const priceInCurrency = pricesInChannel.find(p => p.currencyCode === ctx.currencyCode);
return priceInCurrency || pricesInChannel[0];
return priceInCurrency;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CurrencyCode } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/lib/shared-types';

import { RequestContext, SerializedRequestContext } from '../../../api/common/request-context';
Expand Down Expand Up @@ -28,6 +29,10 @@ export class MutableRequestContext extends RequestContext {
return this.mutatedChannel?.id ?? super.channelId;
}

get currencyCode(): CurrencyCode {
return this.mutatedChannel?.defaultCurrencyCode ?? super.currencyCode;
}

static deserialize(ctxObject: SerializedRequestContext): MutableRequestContext {
return new MutableRequestContext({
req: ctxObject._req,
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/service/services/product-variant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,18 @@ export class ProductVariantService {
);
}

const defaultChannelId = (await this.channelService.getDefaultChannel(ctx)).id;
const defaultChannel = await this.channelService.getDefaultChannel(ctx);
await this.createOrUpdateProductVariantPrice(ctx, createdVariant.id, input.price, ctx.channelId);
if (!idsAreEqual(ctx.channelId, defaultChannelId)) {
if (!idsAreEqual(ctx.channelId, defaultChannel.id)) {
// When creating a ProductVariant _not_ in the default Channel, we still need to
// create a ProductVariantPrice for it in the default Channel, otherwise errors will
// result when trying to query it there.
await this.createOrUpdateProductVariantPrice(
ctx,
createdVariant.id,
input.price,
defaultChannelId,
defaultChannel.id,
defaultChannel.defaultCurrencyCode,
);
}
return createdVariant.id;
Expand Down

0 comments on commit 6f34d06

Please sign in to comment.