diff --git a/api.planx.uk/modules/webhooks/docs.yaml b/api.planx.uk/modules/webhooks/docs.yaml index b9e0e26050..947837a003 100644 --- a/api.planx.uk/modules/webhooks/docs.yaml +++ b/api.planx.uk/modules/webhooks/docs.yaml @@ -259,12 +259,19 @@ paths: security: - hasuraAuth: [] summary: Send Slack notification - description: Endpoint to trigger a Slack notification following a submission event + description: Endpoint to trigger a Slack notification following a database event (eg insert record into submission audit table, update a flow table column) parameters: - in: query name: type type: string - enum: ["bops-submission", "uniform-submission", "email-submission"] + enum: + [ + "bops-submission", + "uniform-submission", + "email-submission", + "s3-submission", + "flow-status", + ] 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 ec6ba4aab9..561bf0f287 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/index.test.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/index.test.ts @@ -1,7 +1,13 @@ import supertest from "supertest"; import app from "../../../../server"; import SlackNotify from "slack-notify"; -import { BOPSBody, EmailBody, FlowStatusBody, S3Body, UniformBody } from "./types"; +import { + BOPSBody, + EmailBody, + FlowStatusBody, + S3Body, + UniformBody, +} from "./types"; import { $api } from "../../../../client"; import { CoreDomainClient } from "@opensystemslab/planx-core"; @@ -406,10 +412,9 @@ describe("Send Slack notifications endpoint", () => { data: { new: { status: "online", - id: "flow-123" - } - } - } + }, + }, + }, }; it("skips the staging environment", async () => { @@ -439,7 +444,6 @@ describe("Send Slack notifications endpoint", () => { expect(mockSend).toHaveBeenCalledTimes(1); expect(response.body.message).toBe("Posted to Slack"); expect(response.body.data).toMatch(/online/); - expect(response.body.data).toMatch(/flow-123/); }); }); diff --git a/api.planx.uk/modules/webhooks/service/sendNotification/index.ts b/api.planx.uk/modules/webhooks/service/sendNotification/index.ts index 8cb2ffd886..782fc16dfe 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/index.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/index.ts @@ -16,56 +16,62 @@ export const sendSlackNotification = async ( type: EventType, ) => { const slack = SlackNotify(process.env.SLACK_WEBHOOK_URL!); - let message = getMessageForEventType(data, type); - let emoji = getEmojiForEventType(type, data); - - const sessionId = getSessionIdFromEvent(data, type); - const { disability, resubmission } = - await getExemptionStatusesForSession(sessionId); - if (disability) message += " [Exempt]"; - if (resubmission) message += " [Resubmission]"; + const message = await getMessageForEventType(data, type); + const emoji = getEmojiForEventType(data, type); await slack.send(emoji + " " + message); return message; }; -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 getMessageForEventType = async (data: EventData, type: EventType) => { + let message = ""; + if (type.endsWith("-submission")) { + if (type === "bops-submission") { + const { bops_id, destination_url } = data as BOPSEventData; + message = `New BOPS submission *${bops_id}* [${destination_url}]`; + } - if (type === "uniform-submission") { - const { submission_reference, response } = data as UniformEventData; - return `New Uniform submission *${submission_reference}* [${response.organisation}]`; - } + if (type === "uniform-submission") { + const { submission_reference, response } = data as UniformEventData; + message = `New Uniform submission *${submission_reference}* [${response.organisation}]`; + } - if (type === "email-submission") { - const { request, session_id, team_slug } = data as EmailEventData; - return `New email submission "${request.personalisation.serviceName}" *${session_id}* [${team_slug}]`; - } + if (type === "email-submission") { + const { request, session_id, team_slug } = data as EmailEventData; + message = `New email submission "${request.personalisation.serviceName}" *${session_id}* [${team_slug}]`; + } - if (type === "s3-submission") { - const { session_id, team_slug } = data as S3EventData; - return `New S3 + Power Automate submission *${session_id}* [${team_slug}]`; + if (type === "s3-submission") { + const { session_id, team_slug } = data as S3EventData; + message = `New S3 + Power Automate submission *${session_id}* [${team_slug}]`; + } + + const sessionId = getSessionIdFromEvent(data, type); + const { disability, resubmission } = + await getExemptionStatusesForSession(sessionId); + if (disability) message += " [Exempt]"; + if (resubmission) message += " [Resubmission]"; } if (type === "flow-status") { - const { id, status } = data as FlowStatusEventData; - return `*${team_slug}/${flow_slug}* is now *${status}* (${id})`; + const { status } = data as FlowStatusEventData; + message = `Flow is now *${status}*`; + // return `*${team_slug}/${flow_slug}* is now *${status}* (${id})`; } + + return message; }; -const getEmojiForEventType = (type: EventType, data?: EventType) => { +const getEmojiForEventType = (data: EventData, type: EventType) => { if (type.endsWith("-submission")) { - return ":incoming_message:" + return ":incoming_message:"; } if (type === "flow-status") { - const { id, status } = data as FlowStatusEventData; + const { status } = data as FlowStatusEventData; return status === "online" ? ":large_green_cirlce:" : ":no_entry:"; } -} +}; const getSessionIdFromEvent = (data: EventData, type: EventType) => ({ @@ -73,7 +79,7 @@ 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": (data as FlowStatusEventData).id, + "flow-status": (data as FlowStatusEventData).status, })[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 f39f13d7cc..f48433c944 100644 --- a/api.planx.uk/modules/webhooks/service/sendNotification/schema.ts +++ b/api.planx.uk/modules/webhooks/service/sendNotification/schema.ts @@ -80,7 +80,6 @@ export const flowStatusSchema = z.object({ event: z.object({ data: z.object({ new: z.object({ - id: z.string(), status: z.string(), }), }), @@ -96,4 +95,5 @@ export const sendSlackNotificationSchema = z.union([ uniformSubmissionSchema, emailSubmissionSchema, s3SubmissionSchema, + flowStatusSchema, ]);