From 69e0880de4c88607c75caa9305ecb0c8c2c9e87e Mon Sep 17 00:00:00 2001 From: EresDev Date: Wed, 11 Dec 2024 16:42:14 +0500 Subject: [PATCH] fix: consider new & old both intl fallback mastercards --- functions/utils/best-card-finder.ts | 28 +++++++++++++++++++++------- functions/utils/reloadly-lists.ts | 4 ++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/functions/utils/best-card-finder.ts b/functions/utils/best-card-finder.ts index 492fc4b4..2db37a08 100644 --- a/functions/utils/best-card-finder.ts +++ b/functions/utils/best-card-finder.ts @@ -4,7 +4,7 @@ import { isGiftCardAvailable } from "../../shared/helpers"; import { GiftCard } from "../../shared/types"; import { commonHeaders, getGiftCards, getReloadlyApiBaseUrl } from "./shared"; import { getGiftCardById } from "../post-order"; -import { fallbackIntlMastercard, fallbackIntlVisa, masterCardIntlSkus, visaIntlSkus } from "./reloadly-lists"; +import { fallbackIntlMastercardFirst, fallbackIntlMastercardSecond, fallbackIntlVisa, masterCardIntlSkus, visaIntlSkus } from "./reloadly-lists"; import { AccessToken, ReloadlyFailureResponse } from "./types"; export async function findBestCard(countryCode: string, amount: BigNumberish, accessToken: AccessToken): Promise { @@ -55,9 +55,14 @@ async function findBestMastercard(masterCards: GiftCard[], countryCode: string, } } - const fallbackMastercard = await getFallbackIntlMastercard(accessToken); - if (fallbackMastercard && isGiftCardAvailable(fallbackMastercard, amount)) { - return fallbackMastercard; + const fallbackMastercardFirst = await getFirstFallbackIntlMastercard(accessToken); + if (fallbackMastercardFirst && isGiftCardAvailable(fallbackMastercardFirst, amount)) { + return fallbackMastercardFirst; + } + + const fallbackMastercardSecond = await getSecondFallbackIntlMastercard(accessToken); + if (fallbackMastercardSecond && isGiftCardAvailable(fallbackMastercardSecond, amount)) { + return fallbackMastercardSecond; } return null; @@ -78,11 +83,20 @@ async function findBestVisaCard(visaCards: GiftCard[], countryCode: string, amou } return null; } -async function getFallbackIntlMastercard(accessToken: AccessToken): Promise { +async function getFirstFallbackIntlMastercard(accessToken: AccessToken): Promise { + try { + return await getGiftCardById(fallbackIntlMastercardFirst.sku, accessToken); + } catch (e) { + console.error(`Failed to load international US mastercard: ${JSON.stringify(fallbackIntlMastercardFirst)}`, e); + return null; + } +} + +async function getSecondFallbackIntlMastercard(accessToken: AccessToken): Promise { try { - return await getGiftCardById(fallbackIntlMastercard.sku, accessToken); + return await getGiftCardById(fallbackIntlMastercardSecond.sku, accessToken); } catch (e) { - console.error(`Failed to load international US mastercard: ${JSON.stringify(fallbackIntlMastercard)}`, e); + console.error(`Failed to load international US mastercard: ${JSON.stringify(fallbackIntlMastercardSecond)}`, e); return null; } } diff --git a/functions/utils/reloadly-lists.ts b/functions/utils/reloadly-lists.ts index f7855d09..c88b259d 100644 --- a/functions/utils/reloadly-lists.ts +++ b/functions/utils/reloadly-lists.ts @@ -1,14 +1,14 @@ /* eslint-disable sonarjs/no-duplicate-string */ // Keep duplicate country names in different lists -export const fallbackIntlMastercard = { +export const fallbackIntlMastercardFirst = { country: "United States", countryCode: "US", name: "Mastercard Prepaid USD Debit (Virtual only) US", sku: 18732, }; -export const fallbackIntlMastercardOld = { +export const fallbackIntlMastercardSecond = { country: "United States", countryCode: "US", name: "Virtual MasterCard International USD US",