Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add paidViaInviteToPay argument to formatGovPayMetadata() #354

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/requests/payment-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function createPaymentRequest(
const payNode = await getPayNode(client, session);
const govPayMetadata = payNode.data?.govPayMetadata || [
{ key: "source", value: "PlanX" },
{ key: "isInviteToPay", value: true },
{ key: "paidViaInviteToPay", value: true },
{ key: "flow", value: session.flow.slug },
];

Expand Down
40 changes: 37 additions & 3 deletions src/utils/govPayMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const mockMetadata: GovPayMetadata[] = [
key: "key_someValueNotSetInPassport",
value: "@someValueNotSetInPassportolean",
},
{
key: "paidViaInviteToPay",
value: "@paidViaInviteToPay",
},
];

const mockPassport: Passport = {
Expand All @@ -38,17 +42,23 @@ const mockPassport: Passport = {
},
};

const mockParams: Parameters<typeof formatGovPayMetadata>[0] = {
metadata: mockMetadata,
userPassport: mockPassport,
paidViaInviteToPay: false,
};

describe("transforming the payload to the correct format for GovPay", () => {
it("handles variables set in the Editor", () => {
const result = formatGovPayMetadata(mockMetadata, mockPassport);
const result = formatGovPayMetadata(mockParams);
expect(result).toMatchObject({
firstKey: "firstValue",
secondKey: "secondValue",
});
});

it("handles variables set in the Passport", () => {
const result = formatGovPayMetadata(mockMetadata, mockPassport);
const result = formatGovPayMetadata(mockParams);
expect(result).toMatchObject({
key_string: "agent",
key_boolean: true,
Expand All @@ -57,7 +67,7 @@ describe("transforming the payload to the correct format for GovPay", () => {
});

describe("validating data", () => {
const formatted = formatGovPayMetadata(mockMetadata, mockPassport);
const formatted = formatGovPayMetadata(mockParams);

const get = (testKey: string) => [
mockPassport.data[testKey], // Expected
Expand Down Expand Up @@ -120,4 +130,28 @@ describe("validating data", () => {
// String ends in ellipsis (...)
expect(result).toMatch(/.*\.\.\.$/);
});

describe("handling of `paidViaInviteToPay` key", () => {
it("sets value to true based on input", () => {
const formatted = formatGovPayMetadata({
...mockParams,
paidViaInviteToPay: true,
});

expect(formatted).toMatchObject({
paidViaInviteToPay: true,
});
});

it("sets value to false based on input", () => {
const formatted = formatGovPayMetadata({
...mockParams,
paidViaInviteToPay: false,
});

expect(formatted).toMatchObject({
paidViaInviteToPay: false,
});
});
});
});
38 changes: 29 additions & 9 deletions src/utils/govPayMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ import {

type FormattedMetadata = NonNullable<GovUKCreatePaymentPayload["metadata"]>;

const ITP_KEY = "paidViaInviteToPay" as const;

const isPassportValue = (value: GovPayMetadataValue) =>
typeof value === "string" && value.startsWith("@");

/**
* Convert GovPayMetadata set in Editor to format accepted by GovPay API
* Read dynamic data variables from passport and inject into output
*/
const parseMetadata = (
metadata: GovPayMetadata[],
passport: Passport,
): FormattedMetadata => {
const parseMetadata = ({
metadata,
passport,
paidViaInviteToPay,
}: {
metadata: GovPayMetadata[];
passport: Passport;
paidViaInviteToPay: boolean;
}): FormattedMetadata => {
let entries: [string, GovPayMetadataValue][] = [];

entries = metadata.map(({ key, value }) => {
// ITP data is set at runtime by user journey, and not read from passport directly
if (key === ITP_KEY) return [ITP_KEY, paidViaInviteToPay];

if (!isPassportValue(value)) return [key, value];

// Remove "@" prefix
Expand All @@ -35,6 +45,7 @@ const parseMetadata = (
});

const parsedMetadata = Object.fromEntries(entries);

return parsedMetadata;
};

Expand Down Expand Up @@ -81,12 +92,21 @@ const validateMetadata = (
* - Static values (e.g. { vat_code: "abc123" })
* - Dynamic values (e.g. { property_type: "@project.propertyType" })
*/
export const formatGovPayMetadata = (
metadata: GovPayMetadata[],
userPassport: IPassport,
): FormattedMetadata => {
export const formatGovPayMetadata = ({
metadata,
userPassport,
paidViaInviteToPay,
}: {
metadata: GovPayMetadata[];
userPassport: IPassport;
paidViaInviteToPay: boolean;
}): FormattedMetadata => {
const passport = new Passport(userPassport);
const parsedAndValidated = parseMetadata(metadata, passport);
const parsedAndValidated = parseMetadata({
metadata,
passport,
paidViaInviteToPay,
});

return parsedAndValidated;
};
Loading