Skip to content

Commit

Permalink
Merge branch 'main' into ORV2-3042
Browse files Browse the repository at this point in the history
  • Loading branch information
zgong-gov committed Jan 11, 2025
2 parents 5c658ee + 4d05079 commit 8a61792
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions frontend/src/features/permits/helpers/feeSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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)}`;
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
};

0 comments on commit 8a61792

Please sign in to comment.