Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): Update remaining webhook routes to modular structure #2252

Merged
merged 14 commits into from
Oct 4, 2023
Merged
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing this cascaded across a few functions and tests which is a bit noisy for a PR, but quite nice in practice.

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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is repeated in api.planx.uk/send/createSendEvents.ts but I'm ignoring this for now as this PR is already bigger than it should be.

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
Loading