Skip to content

Commit

Permalink
refactor(api): Update remaining webhook routes to modular structure (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Oct 4, 2023
1 parent 8984f39 commit fadbae7
Show file tree
Hide file tree
Showing 26 changed files with 637 additions and 332 deletions.
7 changes: 6 additions & 1 deletion api.planx.uk/hasura/metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ type RequiredScheduledEventArgs = Pick<
"webhook" | "schedule_at" | "comment" | "payload"
>;

export interface ScheduledEventResponse {
message: "success";
event_id: string;
}

/**
* POST a request to the Hasura Metadata API
* https://hasura.io/docs/latest/graphql/core/api-reference/metadata-api/index/
*/
const postToMetadataAPI = async (
body: ScheduledEvent,
): Promise<AxiosResponse<any>> => {
): Promise<AxiosResponse<ScheduledEventResponse>> => {
try {
return await Axios.post(
process.env.HASURA_METADATA_URL!,
Expand Down
17 changes: 13 additions & 4 deletions api.planx.uk/inviteToPay/createPaymentSendEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -83,29 +88,33 @@ 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)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.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)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
.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];
Expand Down
11 changes: 7 additions & 4 deletions api.planx.uk/inviteToPay/createPaymentSendEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { NextFunction, Request, Response } from "express";
import { gql } from "graphql-request";
import { $admin } from "../client";
import { adminGraphQLClient as adminClient } from "../hasura";
import { createScheduledEvent } from "../hasura/metadata";
import {
ScheduledEventResponse,
createScheduledEvent,
} from "../hasura/metadata";
import { getMostRecentPublishedFlow } from "../helpers";
import { Flow, Node, Team } from "../types";

Expand All @@ -14,9 +17,9 @@ enum Destination {
}

interface CombinedResponse {
bops?: Record<string, string>;
uniform?: Record<string, string>;
email?: Record<string, string>;
bops?: ScheduledEventResponse;
uniform?: ScheduledEventResponse;
email?: ScheduledEventResponse;
}

// Create "One-off Scheduled Events" in Hasura when a payment request is paid
Expand Down
76 changes: 0 additions & 76 deletions api.planx.uk/modules/webhooks/_old/lowcalSessionEvents.ts

This file was deleted.

136 changes: 0 additions & 136 deletions api.planx.uk/modules/webhooks/_old/paymentRequestEvents.ts

This file was deleted.

Loading

0 comments on commit fadbae7

Please sign in to comment.