From 4d050792a23b37d51b6daee86a98e4c7b3c31f18 Mon Sep 17 00:00:00 2001 From: glen-aot <160973940+glen-aot@users.noreply.github.com> Date: Fri, 10 Jan 2025 16:43:17 -0800 Subject: [PATCH] ORV2-3211 - BVT: Void not refunding the total value (#1740) Co-authored-by: GlenAOT <160973940+GlenAOT@users.noreply.github.com> --- .../features/permits/helpers/feeSummary.ts | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/frontend/src/features/permits/helpers/feeSummary.ts b/frontend/src/features/permits/helpers/feeSummary.ts index 943da6b06..a17374474 100644 --- a/frontend/src/features/permits/helpers/feeSummary.ts +++ b/frontend/src/features/permits/helpers/feeSummary.ts @@ -3,9 +3,12 @@ import { TRANSACTION_TYPES, TransactionType } from "../types/payment"; import { Permit } from "../types/permit"; import { isValidTransaction } from "./payment"; import { Nullable } from "../../../common/types/common"; -import { PERMIT_STATES, daysLeftBeforeExpiry, getPermitState } from "./permitState"; +import { PERMIT_STATES, getPermitState } from "./permitState"; import { PERMIT_TYPES, PermitType } from "../types/PermitType"; -import { getDurationIntervalDays, maxDurationForPermitType } from "./dateSelection"; +import { + getDurationIntervalDays, + maxDurationForPermitType, +} from "./dateSelection"; import { applyWhenNotNullable, getDefaultRequiredVal, @@ -17,19 +20,27 @@ import { * @param duration Number of days for duration of permit * @returns Fee to be paid for the permit duration */ -export const calculateFeeByDuration = (permitType: PermitType, duration: number) => { +export const calculateFeeByDuration = ( + permitType: PermitType, + duration: number, +) => { const maxAllowableDuration = maxDurationForPermitType(permitType); - - // Make sure that duration is between 0 and max allowable duration (for given permit type) - const safeDuration = duration < 0 - ? 0 - : (duration > maxAllowableDuration) ? maxAllowableDuration : duration; + + // Make sure that duration is between 0 and max allowable duration (for given permit type) + const safeDuration = + duration < 0 + ? 0 + : duration > maxAllowableDuration + ? maxAllowableDuration + : duration; const intervalDays = getDurationIntervalDays(permitType); - const intervalPeriodsToPay = safeDuration > 360 - ? Math.ceil(360 / intervalDays) : Math.ceil(safeDuration / intervalDays); - + const intervalPeriodsToPay = + safeDuration > 360 + ? Math.ceil(360 / intervalDays) + : Math.ceil(safeDuration / intervalDays); + switch (permitType) { // Add more conditions for other permit types if needed case PERMIT_TYPES.STOS: @@ -61,9 +72,11 @@ export const feeSummaryDisplayText = ( (numericStr) => Number(numericStr).toFixed(2), feeSummary, ); - const feeFromDuration = duration && permitType ? - calculateFeeByDuration(permitType, duration).toFixed(2) : null; - + const feeFromDuration = + duration && permitType + ? calculateFeeByDuration(permitType, duration).toFixed(2) + : null; + const fee = getDefaultRequiredVal("0.00", feeFromSummary, feeFromDuration); const numericFee = Number(fee); return numericFee >= 0 ? `$${fee}` : `-$${(numericFee * -1).toFixed(2)}`; @@ -114,7 +127,10 @@ export const calculateAmountToRefund = ( const netPaid = calculateNetAmount(permitHistory); if (isZeroAmount(netPaid)) return 0; // If total paid is $0 (eg. no-fee permits), then refund nothing - const feeForCurrDuration = calculateFeeByDuration(currPermitType, currDuration); + const feeForCurrDuration = calculateFeeByDuration( + currPermitType, + currDuration, + ); return netPaid - feeForCurrDuration; }; @@ -142,13 +158,8 @@ export const calculateAmountForVoid = ( return 0; } - const netAmountPaid = calculateNetAmount(transactionHistory); - if (isZeroAmount(netAmountPaid)) return 0; // If existing net paid is $0 (eg. no-fee permits), then refund nothing + const netPaid = calculateNetAmount(transactionHistory); + if (isZeroAmount(netPaid)) return 0; // If existing net paid is $0 (eg. no-fee permits), then refund nothing - const daysLeft = daysLeftBeforeExpiry(permit); - const intervalDays = getDurationIntervalDays(permit.permitType); - return calculateFeeByDuration( - permit.permitType, - Math.floor(daysLeft / intervalDays) * intervalDays, - ); + return netPaid; };