Skip to content

Commit

Permalink
setup and refactor sendNotification to be less submission specific
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Jul 29, 2024
1 parent 5acb022 commit 2d105ab
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
11 changes: 9 additions & 2 deletions api.planx.uk/modules/webhooks/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -406,10 +412,9 @@ describe("Send Slack notifications endpoint", () => {
data: {
new: {
status: "online",
id: "flow-123"
}
}
}
},
},
},
};

it("skips the staging environment", async () => {
Expand Down Expand Up @@ -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/);
});
});

Expand Down
68 changes: 37 additions & 31 deletions api.planx.uk/modules/webhooks/service/sendNotification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,70 @@ 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) =>
({
"bops-submission": (data as BOPSEventData).session_id,
"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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const flowStatusSchema = z.object({
event: z.object({
data: z.object({
new: z.object({
id: z.string(),
status: z.string(),
}),
}),
Expand All @@ -96,4 +95,5 @@ export const sendSlackNotificationSchema = z.union([
uniformSubmissionSchema,
emailSubmissionSchema,
s3SubmissionSchema,
flowStatusSchema,
]);

0 comments on commit 2d105ab

Please sign in to comment.