From 65ac79b98aa53ce70a296745d4cd7e228a9d05eb Mon Sep 17 00:00:00 2001 From: James Gorrie Date: Wed, 18 Sep 2024 11:55:45 +0100 Subject: [PATCH 1/2] feat: add checkout to addRegionIdToSupportUrl --- packages/shared/src/lib/geolocation.test.ts | 20 ++++++++++++++++++++ packages/shared/src/lib/geolocation.ts | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/shared/src/lib/geolocation.test.ts b/packages/shared/src/lib/geolocation.test.ts index 247f23ec8..13e1908d6 100644 --- a/packages/shared/src/lib/geolocation.test.ts +++ b/packages/shared/src/lib/geolocation.test.ts @@ -32,6 +32,8 @@ describe('getCountryName', () => { describe('addRegionIdToSupportUrl', () => { const originalUrl = 'https://support.theguardian.com/contribute'; + const checkoutUrl = 'https://support.theguardian.com/checkout'; + it('should modify the URL to include UK if country code is GB', () => { const countryCode = 'GB'; const modifiedUrl = addRegionIdToSupportUrl(originalUrl, countryCode); @@ -50,6 +52,24 @@ describe('addRegionIdToSupportUrl', () => { expect(modifiedUrl).toEqual('https://support.theguardian.com/int/contribute'); }); + it('should modify the URL to include UK if country code is GB and URL is checkout', () => { + const countryCode = 'GB'; + const modifiedUrl = addRegionIdToSupportUrl(checkoutUrl, countryCode); + expect(modifiedUrl).toEqual('https://support.theguardian.com/uk/checkout'); + }); + + it('should modify the URL to include EU if country code is PT and URL is checkout', () => { + const countryCode = 'PT'; + const modifiedUrl = addRegionIdToSupportUrl(checkoutUrl, countryCode); + expect(modifiedUrl).toEqual('https://support.theguardian.com/eu/checkout'); + }); + + it('should modify the URL to include INT if country code is unknown and URL is checkout', () => { + const countryCode = 'asdasd'; + const modifiedUrl = addRegionIdToSupportUrl(checkoutUrl, countryCode); + expect(modifiedUrl).toEqual('https://support.theguardian.com/int/checkout'); + }); + it('should not modify the URL if country code is missing', () => { const countryCode = undefined; const modifiedUrl = addRegionIdToSupportUrl(originalUrl, countryCode); diff --git a/packages/shared/src/lib/geolocation.ts b/packages/shared/src/lib/geolocation.ts index 315b72ff9..e4bacaace 100644 --- a/packages/shared/src/lib/geolocation.ts +++ b/packages/shared/src/lib/geolocation.ts @@ -615,7 +615,7 @@ export const addRegionIdToSupportUrl = (originalUrl: string, countryCode?: strin const supportRegionId = countryCodeToSupportRegionId(countryCode); if (supportRegionId && !isGWCheckoutUrl(originalUrl)) { return originalUrl.replace( - /(support.theguardian.com)\/(contribute-in-epic|contribute|subscribe)/, + /(support.theguardian.com)\/(contribute-in-epic|contribute|subscribe|checkout)/, (_, domain, path) => `${domain}/${supportRegionId.toLowerCase()}/${path}`, ); } From d7400c3630d7c1fea54848c1632b2872b540a786 Mon Sep 17 00:00:00 2001 From: James Gorrie Date: Thu, 19 Sep 2024 10:33:58 +0100 Subject: [PATCH 2/2] fix: escape . in URL regex --- packages/shared/src/lib/geolocation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared/src/lib/geolocation.ts b/packages/shared/src/lib/geolocation.ts index e4bacaace..b57e8ab23 100644 --- a/packages/shared/src/lib/geolocation.ts +++ b/packages/shared/src/lib/geolocation.ts @@ -615,7 +615,7 @@ export const addRegionIdToSupportUrl = (originalUrl: string, countryCode?: strin const supportRegionId = countryCodeToSupportRegionId(countryCode); if (supportRegionId && !isGWCheckoutUrl(originalUrl)) { return originalUrl.replace( - /(support.theguardian.com)\/(contribute-in-epic|contribute|subscribe|checkout)/, + /(support\.theguardian\.com)\/(contribute-in-epic|contribute|subscribe|checkout)/, (_, domain, path) => `${domain}/${supportRegionId.toLowerCase()}/${path}`, ); }