Skip to content

Commit 9089a06

Browse files
committed
refactor: pricing preview to add locale option
1 parent b9014b7 commit 9089a06

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

__tests__/paddle.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ describe('plus pricing metadata', () => {
371371

372372
describe('plus pricing preview', () => {
373373
const QUERY = /* GraphQL */ `
374-
query PricingPreview($type: PricingType) {
375-
pricingPreview(type: $type) {
374+
query PricingPreview($type: PricingType, $locale: String) {
375+
pricingPreview(type: $type, locale: $locale) {
376376
metadata {
377377
appsId
378378
title
@@ -562,6 +562,31 @@ describe('plus pricing preview', () => {
562562

563563
expect(result).toEqual(result2);
564564
});
565+
566+
it('should format prices according to locale', async () => {
567+
loggedUser = 'whp-1';
568+
const result = await client.query(QUERY, {
569+
variables: {
570+
type: PricingType.Plus,
571+
locale: 'de-DE',
572+
},
573+
});
574+
expect(result.data.pricingPreview).toHaveLength(1);
575+
const preview = result.data.pricingPreview[0];
576+
expect(preview.price.formatted).toMatch(/\d+,\d+/); // German format with € and comma
577+
});
578+
579+
it('should use default locale when not specified', async () => {
580+
loggedUser = 'whp-1';
581+
const result = await client.query(QUERY, {
582+
variables: {
583+
type: PricingType.Plus,
584+
},
585+
});
586+
expect(result.data.pricingPreview).toHaveLength(1);
587+
const preview = result.data.pricingPreview[0];
588+
expect(preview.price.formatted).toMatch(/\$\d+\.\d+/); // Default USD format
589+
});
565590
});
566591

567592
describe('plus subscription', () => {

src/schema/paddle.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ export const typeDefs = /* GraphQL */ `
131131
pricePreviews: PricePreviews! @auth
132132
corePricePreviews: PricePreviews! @auth
133133
pricingMetadata(type: PricingType): [ProductPricingMetadata!]! @auth
134-
pricingPreview(type: PricingType): [ProductPricingPreview!]! @auth
134+
pricingPreview(
135+
type: PricingType
136+
locale: String
137+
): [ProductPricingPreview!]! @auth
135138
}
136139
137140
${toGQLEnum(PricingType, 'PricingType')}
@@ -275,6 +278,7 @@ export interface GQLCustomData {
275278

276279
interface PaddlePricingPreviewArgs {
277280
type?: PricingType;
281+
locale?: string;
278282
}
279283

280284
export const resolvers: IResolvers<unknown, AuthContext> = traceResolvers<
@@ -389,7 +393,7 @@ export const resolvers: IResolvers<unknown, AuthContext> = traceResolvers<
389393
): Promise<BasePricingMetadata[]> => getPricingMetadata(ctx, type),
390394
pricingPreview: async (
391395
_,
392-
{ type = PricingType.Plus }: PaddlePricingPreviewArgs,
396+
{ type = PricingType.Plus, locale }: PaddlePricingPreviewArgs,
393397
ctx,
394398
): Promise<BasePricingPreview[]> => {
395399
const metadata = await getPricingMetadata(ctx, type);
@@ -416,7 +420,7 @@ export const resolvers: IResolvers<unknown, AuthContext> = traceResolvers<
416420
return {
417421
metadata: meta,
418422
priceId: item.price.id,
419-
price: getProductPrice(item),
423+
price: getProductPrice(item, locale),
420424
currency: {
421425
code: preview.currencyCode,
422426
symbol: removeNumbers(item.formattedTotals.total),

0 commit comments

Comments
 (0)