Skip to content

Commit

Permalink
chore: Add paidViaInviteToPay argument to formatGovPayMetadata() (#…
Browse files Browse the repository at this point in the history
…354)

Implemented in theopensystemslab/planx-new#3038,
please see this PR for context.
  • Loading branch information
DafyddLlyr authored Apr 19, 2024
1 parent 52f76e7 commit 4e67b2e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
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;
};

0 comments on commit 4e67b2e

Please sign in to comment.