Skip to content

Commit

Permalink
Hotfix for TROW Fee Calculation (#1401)
Browse files Browse the repository at this point in the history
Co-authored-by: gchauhan-aot <[email protected]>
  • Loading branch information
zgong-gov and gchauhan-aot authored May 29, 2024
1 parent 34aa28a commit e891000
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Nullable } from "../../../../common/types/common";
import { feeSummaryDisplayText } from "../../helpers/feeSummary";
import { permitTypeDisplayText } from "../../types/PermitType";
import { PermitType, permitTypeDisplayText } from "../../types/PermitType";
import "./FeeSummary.scss";

export const FeeSummary = ({
Expand All @@ -9,12 +9,12 @@ export const FeeSummary = ({
permitDuration,
hideDescriptions,
}: {
permitType?: Nullable<string>;
permitType?: Nullable<PermitType>;
feeSummary?: Nullable<string>;
permitDuration?: number;
hideDescriptions?: boolean;
}) => {
const feeDisplayText = feeSummaryDisplayText(feeSummary, permitDuration);
const feeDisplayText = feeSummaryDisplayText(feeSummary, permitDuration, permitType);

return (
<div className="fee-summary">
Expand Down
22 changes: 16 additions & 6 deletions frontend/src/features/permits/helpers/feeSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Permit } from "../types/permit";
import { isValidTransaction } from "./payment";
import { Nullable } from "../../../common/types/common";
import { PERMIT_STATES, getPermitState } from "./permitState";
import { PERMIT_TYPES, PermitType } from "../types/PermitType";

import {
applyWhenNotNullable,
Expand All @@ -12,10 +13,16 @@ import {

/**
* Calculates the fee for a permit only by its duration.
* @param permitType Type of permit
* @param duration Number of days for duration of permit
* @returns Fee to be paid for the permit duration
*/
export const calculateFeeByDuration = (duration: number) => {
export const calculateFeeByDuration = (permitType: PermitType, duration: number) => {
if (permitType === PERMIT_TYPES.TROW) {
// Only for TROW
return duration > 360 ? 1200 : Math.floor(duration / 30) * 100;
}
// Add more conditions for other permit types if needed
// 1 Year === 365 days, but the fee for one year is only $360
return duration > 360 ? 360 : duration;
};
Expand All @@ -24,20 +31,21 @@ export const calculateFeeByDuration = (duration: number) => {
* Gets full display text for fee summary.
* @param feeSummary fee summary field for a permit (if exists)
* @param duration duration field for a permit (if exists)
* @param permitType type of permit (if exists)
* @returns display text for the fee summary (currency amount to 2 decimal places)
*/
export const feeSummaryDisplayText = (
feeSummary?: Nullable<string>,
duration?: Nullable<number>,
permitType?: Nullable<PermitType>,
) => {
const feeFromSummary = applyWhenNotNullable(
(numericStr) => Number(numericStr).toFixed(2),
feeSummary,
);
const feeFromDuration = applyWhenNotNullable(
(num) => calculateFeeByDuration(num).toFixed(2),
duration,
);
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 @@ -77,14 +85,16 @@ export const calculateNetAmount = (permitHistory: PermitHistory[]) => {
* Calculates the amount that needs to be refunded (or paid if amount is negative) for a permit given a new duration period.
* @param permitHistory List of history objects that make up the history of a permit and its transactions
* @param currDuration Current (updated) duration of the permit
* @param currPermitType Permit type of current permit to refund
* @returns Amount that needs to be refunded, or if negative then the amount that still needs to be paid
*/
export const calculateAmountToRefund = (
permitHistory: PermitHistory[],
currDuration: number,
currPermitType: PermitType,
) => {
const netPaid = calculateNetAmount(permitHistory);
const feeForCurrDuration = calculateFeeByDuration(currDuration);
const feeForCurrDuration = calculateFeeByDuration(currPermitType, currDuration);
return netPaid - feeForCurrDuration;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ export const getDefaultValues = (
applicationData?.permitData?.expiryDate,
);

const defaultPermitType = getDefaultRequiredVal(
permitType,
applicationData?.permitType,
);

return {
originalPermitId: getDefaultRequiredVal(
"",
Expand All @@ -194,10 +199,7 @@ export const getDefaultValues = (
),
permitId: getDefaultRequiredVal("", applicationData?.permitId),
permitNumber: getDefaultRequiredVal("", applicationData?.permitNumber),
permitType: getDefaultRequiredVal(
permitType,
applicationData?.permitType,
),
permitType: defaultPermitType,
permitStatus: getDefaultRequiredVal(
PERMIT_STATUSES.IN_PROGRESS,
applicationData?.permitStatus,
Expand Down Expand Up @@ -242,7 +244,7 @@ export const getDefaultValues = (
vehicleDetails: getDefaultVehicleDetails(
applicationData?.permitData?.vehicleDetails,
),
feeSummary: `${calculateFeeByDuration(durationOrDefault)}`,
feeSummary: `${calculateFeeByDuration(defaultPermitType, durationOrDefault)}`,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isValidTransaction } from "../../../helpers/payment";
import { hasPermitsActionFailed } from "../../../helpers/permitState";
import { ERROR_ROUTES } from "../../../../../routes/constants";
import { getDefaultRequiredVal } from "../../../../../common/helpers/util";
import { DEFAULT_PERMIT_TYPE } from "../../../types/PermitType";

export const AmendPermitFinish = () => {
const navigate = useNavigate();
Expand All @@ -39,6 +40,11 @@ export const AmendPermitFinish = () => {
0,
amendmentApplication?.permitData?.permitDuration,
),
getDefaultRequiredVal(
DEFAULT_PERMIT_TYPE,
amendmentApplication?.permitType,
permit?.permitType,
),
);

const { mutation: startTransactionMutation, transaction } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isValidTransaction } from "../../../helpers/payment";
import { getDatetimes } from "./helpers/getDatetimes";
import { useModifyAmendmentApplication } from "../../../hooks/hooks";
import { ERROR_ROUTES } from "../../../../../routes/constants";
import { DEFAULT_PERMIT_TYPE } from "../../../types/PermitType";
import {
applyWhenNotNullable,
getDefaultRequiredVal,
Expand Down Expand Up @@ -102,6 +103,11 @@ export const AmendPermitReview = () => {
0,
amendmentApplication?.permitData?.permitDuration,
),
getDefaultRequiredVal(
DEFAULT_PERMIT_TYPE,
amendmentApplication?.permitType,
permit?.permitType,
),
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export const PermitDetails = ({
// this needs useEffect as this form field update process is manual, and needs to happen whenever startDate and duration changes
// also, the form field component is accepting a dayJS object
setValue("permitData.expiryDate", dayjs(expiryDate));
setValue("permitData.feeSummary", `${calculateFeeByDuration(duration)}`);
}, [startDate, duration]);
setValue("permitData.feeSummary", `${calculateFeeByDuration(permitType, duration)}`);
}, [startDate, duration, permitType]);

return (
<Box className="permit-details">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ReviewFeeSummary } from "./ReviewFeeSummary";
import { ReviewActions } from "./ReviewActions";
import { CompanyProfile } from "../../../../../manageProfile/types/manageProfile";
import { VehicleSubType } from "../../../../../manageVehicles/types/Vehicle";
import { PermitType } from "../../../../types/PermitType";
import { DEFAULT_PERMIT_TYPE, PermitType } from "../../../../types/PermitType";
import { calculateFeeByDuration } from "../../../../helpers/feeSummary";
import { getDefaultRequiredVal } from "../../../../../../common/helpers/util";
import { Nullable } from "../../../../../../common/types/common";
Expand Down Expand Up @@ -56,6 +56,7 @@ export const PermitReview = (props: PermitReviewProps) => {
const feeSummary = props.calculatedFee
? props.calculatedFee
: `${calculateFeeByDuration(
getDefaultRequiredVal(DEFAULT_PERMIT_TYPE, props.permitType),
getDefaultRequiredVal(0, props.permitDuration),
)}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ describe("Review and Confirm Application Details", () => {
permitData: {
...defaultApplicationData.permitData,
feeSummary: `${calculateFeeByDuration(
defaultApplicationData.permitType,
defaultApplicationData.permitData.permitDuration,
)}`,
},
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/features/permits/pages/Refund/RefundPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@mui/material";

import "./RefundPage.scss";
import { permitTypeDisplayText } from "../../types/PermitType";
import { PermitType, permitTypeDisplayText } from "../../types/PermitType";
import { RefundFormData } from "./types/RefundFormData";
import { requiredMessage } from "../../../../common/helpers/validationMessages";
import { getErrorMessage } from "../../../../common/components/form/CustomFormComponents";
Expand Down Expand Up @@ -90,7 +90,7 @@ export const RefundPage = ({
fax?: Nullable<string>;
reason?: Nullable<string>;
permitNumber?: Nullable<string>;
permitType?: Nullable<string>;
permitType?: Nullable<PermitType>;
permitAction: PermitAction;
amountToRefund: number;
onFinish: (refundData: RefundFormData) => void;
Expand Down
2 changes: 1 addition & 1 deletion vehicles/src/common/constants/permit.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export const TROS_PRICE_PER_TERM = 30;
export const TROS_MIN_VALID_DURATION = TROS_TERM;
export const TROS_MAX_VALID_DURATION = 366;
export const TROW_TERM = 30;
export const TROW_PRICE_PER_TERM = 30;
export const TROW_PRICE_PER_TERM = 100;
export const TROW_MIN_VALID_DURATION = TROS_TERM;
export const TROW_MAX_VALID_DURATION = 366;
6 changes: 4 additions & 2 deletions vehicles/src/common/helper/permit-fee.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
TROS_TERM,
TROW_MAX_VALID_DURATION,
TROW_MIN_VALID_DURATION,
TROW_PRICE_PER_TERM,
TROW_TERM,
} from '../constants/permit.constant';
import { differenceBetween } from './date-time.helper';
import * as dayjs from 'dayjs';
Expand Down Expand Up @@ -81,8 +83,8 @@ export const permitFee = (application: Permit, oldAmount: number): number => {
duration = 360;
return currentPermitFee(
duration,
TROS_PRICE_PER_TERM,
TROS_TERM,
TROW_PRICE_PER_TERM,
TROW_TERM,
oldAmount,
);
}
Expand Down

0 comments on commit e891000

Please sign in to comment.