Skip to content

Commit 5420c2b

Browse files
authored
feat: add support for discountId in pricing preview queries (#3447)
1 parent ce27cb9 commit 5420c2b

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/common/paddle/pricing.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,24 @@ export const getPricingMetadataByPriceIds = async (
213213
return Object.fromEntries(items.map(({ item }) => [item.idMap.paddle, item]));
214214
};
215215

216-
export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => {
216+
export const getPlusPricePreview = async (
217+
ctx: AuthContext,
218+
ids: string[],
219+
discountId?: string,
220+
) => {
217221
const region = ctx.region;
218222
const sortedIds = ids.sort();
219223

220224
const hmac = createHmac('sha1', StorageTopic.Paddle);
221225
hmac.update(sortedIds.toString());
222226
const pricesHash = hmac.digest().toString('hex');
223227

228+
// Include discountId in cache key when present
229+
const cacheKeyParts = [pricesHash, region, discountId].filter(Boolean);
224230
const redisKey = generateStorageKey(
225231
StorageTopic.Paddle,
226232
StorageKey.PricingPreviewPlus,
227-
[pricesHash, region].join(':'),
233+
cacheKeyParts.join(':'),
228234
);
229235

230236
const redisResult = await getRedisObject(redisKey);
@@ -239,6 +245,7 @@ export const getPlusPricePreview = async (ctx: AuthContext, ids: string[]) => {
239245
quantity: 1,
240246
})),
241247
address: region ? { countryCode: region as CountryCode } : undefined,
248+
discountId,
242249
});
243250

244251
await setRedisObjectWithExpiry(

src/schema/paddle.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ export const typeDefs = /* GraphQL */ `
134134
pricePreviews: PricePreviews! @auth
135135
corePricePreviews: PricePreviews! @auth
136136
pricingMetadata(type: PricingType): [ProductPricingMetadata!]! @auth
137-
pricingPreview(type: PricingType, locale: String): [ProductPricingPreview!]!
137+
pricingPreview(
138+
type: PricingType
139+
locale: String
140+
discountId: String
141+
): [ProductPricingPreview!]!
138142
pricingPreviewByIds(
139143
"""
140144
The IDs of the prices to preview
@@ -323,6 +327,7 @@ export interface GQLCustomData {
323327
interface PaddlePricingPreviewArgs {
324328
type?: PurchaseType;
325329
locale?: string;
330+
discountId?: string;
326331
}
327332

328333
interface PaddlePricingPreviewByIdsArgs {
@@ -445,15 +450,19 @@ export const resolvers: IResolvers<unknown, AuthContext> = traceResolvers<
445450
): Promise<BasePricingMetadata[]> => getPricingMetadata(ctx, type),
446451
pricingPreview: async (
447452
_,
448-
{ type = PurchaseType.Plus, locale }: PaddlePricingPreviewArgs,
453+
{
454+
type = PurchaseType.Plus,
455+
locale,
456+
discountId,
457+
}: PaddlePricingPreviewArgs,
449458
ctx,
450459
): Promise<BasePricingPreview[]> => {
451460
const metadata = await getPricingMetadata(ctx, type);
452461
const ids = metadata
453462
.map(({ idMap }) => idMap.paddle)
454463
.filter(Boolean) as string[];
455464

456-
const preview = await getPlusPricePreview(ctx, ids);
465+
const preview = await getPlusPricePreview(ctx, ids, discountId);
457466

458467
// consolidate the preview data and metadata
459468
const consolidated = metadata.map((meta) => {

0 commit comments

Comments
 (0)