diff --git a/src/common/paddle/pricing.ts b/src/common/paddle/pricing.ts index 378d451be4..90be887c40 100644 --- a/src/common/paddle/pricing.ts +++ b/src/common/paddle/pricing.ts @@ -213,7 +213,11 @@ export const getPricingMetadataByPriceIds = async ( return Object.fromEntries(items.map(({ item }) => [item.idMap.paddle, item])); }; -export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => { +export const getPlusPricePreview = async ( + ctx: AuthContext, + ids: string[], + discountId?: string, +) => { const region = ctx.region; const sortedIds = ids.sort(); @@ -221,10 +225,12 @@ export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => { hmac.update(sortedIds.toString()); const pricesHash = hmac.digest().toString('hex'); + // Include discountId in cache key when present + const cacheKeyParts = [pricesHash, region, discountId].filter(Boolean); const redisKey = generateStorageKey( StorageTopic.Paddle, StorageKey.PricingPreviewPlus, - [pricesHash, region].join(':'), + cacheKeyParts.join(':'), ); const redisResult = await getRedisObject(redisKey); @@ -239,6 +245,7 @@ export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => { quantity: 1, })), address: region ? { countryCode: region as CountryCode } : undefined, + discountId, }); await setRedisObjectWithExpiry( diff --git a/src/schema/paddle.ts b/src/schema/paddle.ts index c665c49ad4..34a8fe1a39 100644 --- a/src/schema/paddle.ts +++ b/src/schema/paddle.ts @@ -134,7 +134,11 @@ export const typeDefs = /* GraphQL */ ` pricePreviews: PricePreviews! @auth corePricePreviews: PricePreviews! @auth pricingMetadata(type: PricingType): [ProductPricingMetadata!]! @auth - pricingPreview(type: PricingType, locale: String): [ProductPricingPreview!]! + pricingPreview( + type: PricingType + locale: String + discountId: String + ): [ProductPricingPreview!]! pricingPreviewByIds( """ The IDs of the prices to preview @@ -323,6 +327,7 @@ export interface GQLCustomData { interface PaddlePricingPreviewArgs { type?: PurchaseType; locale?: string; + discountId?: string; } interface PaddlePricingPreviewByIdsArgs { @@ -445,7 +450,11 @@ export const resolvers: IResolvers = traceResolvers< ): Promise => getPricingMetadata(ctx, type), pricingPreview: async ( _, - { type = PurchaseType.Plus, locale }: PaddlePricingPreviewArgs, + { + type = PurchaseType.Plus, + locale, + discountId, + }: PaddlePricingPreviewArgs, ctx, ): Promise => { const metadata = await getPricingMetadata(ctx, type); @@ -453,7 +462,7 @@ export const resolvers: IResolvers = traceResolvers< .map(({ idMap }) => idMap.paddle) .filter(Boolean) as string[]; - const preview = await getPlusPricePreview(ctx, ids); + const preview = await getPlusPricePreview(ctx, ids, discountId); // consolidate the preview data and metadata const consolidated = metadata.map((meta) => {