Skip to content

Commit

Permalink
Revert "feat: setup a Slack notification when flow status is updated" (
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak authored Aug 5, 2024
1 parent 9c869fc commit 862b33e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 187 deletions.
32 changes: 3 additions & 29 deletions api.planx.uk/modules/webhooks/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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/);
});
});
});
});
62 changes: 23 additions & 39 deletions api.planx.uk/modules/webhooks/service/sendNotification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
EmailEventData,
EventData,
EventType,
FlowStatusEventData,
S3EventData,
UniformEventData,
} from "./types";
Expand All @@ -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) =>
Expand All @@ -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) => {
Expand Down
17 changes: 0 additions & 17 deletions api.planx.uk/modules/webhooks/service/sendNotification/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ValidatedRequestHandler } from "../../../../shared/middleware/validate"
import {
bopsSubmissionSchema,
emailSubmissionSchema,
flowStatusSchema,
s3SubmissionSchema,
sendSlackNotificationSchema,
uniformSubmissionSchema,
Expand All @@ -30,15 +29,11 @@ export type EmailEventData = EmailBody["event"]["data"]["new"];
export type S3Body = z.infer<typeof s3SubmissionSchema>["body"];
export type S3EventData = S3Body["event"]["data"]["new"];

export type FlowStatusBody = z.infer<typeof flowStatusSchema>["body"];
export type FlowStatusEventData = FlowStatusBody["event"]["data"]["new"];

export type EventData =
| BOPSEventData
| UniformEventData
| EmailEventData
| S3EventData
| FlowStatusEventData;
| S3EventData;

export type SendSlackNotification = ValidatedRequestHandler<
typeof sendSlackNotificationSchema,
Expand Down
29 changes: 0 additions & 29 deletions hasura.planx.uk/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 862b33e

Please sign in to comment.