feat: booking no show webhook (#15502)
* WIP Signed-off-by: zomars <[email protected]> * WIP Signed-off-by: zomars <[email protected]> * WIP Signed-off-by: zomars <[email protected]> * Type fixes * Update webhook.e2e.ts * Update noShow.handler.ts * Log failed results * Updates tests * Show generic error on frontend. * test: add basic webhook service test * fix: webhook end to end test * fix: type error * fix: test --------- Signed-off-by: zomars <[email protected]> Co-authored-by: sean-brydon <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
14 files changed
+371
-156
No files matched your search
@@ -38,6 +38,7 @@ const WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP_V2: Record<string, WebhookTriggerEve
|
||||
{ value: WebhookTriggerEvents.BOOKING_PAYMENT_INITIATED, label: "booking_payment_initiated" },
|
||||
{ value: WebhookTriggerEvents.BOOKING_RESCHEDULED, label: "booking_rescheduled" },
|
||||
{ value: WebhookTriggerEvents.BOOKING_PAID, label: "booking_paid" },
|
||||
{ value: WebhookTriggerEvents.BOOKING_NO_SHOW_UPDATED, label: "booking_no_show_updated" },
|
||||
{ value: WebhookTriggerEvents.MEETING_ENDED, label: "meeting_ended" },
|
||||
{ value: WebhookTriggerEvents.MEETING_STARTED, label: "meeting_started" },
|
||||
{ value: WebhookTriggerEvents.RECORDING_READY, label: "recording_ready" },
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
|
||||
import getWebhooks from "./getWebhooks";
|
||||
import sendOrSchedulePayload from "./sendOrSchedulePayload";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["[WebhookService] "] });
|
||||
|
||||
/** This is a WIP. With minimal methods until the API matures and stabilizes */
|
||||
export class WebhookService {
|
||||
private options = {} as Parameters<typeof getWebhooks>[0];
|
||||
private webhooks: Awaited<ReturnType<typeof getWebhooks>> = [];
|
||||
constructor(options: Parameters<typeof getWebhooks>[0]) {
|
||||
return (async (): Promise<WebhookService> => {
|
||||
this.options = options;
|
||||
this.webhooks = await getWebhooks(options);
|
||||
return this;
|
||||
})() as unknown as WebhookService;
|
||||
}
|
||||
async getWebhooks() {
|
||||
return this.webhooks;
|
||||
}
|
||||
async sendPayload(payload: Parameters<typeof sendOrSchedulePayload>[4]) {
|
||||
const promises = this.webhooks.map((sub) =>
|
||||
sendOrSchedulePayload(
|
||||
sub.secret,
|
||||
this.options.triggerEvent,
|
||||
new Date().toISOString(),
|
||||
sub,
|
||||
payload
|
||||
).catch((e) => {
|
||||
log.error(
|
||||
`Error executing webhook for event: ${this.options.triggerEvent}, URL: ${sub.subscriberUrl}`,
|
||||
safeStringify(e)
|
||||
);
|
||||
})
|
||||
);
|
||||
await Promise.allSettled(promises);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ export const WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP = {
|
||||
WebhookTriggerEvents.BOOKING_REJECTED,
|
||||
WebhookTriggerEvents.RECORDING_READY,
|
||||
WebhookTriggerEvents.INSTANT_MEETING,
|
||||
WebhookTriggerEvents.BOOKING_NO_SHOW_UPDATED,
|
||||
] as const,
|
||||
"routing-forms": [WebhookTriggerEvents.FORM_SUBMITTED] as const,
|
||||
};
|
||||
|
||||
@@ -29,7 +29,14 @@ export type WithUTCOffsetType<T> = T & {
|
||||
attendees?: (Person & UTCOffset)[];
|
||||
};
|
||||
|
||||
export type BookingNoShowUpdatedPayload = {
|
||||
message: string;
|
||||
bookingUid: string;
|
||||
attendees: { email: string; noShow: boolean }[];
|
||||
};
|
||||
|
||||
export type WebhookDataType = CalendarEvent &
|
||||
// BookingNoShowUpdatedPayload & // This breaks all other webhooks
|
||||
EventTypeInfo & {
|
||||
metadata?: { [key: string]: string | number | boolean | null };
|
||||
bookingId?: number;
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
|
||||
|
||||
import { WebhookService } from "../WebhookService";
|
||||
import getWebhooks from "../getWebhooks";
|
||||
|
||||
vi.mock("../getWebhooks");
|
||||
vi.mock("../sendOrSchedulePayload");
|
||||
|
||||
vi.mock("@calcom/lib/logger", async () => {
|
||||
const actual = await vi.importActual<typeof import("@calcom/lib/logger")>("@calcom/lib/logger");
|
||||
return {
|
||||
...actual,
|
||||
getSubLogger: vi.fn(() => ({
|
||||
error: vi.fn(),
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@calcom/lib/safeStringify", () => ({
|
||||
safeStringify: JSON.stringify,
|
||||
}));
|
||||
|
||||
describe("WebhookService", () => {
|
||||
const mockOptions = {
|
||||
id: "mockOptionsId",
|
||||
subscriberUrl: "subUrl",
|
||||
payloadTemplate: "PayloadTemplate",
|
||||
appId: "AppId",
|
||||
secret: "WhSecret",
|
||||
triggerEvent: WebhookTriggerEvents.BOOKING_CREATED,
|
||||
};
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it("should initialize with options and webhooks", async () => {
|
||||
const mockWebhooks = [
|
||||
{
|
||||
id: "webhookId",
|
||||
subscriberUrl: "url",
|
||||
secret: "secret",
|
||||
appId: "appId",
|
||||
payloadTemplate: "payloadTemplate",
|
||||
},
|
||||
];
|
||||
vi.mocked(getWebhooks).mockResolvedValue(mockWebhooks);
|
||||
|
||||
// Has to be called with await due to the iffi being async
|
||||
const service = await new WebhookService(mockOptions);
|
||||
|
||||
expect(service).toBeInstanceOf(WebhookService);
|
||||
expect(await service.getWebhooks()).toEqual(mockWebhooks);
|
||||
expect(getWebhooks).toHaveBeenCalledWith(mockOptions);
|
||||
});
|
||||
|
||||
// it("should send payload to all webhooks", async () => {
|
||||
// const mockWebhooks = [
|
||||
// {
|
||||
// id: "webhookId",
|
||||
// subscriberUrl: "url",
|
||||
// secret: "secret",
|
||||
// appId: "appId",
|
||||
// payloadTemplate: "payloadTemplate",
|
||||
// },
|
||||
// {
|
||||
// id: "webhookId2",
|
||||
// subscriberUrl: "url",
|
||||
// secret: "secret2",
|
||||
// appId: "appId2",
|
||||
// payloadTemplate: "payloadTemplate",
|
||||
// },
|
||||
// ];
|
||||
// vi.mocked(getWebhooks).mockResolvedValue(mockWebhooks);
|
||||
// const service = await new WebhookService(mockOptions);
|
||||
//
|
||||
// const payload = {
|
||||
// secretKey: "secret",
|
||||
// triggerEvent: "triggerEvent",
|
||||
// createdAt: "now",
|
||||
// webhook: {
|
||||
// subscriberUrl: "url",
|
||||
// appId: "appId",
|
||||
// payloadTemplate: "payloadTemplate",
|
||||
// },
|
||||
// data: "test",
|
||||
// };
|
||||
//
|
||||
// await service.sendPayload(payload as any);
|
||||
//
|
||||
// expect(sendOrSchedulePayload).toHaveBeenCalledTimes(mockWebhooks.length);
|
||||
//
|
||||
// mockWebhooks.forEach((webhook) => {
|
||||
// expect(sendOrSchedulePayload).toHaveBeenCalledWith(
|
||||
// webhook.secret,
|
||||
// mockOptions.triggerEvent,
|
||||
// expect.any(String),
|
||||
// webhook,
|
||||
// payload
|
||||
// );
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// it("should log error when sending payload fails", async () => {
|
||||
// const mockWebhooks = [
|
||||
// {
|
||||
// id: "webhookId",
|
||||
// subscriberUrl: "url",
|
||||
// secret: "secret",
|
||||
// appId: "appId",
|
||||
// payloadTemplate: "payloadTemplate",
|
||||
// },
|
||||
// ];
|
||||
// vi.mocked(getWebhooks).mockResolvedValue(mockWebhooks);
|
||||
//
|
||||
// const logError = vi.fn();
|
||||
//
|
||||
// (sendOrSchedulePayload as any).mockImplementation(() => {
|
||||
// throw new Error("Failure");
|
||||
// });
|
||||
//
|
||||
// const service = new WebhookService(mockOptions);
|
||||
//
|
||||
// const payload = {
|
||||
// secretKey: "secret", triggerEvent: "triggerEvent", createdAt: "now", webhook: {
|
||||
// subscriberUrl: "url",
|
||||
// appId: "appId",
|
||||
// payloadTemplate: "payloadTemplate"
|
||||
// },
|
||||
// data: "test"
|
||||
// };
|
||||
//
|
||||
// await service.sendPayload(payload as any);
|
||||
//
|
||||
// expect(logError).toHaveBeenCalledWith(
|
||||
// `Error executing webhook for event: ${mockOptions.triggerEvent}, URL: ${mockWebhooks[0].subscriberUrl}`,
|
||||
// JSON.stringify(new Error("Failure"))
|
||||
// );
|
||||
// });
|
||||
});
|
||||
Reference in new issue
Block a user