Skip to content

Commit

Permalink
feat: balance campaign prices added to loan utility (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
roger-in-kiva authored Aug 2, 2023
1 parent 4490262 commit 319707e
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions @kiva/kv-components/utils/loanUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
export const ERL_COOKIE_NAME = 'kverlfivedollarnotes';
export const TOP_UP_CAMPAIGN = 'TOPUP-VB-BALANCE-MPV1';
export const BASE_CAMPAIGN = 'BASE-VB_BALANCE_MPV1';
export const BALANCE_CAMPAIGN = 'REPAYMENT-NOTIFICATION_BALANCE_MPV1';
export const NO_BALANCE_CAMPAIGN = 'REPAYMENT-NOTIFICATION_NO-BALANCE_MPV1';

function balanceCampaignOptions(val) {
if (val > 20) return 25;
if (val > 15) return 20;
if (val > 10) return 15;
if (val > 5) return 10;
return 5;
}

/**
* Checks if the unreserved amount is between 25 and 50
Expand Down Expand Up @@ -42,6 +52,11 @@ export function getLendCtaSelectedOption(
userBalance,
fiveDollarsSelected,
) {
// defaulted to $5 for fiveDollarsSelected flag even when users come from email with a different balance
if (enableFiveDollarsNotes && fiveDollarsSelected) {
return '5';
}

// Don't enable the campaign changes when the user balance is undefined (user not logged in)
if (enableFiveDollarsNotes && typeof userBalance !== 'undefined') {
let currentCampaign = getCookie?.(ERL_COOKIE_NAME);
Expand All @@ -56,33 +71,36 @@ export function getLendCtaSelectedOption(
// eslint-disable-next-line no-nested-ternary
currentCampaign = campaignToCheck.includes(TOP_UP_CAMPAIGN)
? TOP_UP_CAMPAIGN
: (campaignToCheck.includes(BASE_CAMPAIGN) ? BASE_CAMPAIGN : '');
: campaignToCheck.includes(BASE_CAMPAIGN) ? BASE_CAMPAIGN // eslint-disable-line no-nested-ternary
: campaignToCheck.includes(BALANCE_CAMPAIGN) ? BALANCE_CAMPAIGN // eslint-disable-line no-nested-ternary
: (campaignToCheck.includes(NO_BALANCE_CAMPAIGN) ? NO_BALANCE_CAMPAIGN : '');

if (currentCampaign && setCookie) {
setCookie(ERL_COOKIE_NAME, currentCampaign, { expires });
}
}

if (currentCampaign) {
let val = Math.floor(userBalance / 5) * 5;

// Base campaign gets largest increment of $5 under the user's balance up to $25 or the unreserved amount
if (currentCampaign === BASE_CAMPAIGN) {
let val = Math.floor(userBalance / 5) * 5;

// eslint-disable-next-line no-nested-ternary
val = val === 0 ? 5 : (val > 25 ? 25 : val);

return Number(val <= unreservedAmount ? val : unreservedAmount).toFixed();
}

// Top up campaign defaults to $5
if (currentCampaign === BALANCE_CAMPAIGN) {
val = balanceCampaignOptions(val);
return Number(val).toFixed();
}

// Top up and no balance campaigns defaults to $5
return Number(unreservedAmount > 5 ? 5 : unreservedAmount).toFixed();
}
}

if (enableFiveDollarsNotes && fiveDollarsSelected) {
return '5';
}

// Handle when $5 notes isn't enabled
if (isBetween25And50(unreservedAmount) || isLessThan25(unreservedAmount)) {
return Number(unreservedAmount).toFixed();
Expand Down

0 comments on commit 319707e

Please sign in to comment.