From b5a67cef1d09e54b33a2eb32baaa834d6ddef135 Mon Sep 17 00:00:00 2001 From: Reino Muhl <10620585+StaberindeZA@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:53:09 -0400 Subject: [PATCH] fix(payments-legacy): support cms localization Because: - Legacy mapper was not using localization data from the CMS This commit: - Updated legacy mapper to utilize localization data. Closes #FXA-10436 --- .../src/lib/stripe-mapper.service.spec.ts | 36 ++++++++++++++++++- .../legacy/src/lib/stripe-mapper.service.ts | 20 ++++++++--- packages/fxa-auth-server/bin/key_server.js | 1 + .../test/local/payments/stripe.js | 15 +++----- 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/libs/payments/legacy/src/lib/stripe-mapper.service.spec.ts b/libs/payments/legacy/src/lib/stripe-mapper.service.spec.ts index cd8982d8c0..be5f19f96f 100644 --- a/libs/payments/legacy/src/lib/stripe-mapper.service.spec.ts +++ b/libs/payments/legacy/src/lib/stripe-mapper.service.spec.ts @@ -147,8 +147,9 @@ describe('StripeMapperService', () => { expect(nonMatchingPlans[0].split(' - ')[1]).toBe('webIconURL'); }); - it('should return data from cms', async () => { + it('should return data from cms default locale if no localization data is available', async () => { const expected = PurchaseWithDetailsOfferingContentTransformedFactory(); + expected.purchaseDetails.data.attributes.localizations.data = []; mockCMSConfigUtil.transformedPurchaseWithCommonContentForPlanId.mockReturnValueOnce( expected ); @@ -178,6 +179,39 @@ describe('StripeMapperService', () => { expect(nonMatchingPlans).toHaveLength(0); }); + it('should return data from cms for locale when available', async () => { + const expected = PurchaseWithDetailsOfferingContentTransformedFactory(); + mockCMSConfigUtil.transformedPurchaseWithCommonContentForPlanId.mockReturnValueOnce( + expected + ); + const productMetadata = { + productSet: 'set', + productOrder: 'order', + }; + const stripePlan = StripePlanFactory() as Stripe.Plan; + stripePlan.product = StripeProductFactory({ + metadata: productMetadata, + }); + const { mappedPlans, nonMatchingPlans } = + await stripeMapper.mapCMSToStripePlans([stripePlan], 'en', false); + const actualProduct = mappedPlans[0].product as Stripe.Product; + expect(mappedPlans[0].metadata?.['webIconURL']).toBe( + expected.purchaseDetails.data.attributes.localizations.data[0] + .attributes.webIcon + ); + expect(actualProduct.metadata?.['webIconURL']).toBe( + expected.purchaseDetails.data.attributes.localizations.data[0] + .attributes.webIcon + ); + expect(actualProduct.metadata?.['productSet']).toBe( + productMetadata.productSet + ); + expect(actualProduct.metadata?.['productOrder']).toBe( + productMetadata.productOrder + ); + expect(nonMatchingPlans).toHaveLength(0); + }); + it('should return data from CMS and not error on locale plan', async () => { const expected = PurchaseWithDetailsOfferingContentTransformedFactory({ purchaseDetails: { diff --git a/libs/payments/legacy/src/lib/stripe-mapper.service.ts b/libs/payments/legacy/src/lib/stripe-mapper.service.ts index 0beaa1a61d..5730a222c5 100644 --- a/libs/payments/legacy/src/lib/stripe-mapper.service.ts +++ b/libs/payments/legacy/src/lib/stripe-mapper.service.ts @@ -95,13 +95,23 @@ export class StripeMapperService { continue; } - const commonContent = - cmsConfigData.offering.data.attributes.commonContent; - const purchaseDetails = cmsConfigData.purchaseDetails; + const commonContentAttributes = + cmsConfigData.offering.data.attributes.commonContent.data.attributes; + const commonContentAttributesLocalized = commonContentAttributes + .localizations.data.length + ? commonContentAttributes.localizations.data[0].attributes + : commonContentAttributes; + + const purchaseDetailsAttributes = + cmsConfigData.purchaseDetails.data.attributes; + const purchaseDetailsLocalizedAttributes = purchaseDetailsAttributes + .localizations.data.length + ? purchaseDetailsAttributes.localizations.data[0].attributes + : purchaseDetailsAttributes; const planMapper = new PlanMapperUtil( - commonContent.data.attributes, - purchaseDetails.data.attributes, + commonContentAttributesLocalized, + purchaseDetailsLocalizedAttributes, mergedStripeMetadata, cmsEnabled ); diff --git a/packages/fxa-auth-server/bin/key_server.js b/packages/fxa-auth-server/bin/key_server.js index f2c1dc45bd..31f0308049 100755 --- a/packages/fxa-auth-server/bin/key_server.js +++ b/packages/fxa-auth-server/bin/key_server.js @@ -148,6 +148,7 @@ async function run(config) { const strapiClient = new StrapiClient(strapiClientConfig, firestore); const productConfigurationManager = new ProductConfigurationManager( strapiClient, + priceManager, statsd ); Container.set(ProductConfigurationManager, productConfigurationManager); diff --git a/packages/fxa-auth-server/test/local/payments/stripe.js b/packages/fxa-auth-server/test/local/payments/stripe.js index 2972bd6b8a..2178dae543 100644 --- a/packages/fxa-auth-server/test/local/payments/stripe.js +++ b/packages/fxa-auth-server/test/local/payments/stripe.js @@ -50,7 +50,6 @@ const { const { ProductConfigurationManager, PurchaseWithDetailsOfferingContentTransformedFactory, - PurchaseDetailsTransformedFactory, } = require('@fxa/shared/cms'); const customer1 = require('./fixtures/stripe/customer1.json'); @@ -3426,15 +3425,11 @@ describe('#integration - StripeHelper', () => { const newWebIconURL = 'http://strapi.example/webicon'; const mockCMSConfigUtil = { transformedPurchaseWithCommonContentForPlanId: (planId) => { - return PurchaseWithDetailsOfferingContentTransformedFactory({ - purchaseDetails: { - data: { - attributes: PurchaseDetailsTransformedFactory({ - webIcon: newWebIconURL, - }), - }, - }, - }); + const mockValue = + PurchaseWithDetailsOfferingContentTransformedFactory(); + mockValue.purchaseDetails.data.attributes.webIcon = newWebIconURL; + mockValue.purchaseDetails.data.attributes.localizations.data = []; + return mockValue; }, }; const mockProductConfigurationManager = {