Skip to content

Commit

Permalink
new planx-core implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Jul 11, 2024
1 parent b8da414 commit b3dd30e
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,13 @@ describe("sendAgentAndPayeeConfirmationEmail", () => {
],
},
});
queryMock.mockQuery({
name: "LookupHumanReadableProjectType",
variables: {
rawList: [
"alter.internal",
"alter.openings.add.doors.rear",
"alter.facades.paint",
],
},
data: {
projectTypes: [
{ description: "internal alterations" },
{ description: "addition of doorways to the rear of the building" },
{ description: "painting of facades" },
],
},
});

const expectedConfig = {
personalisation: {
applicantName: "xyz",
payeeName: "payeeName",
address: "123 PLACE",
projectType:
"Internal alterations, addition of doorways to the rear of the building, and painting of facades",
projectType: "Paint the facade and changes to internal walls or layout",
emailReplyToId: "123",
helpEmail: "[email protected]",
helpOpeningHours: "9-5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { $public, $api } from "../../../../client";
import { sendEmail } from "../../../../lib/notify";
import { formatRawProjectTypes } from "@opensystemslab/planx-core";
import { gql } from "graphql-request";
import { $api } from "../../../../client";
import { sendEmail } from "../../../../lib/notify";
import type { AgentAndPayeeSubmissionNotifyConfig } from "../../../../types";

export async function sendAgentAndPayeeConfirmationEmail(sessionId: string) {
const { personalisation, applicantEmail, payeeEmail, projectTypes } =
await getDataForPayeeAndAgentEmails(sessionId);
const projectType = projectTypes.length
? await $public.formatRawProjectTypes(projectTypes)
? formatRawProjectTypes(projectTypes)
: "Project type not submitted";
const config: AgentAndPayeeSubmissionNotifyConfig = {
personalisation: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import {
validatePaymentRequestNotFoundQueryMock,
validatePaymentRequestQueryMock,
} from "../../../../tests/mocks/inviteToPayMocks";
import { CoreDomainClient } from "@opensystemslab/planx-core";

jest
.spyOn(CoreDomainClient.prototype, "formatRawProjectTypes")
.mockResolvedValue("New office premises");

const TEST_PAYMENT_REQUEST_ID = "09655c28-3f34-4619-9385-cd57312acc44";

Expand Down
25 changes: 12 additions & 13 deletions api.planx.uk/modules/pay/service/inviteToPay/sendPaymentEmail.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { formatRawProjectTypes } from "@opensystemslab/planx-core";
import type { PaymentRequest } from "@opensystemslab/planx-core/types";
import { gql } from "graphql-request";
import {
calculateExpiryDate,
getServiceLink,
} from "../../../saveAndReturn/service/utils";
import {
Template,
getClientForTemplate,
sendEmail,
} from "../../../../lib/notify";
import { InviteToPayNotifyConfig } from "../../../../types";
import { Team } from "../../../../types";
import type { PaymentRequest } from "@opensystemslab/planx-core/types";
import { $public } from "../../../../client";
import { InviteToPayNotifyConfig, Team } from "../../../../types";
import {
calculateExpiryDate,
getServiceLink,
} from "../../../saveAndReturn/service/utils";

interface SessionDetails {
email: string;
Expand All @@ -38,7 +37,7 @@ const sendSinglePaymentEmail = async ({
paymentRequestId,
template,
);
const config = await getInviteToPayNotifyConfig(session, paymentRequest);
const config = getInviteToPayNotifyConfig(session, paymentRequest);
const recipient = template.includes("-agent")
? session.email
: paymentRequest.payeeEmail;
Expand Down Expand Up @@ -105,10 +104,10 @@ const validatePaymentRequest = async (
}
};

const getInviteToPayNotifyConfig = async (
const getInviteToPayNotifyConfig = (
session: SessionDetails,
paymentRequest: PaymentRequest,
): Promise<InviteToPayNotifyConfig> => ({
): InviteToPayNotifyConfig => ({
personalisation: {
...session.flow.team.notifyPersonalisation,
sessionId: paymentRequest.sessionId,
Expand All @@ -121,9 +120,9 @@ const getInviteToPayNotifyConfig = async (
).title,
fee: getFee(paymentRequest),
projectType:
(await $public.formatRawProjectTypes(
formatRawProjectTypes(
paymentRequest.sessionPreviewData?.["proposal.projectType"] as string[],
)) || "Project type not submitted",
) || "Project type not submitted",
serviceName: session.flow.name,
serviceLink: getServiceLink(session.flow.team, session.flow.slug),
expiryDate: calculateExpiryDate(paymentRequest.createdAt),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ import { PartialDeep } from "type-fest";
const ENDPOINT = "/resume-application";
const TEST_EMAIL = "[email protected]";

const mockFormatRawProjectTypes = jest
.fn()
.mockResolvedValue(["New office premises"]);

jest.mock("@opensystemslab/planx-core", () => {
const actualCoreDomainClient = jest.requireActual(
"@opensystemslab/planx-core",
).CoreDomainClient;

return {
CoreDomainClient: class extends actualCoreDomainClient {
constructor() {
super();
this.formatRawProjectTypes = () => mockFormatRawProjectTypes();
}
},
};
});

describe("buildContentFromSessions function", () => {
it("should return correctly formatted content for a single session", async () => {
const sessions: PartialDeep<LowCalSession>[] = [
Expand All @@ -56,7 +37,7 @@ describe("buildContentFromSessions function", () => {

const result = `Service: Apply for a Lawful Development Certificate
Address: 1 High Street
Project type: New office premises
Project type: New offices
Expiry Date: 29 May 2026
Link: example.com/team/apply-for-a-lawful-development-certificate/published?sessionId=123`;
expect(
Expand Down Expand Up @@ -126,15 +107,15 @@ describe("buildContentFromSessions function", () => {
];
const result = `Service: Apply for a Lawful Development Certificate
Address: 1 High Street
Project type: New office premises
Project type: New offices
Expiry Date: 29 May 2026
Link: example.com/team/apply-for-a-lawful-development-certificate/published?sessionId=123\n\nService: Apply for a Lawful Development Certificate
Address: 2 High Street
Project type: New office premises
Project type: New offices
Expiry Date: 29 May 2026
Link: example.com/team/apply-for-a-lawful-development-certificate/published?sessionId=456\n\nService: Apply for a Lawful Development Certificate
Address: 3 High Street
Project type: New office premises
Project type: New offices
Expiry Date: 29 May 2026
Link: example.com/team/apply-for-a-lawful-development-certificate/published?sessionId=789`;
expect(
Expand Down Expand Up @@ -186,7 +167,7 @@ describe("buildContentFromSessions function", () => {
];
const result = `Service: Apply for a Lawful Development Certificate
Address: 1 High Street
Project type: New office premises
Project type: New offices
Expiry Date: 29 May 2026
Link: example.com/team/apply-for-a-lawful-development-certificate/published?sessionId=123`;
expect(
Expand Down Expand Up @@ -219,7 +200,7 @@ describe("buildContentFromSessions function", () => {

const result = `Service: Apply for a Lawful Development Certificate
Address: Address not submitted
Project type: New office premises
Project type: New offices
Expiry Date: 29 May 2026
Link: example.com/team/apply-for-a-lawful-development-certificate/published?sessionId=123`;
expect(
Expand All @@ -231,7 +212,6 @@ describe("buildContentFromSessions function", () => {
});

it("should handle an empty project type field", async () => {
mockFormatRawProjectTypes.mockResolvedValueOnce("");
const sessions: PartialDeep<LowCalSession>[] = [
{
data: {
Expand Down
11 changes: 7 additions & 4 deletions api.planx.uk/modules/saveAndReturn/service/resumeApplication.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { formatRawProjectTypes } from "@opensystemslab/planx-core";
import type { SiteAddress } from "@opensystemslab/planx-core/types";
import { differenceInDays } from "date-fns";
import { gql } from "graphql-request";
import { $api, $public } from "../../../client";
import { $api } from "../../../client";
import { sendEmail } from "../../../lib/notify";
import { LowCalSession, Team } from "../../../types";
import { DAYS_UNTIL_EXPIRY, calculateExpiryDate, getResumeLink } from "./utils";
Expand Down Expand Up @@ -109,9 +110,11 @@ const buildContentFromSessions = async (
const address: SiteAddress | undefined =
session.data?.passport?.data?._address;
const addressLine = address?.single_line_address || address?.title;
const projectType = await $public.formatRawProjectTypes(
session.data?.passport?.data?.["proposal.projectType"],
);
const projectType = session.data?.passport?.data?.["proposal.projectType"]
? formatRawProjectTypes(
session.data?.passport?.data?.["proposal.projectType"],
)
: "Project type not submitted";
const resumeLink = getResumeLink(session, team, session.flow.slug);
const expiryDate = calculateExpiryDate(session.created_at);

Expand Down
24 changes: 11 additions & 13 deletions api.planx.uk/modules/saveAndReturn/service/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { formatRawProjectTypes } from "@opensystemslab/planx-core";
import { SiteAddress } from "@opensystemslab/planx-core/types";
import { format, addDays } from "date-fns";
import { addDays, format } from "date-fns";
import { gql } from "graphql-request";
import { LowCalSession, Team } from "../../../types";
import { $api } from "../../../client";
import { Template, getClientForTemplate, sendEmail } from "../../../lib/notify";
import { $api, $public } from "../../../client";
import { LowCalSession, Team } from "../../../types";

const DAYS_UNTIL_EXPIRY = 28;
const REMINDER_DAYS_FROM_EXPIRY = [7, 1];
Expand Down Expand Up @@ -120,7 +121,7 @@ const validateSingleSessionRequest = async (
flowSlug: session.flow.slug,
flowName: session.flow.name,
team: session.flow.team,
session: await getSessionDetails(session),
session: getSessionDetails(session),
};
} catch (error) {
throw Error(`Unable to validate request. ${(error as Error).message}`);
Expand All @@ -139,14 +140,11 @@ interface SessionDetails {
/**
* Parse session details into an object which will be read by email template
*/
export const getSessionDetails = async (
session: LowCalSession,
): Promise<SessionDetails> => {
export const getSessionDetails = (session: LowCalSession): SessionDetails => {
const passportProtectTypes =
session.data.passport?.data?.["proposal.projectType"];
const projectTypes =
passportProtectTypes &&
(await $public.formatRawProjectTypes(passportProtectTypes));
passportProtectTypes && formatRawProjectTypes(passportProtectTypes);
const address: SiteAddress | undefined =
session.data?.passport?.data?._address;
const addressLine = address?.single_line_address || address?.title;
Expand Down Expand Up @@ -268,12 +266,12 @@ export const setupEmailEventTriggers = async (sessionId: string) => {
};

export {
getSaveAndReturnPublicHeaders,
getResumeLink,
sendSingleApplicationEmail,
markSessionAsSubmitted,
DAYS_UNTIL_EXPIRY,
REMINDER_DAYS_FROM_EXPIRY,
calculateExpiryDate,
getResumeLink,
getSaveAndReturnPublicHeaders,
markSessionAsSubmitted,
sendSingleApplicationEmail,
softDeleteSession,
};
5 changes: 0 additions & 5 deletions api.planx.uk/modules/sendEmail/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import {
mockValidateSingleSessionRequest,
mockValidateSingleSessionRequestMissingSession,
} from "../../tests/mocks/saveAndReturnMocks";
import { CoreDomainClient } from "@opensystemslab/planx-core";

// https://docs.notifications.service.gov.uk/node.html#email-addresses
const TEST_EMAIL = "[email protected]";
const SAVE_ENDPOINT = "/send-email/save";

jest
.spyOn(CoreDomainClient.prototype, "formatRawProjectTypes")
.mockResolvedValue("New office premises");

describe("Send Email endpoint", () => {
beforeEach(() => {
queryMock.reset();
Expand Down
2 changes: 1 addition & 1 deletion api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@airbrake/node": "^2.1.8",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b43b268",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#0c256f1",
"@types/isomorphic-fetch": "^0.0.36",
"adm-zip": "^0.5.10",
"aws-sdk": "^2.1467.0",
Expand Down
8 changes: 4 additions & 4 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/tests/api-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@cucumber/cucumber": "^9.3.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b43b268",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#0c256f1",
"axios": "^1.6.8",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/api-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/tests/ui-driven/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"postinstall": "./install-dependencies.sh"
},
"dependencies": {
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#b43b268",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#0c256f1",
"axios": "^1.6.8",
"dotenv": "^16.3.1",
"eslint": "^8.56.0",
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/ui-driven/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b3dd30e

Please sign in to comment.