-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc82d58
commit 50b7831
Showing
5 changed files
with
116 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
editor.planx.uk/src/@planx/components/Pay/Public/FeeBreakdown/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface FeeBreakdown { | ||
applicationFee: number; | ||
total: number; | ||
reduction: number; | ||
vat: number | undefined; | ||
} | ||
|
||
export interface PassportFeeFields { | ||
"application.fee.calculated": number; | ||
"application.fee.payable": number; | ||
"application.fee.payable.vat": number; | ||
} |
24 changes: 24 additions & 0 deletions
24
editor.planx.uk/src/@planx/components/Pay/Public/FeeBreakdown/useFeeBreakdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { logger } from "airbrake"; | ||
import { useStore } from "pages/FlowEditor/lib/store"; | ||
|
||
import { FeeBreakdown } from "./types"; | ||
import { createPassportSchema } from "./utils"; | ||
|
||
export const useFeeBreakdown = (): FeeBreakdown | undefined => { | ||
const [passportData, sessionId] = useStore((state) => [ | ||
state.computePassport().data, | ||
state.sessionId, | ||
]); | ||
const schema = createPassportSchema(); | ||
const result = schema.safeParse(passportData); | ||
|
||
// Unable to parse fee data from passport, do not show FeeBreakdown component | ||
if (!result.success) { | ||
logger.notify( | ||
`Failed to parse fee breakdown data from passport for session ${sessionId}. Error: ${result.error}`, | ||
); | ||
return; | ||
} | ||
|
||
return result.data; | ||
}; |
37 changes: 37 additions & 0 deletions
37
editor.planx.uk/src/@planx/components/Pay/Public/FeeBreakdown/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { z } from "zod"; | ||
|
||
import { FeeBreakdown, PassportFeeFields } from "./types"; | ||
|
||
const toNumber = (input: number | number[]) => | ||
Array.isArray(input) ? Number(input[0]) : input; | ||
|
||
const calculateReduction = (data: PassportFeeFields) => | ||
data["application.fee.calculated"] | ||
? data["application.fee.calculated"] - data["application.fee.payable"] | ||
: 0; | ||
|
||
const toFeeBreakdown = (data: PassportFeeFields): FeeBreakdown => ({ | ||
applicationFee: | ||
data["application.fee.calculated"] || data["application.fee.payable"], | ||
total: data["application.fee.payable"], | ||
vat: data["application.fee.payable.vat"], | ||
reduction: calculateReduction(data), | ||
}); | ||
|
||
export const createPassportSchema = () => { | ||
const questionSchema = z.number(); | ||
const setValueSchema = z.array(z.coerce.number()); | ||
const feeSchema = z | ||
.union([questionSchema, setValueSchema]) | ||
.transform(toNumber); | ||
|
||
const schema = z | ||
.object({ | ||
"application.fee.calculated": feeSchema.optional().default(0), | ||
"application.fee.payable": feeSchema, | ||
"application.fee.payable.vat": feeSchema.optional().default(0), | ||
}) | ||
.transform(toFeeBreakdown); | ||
|
||
return schema; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters