Skip to content

Commit

Permalink
fix: consider new & old both intl fallback mastercards
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Dec 11, 2024
1 parent 40af449 commit 69e0880
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
28 changes: 21 additions & 7 deletions functions/utils/best-card-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GiftCard | null> {
Expand Down Expand Up @@ -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;
Expand All @@ -78,11 +83,20 @@ async function findBestVisaCard(visaCards: GiftCard[], countryCode: string, amou
}
return null;
}
async function getFallbackIntlMastercard(accessToken: AccessToken): Promise<GiftCard | null> {
async function getFirstFallbackIntlMastercard(accessToken: AccessToken): Promise<GiftCard | null> {
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<GiftCard | null> {
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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions functions/utils/reloadly-lists.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 69e0880

Please sign in to comment.