From 862b33e28e7c26d72195bc8aa387ed26fb83ef0e Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Mon, 5 Aug 2024 17:47:24 +0200 Subject: [PATCH] Revert "feat: setup a Slack notification when flow status is updated" (#3493) --- api.planx.uk/modules/webhooks/docs.yaml | 32 +-------- .../service/sendNotification/index.test.ts | 68 +------------------ .../service/sendNotification/index.ts | 62 +++++++---------- .../service/sendNotification/schema.ts | 17 ----- .../service/sendNotification/types.ts | 7 +- hasura.planx.uk/metadata/tables.yaml | 29 -------- 6 files changed, 28 insertions(+), 187 deletions(-) diff --git a/api.planx.uk/modules/webhooks/docs.yaml b/api.planx.uk/modules/webhooks/docs.yaml index 55c7fd7c33..a392d99b26 100644 --- a/api.planx.uk/modules/webhooks/docs.yaml +++ b/api.planx.uk/modules/webhooks/docs.yaml @@ -103,32 +103,13 @@ components: team_slug: type: string required: - - body - FlowStatusChange: - type: object - properties: - event: - type: object - properties: - data: - type: object - properties: - new: - type: object - properties: - id: - type: string; - status: - type: string; - required: - - body + - event SendSlackNotification: oneOf: - $ref: "#/components/schemas/BopsSubmission" - $ref: "#/components/schemas/UniformSubmission" - $ref: "#/components/schemas/EmailSubmission" - $ref: "#/components/schemas/S3Submission" - - $ref: "#/components/schemas/FlowStatusChange" CreatePaymentEvent: type: object properties: @@ -269,19 +250,12 @@ paths: security: - hasuraAuth: [] summary: Send Slack notification - description: Endpoint to trigger a Slack notification following a database event (eg insert record into submission audit table, update a flow table column) + description: Endpoint to trigger a Slack notification following a submission event parameters: - in: query name: type type: string - enum: - [ - "bops-submission", - "uniform-submission", - "email-submission", - "s3-submission", - "flow-status", - ] + enum: ["bops-submission", "uniform-submission", "email-submission"] required: true requestBody: content: diff --git a/api.planx.uk/modules/webhooks/service/sendNotification/index.test.ts b/api.planx.uk/modules/webhooks/service/sendNotification/index.test.ts index 986f6f8af8..433f8dc21b 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/index.test.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/index.test.ts @@ -1,13 +1,7 @@ import supertest from "supertest"; import app from "../../../../server"; import SlackNotify from "slack-notify"; -import { - BOPSBody, - EmailBody, - FlowStatusBody, - S3Body, - UniformBody, -} from "./types"; +import { BOPSBody, EmailBody, S3Body, UniformBody } from "./types"; import { $api } from "../../../../client"; import { CoreDomainClient } from "@opensystemslab/planx-core"; @@ -405,64 +399,4 @@ describe("Send Slack notifications endpoint", () => { }); }); }); - - describe("Flow status update notification", () => { - const body: FlowStatusBody = { - event: { - data: { - new: { - id: "flow-id-369", - status: "online", - }, - }, - }, - }; - - it("skips the staging environment", async () => { - process.env.APP_ENVIRONMENT = "staging"; - await post(ENDPOINT) - .query({ type: "flow-status" }) - .set({ Authorization: process.env.HASURA_PLANX_API_KEY! }) - .send(body) - .expect(200) - .then((response) => { - expect(response.body.message).toMatch(/skipping Slack notification/); - }); - }); - - it("posts to Slack on success", async () => { - process.env.APP_ENVIRONMENT = "production"; - - await post(ENDPOINT) - .query({ type: "flow-status" }) - .set({ Authorization: process.env.HASURA_PLANX_API_KEY! }) - .send(body) - .expect(200) - .then((response) => { - expect(SlackNotify).toHaveBeenCalledWith( - process.env.SLACK_WEBHOOK_URL, - ); - expect(mockSend).toHaveBeenCalledTimes(1); - expect(response.body.message).toBe("Posted to Slack"); - expect(response.body.data).toMatch(/large_green_circle/); - expect(response.body.data).toMatch(/flow-id-369/); - expect(response.body.data).toMatch(/online/); - }); - }); - - it("returns error when Slack fails", async () => { - process.env.APP_ENVIRONMENT = "production"; - mockSend.mockRejectedValue("Fail!"); - - await post(ENDPOINT) - .query({ type: "flow-status" }) - .set({ Authorization: process.env.HASURA_PLANX_API_KEY! }) - .send(body) - .expect(500) - .then((response) => { - expect(mockSend).toHaveBeenCalledTimes(1); - expect(response.body.error).toMatch(/Failed to send/); - }); - }); - }); }); diff --git a/api.planx.uk/modules/webhooks/service/sendNotification/index.ts b/api.planx.uk/modules/webhooks/service/sendNotification/index.ts index 1548164b5c..c35bc225cf 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/index.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/index.ts @@ -5,7 +5,6 @@ import { EmailEventData, EventData, EventType, - FlowStatusEventData, S3EventData, UniformEventData, } from "./types"; @@ -16,52 +15,38 @@ export const sendSlackNotification = async ( type: EventType, ) => { const slack = SlackNotify(process.env.SLACK_WEBHOOK_URL!); - const message = await getMessageForEventType(data, type); + let message = getMessageForEventType(data, type); - await slack.send(message); + const sessionId = getSessionIdFromEvent(data, type); + const { disability, resubmission } = + await getExemptionStatusesForSession(sessionId); + if (disability) message += " [Exempt]"; + if (resubmission) message += " [Resubmission]"; + + await slack.send(":incoming_envelope: " + message); return message; }; -const getMessageForEventType = async (data: EventData, type: EventType) => { - let message = ""; - if (type.endsWith("-submission")) { - const emoji = ":incoming_message:"; - if (type === "bops-submission") { - const { bops_id, destination_url } = data as BOPSEventData; - message = `${emoji} New BOPS submission *${bops_id}* [${destination_url}]`; - } - - if (type === "uniform-submission") { - const { submission_reference, response } = data as UniformEventData; - message = `${emoji} New Uniform submission *${submission_reference}* [${response.organisation}]`; - } - - if (type === "email-submission") { - const { request, session_id, team_slug } = data as EmailEventData; - message = `${emoji} New email submission "${request.personalisation.serviceName}" *${session_id}* [${team_slug}]`; - } - - if (type === "s3-submission") { - const { session_id, team_slug } = data as S3EventData; - message = `${emoji} New S3 + Power Automate submission *${session_id}* [${team_slug}]`; - } +const getMessageForEventType = (data: EventData, type: EventType) => { + if (type === "bops-submission") { + const { bops_id, destination_url } = data as BOPSEventData; + return `New BOPS submission *${bops_id}* [${destination_url}]`; + } - const sessionId = getSessionIdFromEvent(data, type); - if (sessionId) { - const { disability, resubmission } = - await getExemptionStatusesForSession(sessionId); - if (disability) message += " [Exempt]"; - if (resubmission) message += " [Resubmission]"; - } + if (type === "uniform-submission") { + const { submission_reference, response } = data as UniformEventData; + return `New Uniform submission *${submission_reference}* [${response.organisation}]`; } - if (type === "flow-status") { - const { id: flowId, status } = data as FlowStatusEventData; - const emoji = status === "online" ? ":large_green_circle:" : ":no_entry:"; - message = `${emoji} Flow is now *${status}* (${flowId})`; + if (type === "email-submission") { + const { request, session_id, team_slug } = data as EmailEventData; + return `New email submission "${request.personalisation.serviceName}" *${session_id}* [${team_slug}]`; } - return message; + if (type === "s3-submission") { + const { session_id, team_slug } = data as S3EventData; + return `New S3 + Power Automate submission *${session_id}* [${team_slug}]`; + } }; const getSessionIdFromEvent = (data: EventData, type: EventType) => @@ -70,7 +55,6 @@ const getSessionIdFromEvent = (data: EventData, type: EventType) => "uniform-submission": (data as UniformEventData).payload?.sessionId, "email-submission": (data as EmailEventData).session_id, "s3-submission": (data as S3EventData).session_id, - "flow-status": undefined, })[type]; const getExemptionStatusesForSession = async (sessionId: string) => { diff --git a/api.planx.uk/modules/webhooks/service/sendNotification/schema.ts b/api.planx.uk/modules/webhooks/service/sendNotification/schema.ts index 1cc9d92b2d..0ecf85ccf5 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/schema.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/schema.ts @@ -75,26 +75,9 @@ export const s3SubmissionSchema = z.object({ }), }); -export const flowStatusSchema = z.object({ - body: z.object({ - event: z.object({ - data: z.object({ - new: z.object({ - id: z.string(), - status: z.string(), - }), - }), - }), - }), - query: z.object({ - type: z.literal("flow-status"), - }), -}); - export const sendSlackNotificationSchema = z.union([ bopsSubmissionSchema, uniformSubmissionSchema, emailSubmissionSchema, s3SubmissionSchema, - flowStatusSchema, ]); diff --git a/api.planx.uk/modules/webhooks/service/sendNotification/types.ts b/api.planx.uk/modules/webhooks/service/sendNotification/types.ts index e3a2d9998f..5373cebb5f 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/types.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/types.ts @@ -3,7 +3,6 @@ import { ValidatedRequestHandler } from "../../../../shared/middleware/validate" import { bopsSubmissionSchema, emailSubmissionSchema, - flowStatusSchema, s3SubmissionSchema, sendSlackNotificationSchema, uniformSubmissionSchema, @@ -30,15 +29,11 @@ export type EmailEventData = EmailBody["event"]["data"]["new"]; export type S3Body = z.infer["body"]; export type S3EventData = S3Body["event"]["data"]["new"]; -export type FlowStatusBody = z.infer["body"]; -export type FlowStatusEventData = FlowStatusBody["event"]["data"]["new"]; - export type EventData = | BOPSEventData | UniformEventData | EmailEventData - | S3EventData - | FlowStatusEventData; + | S3EventData; export type SendSlackNotification = ValidatedRequestHandler< typeof sendSlackNotificationSchema, diff --git a/hasura.planx.uk/metadata/tables.yaml b/hasura.planx.uk/metadata/tables.yaml index 7ca4488d28..fd56e9f079 100644 --- a/hasura.planx.uk/metadata/tables.yaml +++ b/hasura.planx.uk/metadata/tables.yaml @@ -674,35 +674,6 @@ _eq: x-hasura-user-id - role: _eq: teamEditor - event_triggers: - - name: setup_flow_status_notifications - definition: - enable_manual: false - update: - columns: - - status - retry_conf: - interval_sec: 30 - num_retries: 1 - timeout_sec: 60 - webhook_from_env: HASURA_PLANX_API_URL - headers: - - name: authorization - value_from_env: HASURA_PLANX_API_KEY - request_transform: - body: - action: transform - template: |- - { - "id": {{$body.event.data.new.id}}, - "status": {{$body.event.data.new.status}} - } - method: POST - query_params: - type: flow-status - template_engine: Kriti - url: '{{$base_url}}/webhooks/hasura/send-slack-notification' - version: 2 - table: name: global_settings schema: public