Skip to content

Commit

Permalink
docs: Add response type
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Sep 30, 2023
1 parent e92175b commit b9babd2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 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
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
18 changes: 15 additions & 3 deletions api.planx.uk/modules/webhooks/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ components:
message:
type: string
required: true
ScheduledEvent:
content:
application/json:
schema:
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:
Expand Down Expand Up @@ -158,7 +170,7 @@ paths:
$ref: "#/components/schemas/CreatePaymentEvent"
responses:
"200":
$ref: "#/components/responses/TODO"
$ref: "#/components/responses/ScheduledEvent"
"500":
$ref: "#/components/responses/ErrorMessage"
/webhooks/hasura/create-payment-reminder-events:
Expand All @@ -173,7 +185,7 @@ paths:
$ref: "#/components/schemas/CreatePaymentEvent"
responses:
"200":
$ref: "#/components/responses/TODO"
$ref: "#/components/responses/ScheduledEvent"
"500":
$ref: "#/components/responses/ErrorMessage"
/webhooks/hasura/create-payment-expiry-events:
Expand All @@ -188,6 +200,6 @@ paths:
$ref: "#/components/schemas/CreatePaymentEvent"
responses:
"200":
$ref: "#/components/responses/TODO"
$ref: "#/components/responses/ScheduledEvent"
"500":
$ref: "#/components/responses/ErrorMessage"
51 changes: 39 additions & 12 deletions api.planx.uk/modules/webhooks/paymentRequestEvents/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const mockedCreateScheduledEvent = createScheduledEvent as jest.MockedFunction<
typeof createScheduledEvent
>;

const mockScheduledEventResponse = {
message: "success",
event_id: "abc123",
} as const;

describe("Create payment invitation events webhook", () => {
const ENDPOINT = "/webhooks/hasura/create-payment-invitation-events";

Expand Down Expand Up @@ -46,7 +51,7 @@ describe("Create payment invitation events webhook", () => {
createdAt: new Date(),
payload: { paymentRequestId: "123" },
};
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
Expand All @@ -55,7 +60,10 @@ describe("Create payment invitation events webhook", () => {
.then((response) => {
// it's queued up x2 invitations: one for the payee and one for the agent
expect(response.body).toHaveLength(2);
expect(response.body).toStrictEqual(["test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});
});

Expand All @@ -64,15 +72,18 @@ describe("Create payment invitation events webhook", () => {
createdAt: new Date(),
payload: { paymentRequestId: "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).toHaveLength(2);
expect(response.body).toStrictEqual(["test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});

const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0];
Expand Down Expand Up @@ -149,7 +160,7 @@ describe("Create payment reminder events webhook", () => {
createdAt: new Date(),
payload: { paymentRequestId: "123" },
};
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
Expand All @@ -158,7 +169,12 @@ describe("Create payment reminder events webhook", () => {
.then((response) => {
// it's queued up x4 reminders: 2 for the payee and 2 for the agent at 7 days and 1 day from expiry
expect(response.body).toHaveLength(4);
expect(response.body).toStrictEqual(["test", "test", "test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});
});

Expand All @@ -167,15 +183,20 @@ describe("Create payment reminder events webhook", () => {
createdAt: new Date(),
payload: { paymentRequestId: "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).toHaveLength(4);
expect(response.body).toStrictEqual(["test", "test", "test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});

const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0];
Expand Down Expand Up @@ -270,7 +291,7 @@ describe("Create payment expiry events webhook", () => {
createdAt: new Date(),
payload: { paymentRequestId: "123" },
};
mockedCreateScheduledEvent.mockResolvedValue("test");
mockedCreateScheduledEvent.mockResolvedValue(mockScheduledEventResponse);

await post(ENDPOINT)
.set({ Authorization: process.env.HASURA_PLANX_API_KEY })
Expand All @@ -279,7 +300,10 @@ describe("Create payment expiry events webhook", () => {
.then((response) => {
// it's queued up x2 expiries: one for the payee and one for the agent
expect(response.body).toHaveLength(2);
expect(response.body).toStrictEqual(["test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});
});

Expand All @@ -288,15 +312,18 @@ describe("Create payment expiry events webhook", () => {
createdAt: new Date(),
payload: { paymentRequestId: "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).toHaveLength(2);
expect(response.body).toStrictEqual(["test", "test"]);
expect(response.body).toStrictEqual([
mockScheduledEventResponse,
mockScheduledEventResponse,
]);
});

const mockArgs = mockedCreateScheduledEvent.mock.calls[0][0];
Expand Down
11 changes: 7 additions & 4 deletions api.planx.uk/send/createSendEvents.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { NextFunction, Request, Response } from "express";
import { createScheduledEvent } from "../hasura/metadata";
import {
ScheduledEventResponse,
createScheduledEvent,
} from "../hasura/metadata";

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 from Send component for selected destinations
Expand Down

0 comments on commit b9babd2

Please sign in to comment.