diff --git a/api.planx.uk/inviteToPay/createPaymentSendEvents.test.ts b/api.planx.uk/inviteToPay/createPaymentSendEvents.test.ts index d9eac45774..cb75bf84b1 100644 --- a/api.planx.uk/inviteToPay/createPaymentSendEvents.test.ts +++ b/api.planx.uk/inviteToPay/createPaymentSendEvents.test.ts @@ -9,6 +9,11 @@ const mockedCreateScheduledEvent = createScheduledEvent as jest.MockedFunction< typeof createScheduledEvent >; +const mockScheduledEventResponse = { + message: "success", + event_id: "abc123", +} as const; + describe("Create payment send events webhook", () => { const ENDPOINT = "/webhooks/hasura/create-payment-send-events"; @@ -83,7 +88,7 @@ describe("Create payment send events webhook", () => { }); it("returns a 200 on successful event setup", async () => { - mockedCreateScheduledEvent.mockResolvedValue("test-event-id"); + mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse); await supertest(app) .post(ENDPOINT) @@ -91,13 +96,15 @@ describe("Create payment send events webhook", () => { .send({ payload: { sessionId: "123" } }) .expect(200) .then((response) => { - expect(response.body).toMatchObject({ email: "test-event-id" }); + expect(response.body).toMatchObject({ + email: mockScheduledEventResponse, + }); }); }); it("passes the correct arguments along to createScheduledEvent", async () => { const body = { createdAt: new Date(), payload: { sessionId: "123" } }; - mockedCreateScheduledEvent.mockResolvedValue("test-event-id"); + mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse); await supertest(app) .post(ENDPOINT) @@ -105,7 +112,9 @@ describe("Create payment send events webhook", () => { .send(body) .expect(200) .then((response) => { - expect(response.body).toMatchObject({ email: "test-event-id" }); + expect(response.body).toMatchObject({ + email: mockScheduledEventResponse, + }); }); const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0]; diff --git a/api.planx.uk/modules/webhooks/_old/lowcalSessionEvents.test.ts b/api.planx.uk/modules/webhooks/_old/lowcalSessionEvents.test.ts index 0cddfe272b..677b8c2b3f 100644 --- a/api.planx.uk/modules/webhooks/_old/lowcalSessionEvents.test.ts +++ b/api.planx.uk/modules/webhooks/_old/lowcalSessionEvents.test.ts @@ -9,6 +9,11 @@ const mockedCreateScheduledEvent = createScheduledEvent as jest.MockedFunction< typeof createScheduledEvent >; +const mockScheduledEventResponse = { + message: "success", + event_id: "abc123", +} as const; + describe("Create reminder event webhook", () => { const ENDPOINT = "/webhooks/hasura/create-reminder-event"; @@ -41,7 +46,7 @@ describe("Create reminder event webhook", () => { it("returns a 200 on successful event setup", async () => { const body = { createdAt: new Date(), payload: { sessionId: "123" } }; - mockedCreateScheduledEvent.mockResolvedValue("test"); + mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse); await post(ENDPOINT) .set({ Authorization: process.env.HASURA_PLANX_API_KEY }) @@ -50,20 +55,26 @@ describe("Create reminder event webhook", () => { .then((response) => { // it's queued up x2 reminders for 7 days and 1 day from expiry expect(response.body).toHaveLength(2); - expect(response.body).toStrictEqual(["test", "test"]); + expect(response.body).toStrictEqual([ + mockScheduledEventResponse, + mockScheduledEventResponse, + ]); }); }); it("passes the correct arguments along to createScheduledEvent", async () => { const body = { createdAt: new Date(), payload: { sessionId: "123" } }; - mockedCreateScheduledEvent.mockResolvedValue("test"); + mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse); await post(ENDPOINT) .set({ Authorization: process.env.HASURA_PLANX_API_KEY }) .send(body) .expect(200) .then((response) => { - expect(response.body).toStrictEqual(["test", "test"]); + expect(response.body).toStrictEqual([ + mockScheduledEventResponse, + mockScheduledEventResponse, + ]); }); const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0]; @@ -129,27 +140,27 @@ describe("Create expiry event webhook", () => { it("returns a 200 on successful event setup", async () => { const body = { createdAt: new Date(), payload: { sessionId: "123" } }; - mockedCreateScheduledEvent.mockResolvedValue("test"); + mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse); await post(ENDPOINT) .set({ Authorization: process.env.HASURA_PLANX_API_KEY }) .send(body) .expect(200) .then((response) => { - expect(response.body).toBe("test"); + expect(response.body).toStrictEqual(mockScheduledEventResponse); }); }); it("passes the correct arguments along to createScheduledEvent", async () => { const body = { createdAt: new Date(), payload: { sessionId: "123" } }; - mockedCreateScheduledEvent.mockResolvedValue("test"); + mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse); await post(ENDPOINT) .set({ Authorization: process.env.HASURA_PLANX_API_KEY }) .send(body) .expect(200) .then((response) => { - expect(response.body).toBe("test"); + expect(response.body).toStrictEqual(mockScheduledEventResponse); }); const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0]; expect(mockArgs.webhook).toBe("{{HASURA_PLANX_API_URL}}/send-email/expiry"); diff --git a/api.planx.uk/modules/webhooks/docs.yaml b/api.planx.uk/modules/webhooks/docs.yaml index da352b665a..158a8a71b7 100644 --- a/api.planx.uk/modules/webhooks/docs.yaml +++ b/api.planx.uk/modules/webhooks/docs.yaml @@ -128,14 +128,16 @@ components: content: application/json: schema: - type: object - properties: - message: - type: string - enum: ["success"] - event_id: - type: string - description: Internal Hasura ID of the generated event + type: array + items: + type: object + properties: + message: + type: string + enum: ["success"] + event_id: + type: string + description: Internal Hasura ID of the generated event paths: /webhooks/hasura/sendSlackNotification: post: diff --git a/api.planx.uk/modules/webhooks/paymentRequestEvents/schema.ts b/api.planx.uk/modules/webhooks/paymentRequestEvents/schema.ts index 4ad55670e0..293f64a541 100644 --- a/api.planx.uk/modules/webhooks/paymentRequestEvents/schema.ts +++ b/api.planx.uk/modules/webhooks/paymentRequestEvents/schema.ts @@ -1,8 +1,6 @@ import { z } from "zod"; import { ValidatedRequestHandler } from "../../../shared/middleware/validate"; - -// TODO: Make this better -type Response = [any, any] | any[]; +import { ScheduledEventResponse } from "../../../hasura/metadata"; export const createPaymentEventSchema = z.object({ body: z.object({ @@ -19,5 +17,5 @@ export type CreatePaymentEvent = z.infer< export type CreatePaymentEventController = ValidatedRequestHandler< typeof createPaymentEventSchema, - Response + ScheduledEventResponse[] >; diff --git a/api.planx.uk/modules/webhooks/sendNotification/index.test.ts b/api.planx.uk/modules/webhooks/sendNotification/index.test.ts index 2eaa93685a..9ecf4244cd 100644 --- a/api.planx.uk/modules/webhooks/sendNotification/index.test.ts +++ b/api.planx.uk/modules/webhooks/sendNotification/index.test.ts @@ -210,6 +210,7 @@ describe("Send Slack notifications endpoint", () => { expect(mockAdmin.session.find).toHaveBeenCalledTimes(1); expect(response.body.message).toBe("Posted to Slack"); expect(response.body.data).toMatch(/abc123/); + }); }); it("adds a status to the Slack message for a disability exemption", async () => { @@ -259,39 +260,6 @@ describe("Send Slack notifications endpoint", () => { }); }); - it("adds an exemption status if there's no fee for the session", async () => { - process.env.APP_ENVIRONMENT = "production"; - mockAdmin.session.find = jest - .fn() - .mockResolvedValue(mockSessionWithoutFee); - - await post(ENDPOINT) - .query({ type: "uniform-submission" }) - .set({ Authorization: process.env.HASURA_PLANX_API_KEY }) - .send(body) - .expect(200) - .then((response) => { - expect(response.body.data).toMatch(/abc123/); - expect(response.body.data).toMatch(/test-council/); - expect(response.body.data).toMatch(/[Exempt]/); - }); - }); - - it("handles missing sessions", async () => { - process.env.APP_ENVIRONMENT = "production"; - mockAdmin.session.find = jest.fn().mockResolvedValueOnce(null); - - await post(ENDPOINT) - .query({ type: "uniform-submission" }) - .set({ Authorization: process.env.HASURA_PLANX_API_KEY }) - .send(body) - .expect(500) - .then((response) => { - expect(mockAdmin.session.find).toHaveBeenCalledTimes(1); - expect(response.body.error).toMatch(/Failed to send/); - }); - }); - it("returns error when Slack fails", async () => { process.env.APP_ENVIRONMENT = "production"; mockSend.mockRejectedValue("Fail!");