Skip to content

Commit

Permalink
feat(itp): Handle fee breakdown (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Dec 10, 2024
1 parent 6dfb736 commit a9848d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/requests/payment-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe("extractSessionPreviewData", () => {
"passport data not found",
);
});

test("keys must be present in the passport", () => {
const invalidSession: Session = {
id: "abc",
Expand All @@ -44,6 +45,7 @@ describe("extractSessionPreviewData", () => {
extractSessionPreviewData(invalidSession, previewKeys),
).toThrow('passport key "key2.notFoundKey" not found in passport data');
});

test("a simple set of session preview keys are extracted from the session", () => {
const session: Session = {
id: "abc",
Expand Down Expand Up @@ -73,6 +75,7 @@ describe("extractSessionPreviewData", () => {
c: 3,
});
});

test("a set of compound keys are extracted from the session", () => {
const session: Session = {
id: "abc",
Expand All @@ -97,6 +100,7 @@ describe("extractSessionPreviewData", () => {
const sessionPreviewData = extractSessionPreviewData(session, previewKeys);
expect(sessionPreviewData).toEqual({ "a.b": 1, "c.d": 2, "c.d.e": 3 });
});

test("a set of nested and compound keys are extracted from the session", () => {
const session: Session = {
id: "abc",
Expand Down
15 changes: 15 additions & 0 deletions src/requests/payment-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Passport } from "../models/passport";
import { getLatestFlowGraph } from "../requests/flow";
import type { KeyPath, PaymentRequest, Session, Value } from "../types";
import { ComponentType, FlowGraph, Node } from "../types";
import { getFeeBreakdown } from "../utils";
import { FeeBreakdown } from "./../types/feeBreakdown";
import { getDetailedSessionById } from "./session";

export class PaymentRequestClient {
Expand Down Expand Up @@ -91,6 +93,15 @@ export async function createPaymentRequest(
{ key: "flow", value: session.flow.slug },
];

let feeBreakdown: FeeBreakdown | undefined = undefined;

try {
feeBreakdown = getFeeBreakdown(session.data.passport.data);
} catch (error) {
// Fail silently, frontend will handle missing fee breakdown
console.error(`Unable to generate fee breakdown for session ${sessionId}`);
}

const paymentAmountPounds = await getPaymentAmount(payNode, session);
if (!paymentAmountPounds)
throw new Error("Payment amount not found in passport");
Expand All @@ -116,6 +127,7 @@ export async function createPaymentRequest(
$paymentAmount: Int!
$sessionPreviewData: jsonb!
$govPayMetadata: jsonb!
$feeBreakdown: jsonb
) {
insert_payment_requests_one(
object: {
Expand All @@ -126,6 +138,7 @@ export async function createPaymentRequest(
payment_amount: $paymentAmount
session_preview_data: $sessionPreviewData
govpay_metadata: $govPayMetadata
fee_breakdown: $feeBreakdown
}
) {
id
Expand All @@ -136,6 +149,7 @@ export async function createPaymentRequest(
paymentAmount: payment_amount
sessionPreviewData: session_preview_data
govPayMetadata: govpay_metadata
feeBreakdown: fee_breakdown
}
}
`,
Expand All @@ -147,6 +161,7 @@ export async function createPaymentRequest(
paymentAmount,
sessionPreviewData,
govPayMetadata,
feeBreakdown,
},
);
return response?.insert_payment_requests_one;
Expand Down
2 changes: 2 additions & 0 deletions src/types/payment-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataObject } from "./data";
import { FeeBreakdown } from "./feeBreakdown";

export interface PaymentRequest {
id: string;
Expand All @@ -11,4 +12,5 @@ export interface PaymentRequest {
paidAt: string;
createdAt: string;
govPayPaymentId: string;
feeBreakdown?: FeeBreakdown;
}

0 comments on commit a9848d4

Please sign in to comment.