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

feat: Remove duplicated Destination enums, add Send test coverage #3843

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 4 additions & 7 deletions api.planx.uk/lib/hasura/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SendIntegration } from "@opensystemslab/planx-core/types";
import type { AxiosResponse } from "axios";
import axios, { isAxiosError } from "axios";

Expand All @@ -10,13 +11,9 @@ interface ScheduledEvent {
args: ScheduledEventArgs;
}

export interface CombinedResponse {
bops?: ScheduledEventResponse;
uniform?: ScheduledEventResponse;
idox?: ScheduledEventResponse;
email?: ScheduledEventResponse;
s3?: ScheduledEventResponse;
}
export type CombinedResponse = Partial<
Record<SendIntegration, ScheduledEventResponse>
>;

interface ScheduledEventArgs {
headers: Record<string, string>[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Team } from "@opensystemslab/planx-core/types";
import type { SendIntegration, Team } from "@opensystemslab/planx-core/types";
import { ComponentType } from "@opensystemslab/planx-core/types";
import type { NextFunction, Request, Response } from "express";
import { gql } from "graphql-request";
Expand All @@ -8,12 +8,6 @@ import { $api, $public } from "../../../../client/index.js";
import { getMostRecentPublishedFlow } from "../../../../helpers.js";
import type { Flow, Node } from "../../../../types.js";

enum Destination {
BOPS = "bops",
Uniform = "uniform",
Email = "email",
}

// Create "One-off Scheduled Events" in Hasura when a payment request is paid
const createPaymentSendEvents = async (
req: Request,
Expand Down Expand Up @@ -52,32 +46,32 @@ const createPaymentSendEvents = async (
const sendNode: [string, Node] | undefined = Object.entries(
publishedFlowData,
).find(([_nodeId, nodeData]) => nodeData.type === ComponentType.Send);
const destinations: Destination[] = sendNode?.[1]?.data?.destinations;
const destinations: SendIntegration[] = sendNode?.[1]?.data?.destinations;

let teamSlug = await getTeamSlugByFlowId(session.flow.id);
const eventPayload = { sessionId: payload.sessionId };

if (destinations.includes(Destination.BOPS)) {
if (destinations.includes("bops")) {
const bopsEvent = await createScheduledEvent({
webhook: `{{HASURA_PLANX_API_URL}}/bops/${teamSlug}`,
schedule_at: now,
payload: eventPayload,
comment: `bops_submission_${payload.sessionId}`,
});
combinedResponse[Destination.BOPS] = bopsEvent;
combinedResponse["bops"] = bopsEvent;
}

if (destinations.includes(Destination.Email)) {
if (destinations.includes("email")) {
const emailSubmissionEvent = await createScheduledEvent({
webhook: `{{HASURA_PLANX_API_URL}}/email-submission/${teamSlug}`,
schedule_at: now,
payload: eventPayload,
comment: `email_submission_${payload.sessionId}`,
});
combinedResponse[Destination.Email] = emailSubmissionEvent;
combinedResponse["email"] = emailSubmissionEvent;
}

if (destinations.includes(Destination.Uniform)) {
if (destinations.includes("uniform")) {
// Bucks has 3 instances of Uniform for 4 legacy councils, set teamSlug to pre-merger council name
if (teamSlug === "buckinghamshire") {
const localAuthorities: string[] = session.data?.passport?.data?.[
Expand All @@ -101,7 +95,7 @@ const createPaymentSendEvents = async (
payload: eventPayload,
comment: `uniform_submission_${payload.sessionId}`,
});
combinedResponse[Destination.Uniform] = uniformEvent;
combinedResponse["uniform"] = uniformEvent;
}

return res.json(combinedResponse);
Expand Down
23 changes: 16 additions & 7 deletions api.planx.uk/modules/send/createSendEvents/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { z } from "zod";
import type { CombinedResponse } from "../../../lib/hasura/metadata/index.js";
import type { ValidatedRequestHandler } from "../../../shared/middleware/validate.js";
import {
SEND_INTEGRATIONS,
type SendIntegration,
} from "@opensystemslab/planx-core/types";

const eventSchema = z.object({
localAuthority: z.string(),
Expand All @@ -9,14 +13,19 @@ const eventSchema = z.object({
}),
});

/** Iterate over all possible SendIntegrations to generate the body for this endpoint */
const bodySchema = z.object(
SEND_INTEGRATIONS.reduce(
(acc, integration) => {
acc[integration] = eventSchema.optional();
return acc;
},
{} as Record<SendIntegration, z.ZodOptional<typeof eventSchema>>,
),
);

export const combinedEventsPayloadSchema = z.object({
body: z.object({
email: eventSchema.optional(),
bops: eventSchema.optional(),
uniform: eventSchema.optional(),
s3: eventSchema.optional(),
idox: eventSchema.optional(),
}),
body: bodySchema,
params: z.object({
sessionId: z.string().uuid(),
}),
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 @@ -11,7 +11,7 @@
},
"dependencies": {
"@airbrake/node": "^2.1.8",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4cc216f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4916ba8",
"@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": "pnpm@8.6.6",
"dependencies": {
"@cucumber/cucumber": "^9.3.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4cc216f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4916ba8",
"axios": "^1.7.4",
"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#4cc216f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4916ba8",
"axios": "^1.7.4",
"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.

2 changes: 1 addition & 1 deletion editor.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@mui/material": "^5.15.10",
"@mui/utils": "^5.15.11",
"@opensystemslab/map": "1.0.0-alpha.3",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4cc216f",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#4916ba8",
"@tiptap/core": "^2.4.0",
"@tiptap/extension-bold": "^2.0.3",
"@tiptap/extension-bubble-menu": "^2.1.13",
Expand Down
10 changes: 5 additions & 5 deletions editor.planx.uk/pnpm-lock.yaml

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

28 changes: 14 additions & 14 deletions editor.planx.uk/src/@planx/components/Send/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Warning from "@mui/icons-material/Warning";
import Box from "@mui/material/Box";
import Grid from "@mui/material/Grid";
import Typography from "@mui/material/Typography";
import { ComponentType as TYPES } from "@opensystemslab/planx-core/types";
import { ComponentType as TYPES, SendIntegration } from "@opensystemslab/planx-core/types";
import { getIn, useFormik } from "formik";
import React from "react";
import ModalSection from "ui/editor/ModalSection";
Expand All @@ -14,7 +14,7 @@ import InputRow from "ui/shared/InputRow";
import { array, object } from "yup";

import { EditorProps, ICONS } from "../ui";
import { Destination, Send } from "./model";
import { Send } from "./model";
import { parseContent } from "./model";

export type Props = EditorProps<TYPES.Send, Send>;
Expand All @@ -35,40 +35,40 @@ const SendComponent: React.FC<Props> = (props) => {
.test({
name: "atLeastOneChecked",
message: "Select at least one destination",
test: (destinations?: Array<Destination>) => {
test: (destinations?: Array<SendIntegration>) => {
return Boolean(destinations && destinations.length > 0);
},
}),
}),
});

const options: { value: Destination; label: string }[] = [
const options: { value: SendIntegration; label: string }[] = [
{
value: Destination.BOPS,
value: "bops",
label: "BOPS",
},
{
value: Destination.Uniform,
value: "uniform",
label: "Uniform",
},
{
value: Destination.Idox,
value: "idox",
label: "Idox Nexus (TESTING ONLY)",
},
{
value: Destination.Email,
value: "email",
label: "Email to planning office",
},
{
value: Destination.S3,
value: "s3",
label: "Upload to AWS S3 bucket",
},
];

const changeCheckbox =
(value: Destination) =>
(value: SendIntegration) =>
(_checked: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined) => {
let newCheckedValues: Destination[];
let newCheckedValues: SendIntegration[];

if (formik.values.destinations.includes(value)) {
newCheckedValues = formik.values.destinations.filter(
Expand All @@ -90,14 +90,14 @@ const SendComponent: React.FC<Props> = (props) => {
// Don't actually restrict selection because flowSlug matching is imperfect for some valid test cases
const teamSlug = window.location.pathname?.split("/")?.[1];
const flowSlug = window.location.pathname?.split("/")?.[2];
if (value === Destination.BOPS && newCheckedValues.includes(value)) {
if (value === "bops" && newCheckedValues.includes(value)) {
alert(
"BOPS only accepts Lawful Development Certificate, Prior Approval, and Planning Permission submissions. Please do not select if you're building another type of submission service!",
);
}

if (
value === Destination.Uniform &&
value === "uniform" &&
newCheckedValues.includes(value) &&
flowSlug !== "apply-for-a-lawful-development-certificate" &&
!["buckinghamshire", "lambeth", "southwark"].includes(teamSlug)
Expand All @@ -108,7 +108,7 @@ const SendComponent: React.FC<Props> = (props) => {
}

if (
value === Destination.S3 &&
value === "s3" &&
newCheckedValues.includes(value) &&
!["barnet", "lambeth"].includes(teamSlug)
) {
Expand Down
Loading
Loading