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

Production deploy #2287

Merged
merged 8 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
135 changes: 135 additions & 0 deletions api.planx.uk/modules/webhooks/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { ServerError } from "../../errors";
import { CreateSessionEventController } from "./service/lowcalSessionEvents/schema";
import {
createSessionExpiryEvent,
createSessionReminderEvent,
} from "./service/lowcalSessionEvents";
import { SendSlackNotification } from "./service/sendNotification/types";
import { sendSlackNotification } from "./service/sendNotification";
import { CreatePaymentEventController } from "./service/paymentRequestEvents/schema";
import {
createPaymentExpiryEvents,
createPaymentInvitationEvents,
createPaymentReminderEvents,
} from "./service/paymentRequestEvents";
import { SanitiseApplicationData } from "./service/sanitiseApplicationData/types";
import { sanitiseApplicationData } from "./service/sanitiseApplicationData";

export const sendSlackNotificationController: SendSlackNotification = async (
req,
res,
next,
) => {
const isProduction = process.env.APP_ENVIRONMENT === "production";
if (!isProduction) {
return res.status(200).send({
message: `Staging application submitted, skipping Slack notification`,
});
}

const eventData = req.body.event.data.new;
const eventType = req.query.type;

try {
const data = await sendSlackNotification(eventData, eventType);
return res.status(200).send({ message: "Posted to Slack", data });
} catch (error) {
return next(
new ServerError({
message: `Failed to send ${eventType} Slack notification`,
cause: error,
}),
);
}
};

export const createPaymentInvitationEventsController: CreatePaymentEventController =
async (req, res, next) => {
try {
const response = await createPaymentInvitationEvents(req.body);
res.json(response);
} catch (error) {
return next(
new ServerError({
message: `Failed to create payment invitation events`,
cause: error,
}),
);
}
};

export const createPaymentReminderEventsController: CreatePaymentEventController =
async (req, res, next) => {
try {
const response = await createPaymentReminderEvents(req.body);
res.json(response);
} catch (error) {
return next(
new ServerError({
message: "Failed to create payment reminder events",
cause: error,
}),
);
}
};

export const createPaymentExpiryEventsController: CreatePaymentEventController =
async (req, res, next) => {
try {
const response = await createPaymentExpiryEvents(req.body);
res.json(response);
} catch (error) {
return next(
new ServerError({
message: "Failed to create payment expiry events",
cause: error,
}),
);
}
};

export const createSessionReminderEventController: CreateSessionEventController =
async (req, res, next) => {
try {
const response = await createSessionReminderEvent(req.body);
res.json(response);
} catch (error) {
return next(
new ServerError({
message: "Failed to create session reminder event",
cause: error,
}),
);
}
};

export const createSessionExpiryEventController: CreateSessionEventController =
async (req, res, next) => {
try {
const response = await createSessionExpiryEvent(req.body);
res.json(response);
} catch (error) {
return next(
new ServerError({
message: "Failed to create session expiry event",
cause: error,
}),
);
}
};

export const sanitiseApplicationDataController: SanitiseApplicationData =
async (_req, res, next) => {
try {
const { operationFailed, results } = await sanitiseApplicationData();
if (operationFailed) res.status(500);
return res.json(results);
} catch (error) {
return next(
new ServerError({
message: "Failed to sanitise application data",
cause: error,
}),
);
}
};
Loading
Loading