fix: revert assignmentReason breaking change in webhook payloads (#27891)

* fix: revert assignmentReason breaking change in webhook payloads

Remove the new { category, details } format from EventPayloadType to
maintain backward compatibility for webhook consumers. The new format
is stripped at all webhook payload construction sites by destructuring
assignmentReason out of CalendarEvent before spreading into the payload.

A sanitizeAssignmentReasonForWebhook function provides an additional
safety net in sendPayload itself. Emails and booking single view
continue to use the new format via CalendarEvent.

Co-Authored-By: [email protected] <[email protected]>

* fix: strip assignmentReason from handlePaymentSuccess webhook payload

Co-Authored-By: [email protected] <[email protected]>

* fix: strip assignmentReason from triggerWebhooks and handleSeats webhook payloads

Co-Authored-By: [email protected] <[email protected]>

* refactor: use zod safeParse instead of type assertion in sanitizeAssignmentReasonForWebhook

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-02-16 21:55:10 -03:00
1 parent 21d28c9747
commit 3a7122d613
11 files changed
+225 -67

No files matched your search

@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { WebhookVersion } from "./interface/IWebhookRepository";
import sendPayload from "./sendPayload";
import type { EventPayloadType } from "./sendPayload";
import sendPayload, { sanitizeAssignmentReasonForWebhook } from "./sendPayload";
describe("sendPayload", () => {
const mockFetch = vi.fn();
@@ -108,4 +108,138 @@ describe("sendPayload", () => {
expect(options.headers["X-Cal-Webhook-Version"]).toBe("2021-10-20");
});
});
describe("sanitizeAssignmentReasonForWebhook", () => {
const basePayload = {
title: "Test Booking",
startTime: "2024-01-01T10:00:00Z",
endTime: "2024-01-01T11:00:00Z",
organizer: {
email: "[email protected]",
name: "Organizer",
timeZone: "UTC",
language: { locale: "en" },
},
attendees: [],
type: "test-event",
} as unknown as EventPayloadType;
it("should preserve string assignmentReason", () => {
const data = { ...basePayload, assignmentReason: "Salesforce contact owner: [email protected]" };
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toBe("Salesforce contact owner: [email protected]");
});
it("should preserve array assignmentReason with reasonEnum and reasonString", () => {
const reasons = [{ reasonEnum: "ROUTED", reasonString: "Language: English" }];
const data = { ...basePayload, assignmentReason: reasons };
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toEqual(reasons);
});
it("should preserve null assignmentReason", () => {
const data = { ...basePayload, assignmentReason: null };
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toBeNull();
});
it("should preserve undefined assignmentReason", () => {
const data = { ...basePayload };
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toBeUndefined();
});
it("should strip CalendarEvent email format { category, details }", () => {
const data = {
...basePayload,
assignmentReason: { category: "routed", details: "Language: English" },
} as unknown as EventPayloadType;
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toBeUndefined();
});
it("should strip CalendarEvent email format with null details", () => {
const data = {
...basePayload,
assignmentReason: { category: "reassigned", details: null },
} as unknown as EventPayloadType;
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toBeUndefined();
});
it("should not strip other fields when stripping assignmentReason", () => {
const data = {
...basePayload,
bookingId: 123,
status: "ACCEPTED",
assignmentReason: { category: "routed", details: "test" },
} as unknown as EventPayloadType;
const result = sanitizeAssignmentReasonForWebhook(data);
expect(result.assignmentReason).toBeUndefined();
expect(result.bookingId).toBe(123);
expect(result.status).toBe("ACCEPTED");
expect(result.title).toBe("Test Booking");
});
});
describe("sendPayload strips CalendarEvent assignmentReason format", () => {
it("should not include { category, details } format in webhook payload", async () => {
const webhook = {
subscriberUrl: "https://example.com/webhook",
appId: null,
payloadTemplate: null,
version: WebhookVersion.V_2021_10_20,
};
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
title: "Test Booking",
startTime: "2024-01-01T10:00:00Z",
endTime: "2024-01-01T11:00:00Z",
organizer: {
email: "[email protected]",
name: "Organizer",
timeZone: "UTC",
language: { locale: "en" },
},
attendees: [],
type: "test-event",
description: "",
assignmentReason: { category: "routed", details: "Language: English" },
} as unknown as Parameters<typeof sendPayload>[4]);
const [, options] = mockFetch.mock.calls[0];
const body = JSON.parse(options.body);
expect(body.payload.assignmentReason).toBeUndefined();
});
it("should preserve array assignmentReason format in webhook payload", async () => {
const webhook = {
subscriberUrl: "https://example.com/webhook",
appId: null,
payloadTemplate: null,
version: WebhookVersion.V_2021_10_20,
};
const reasons = [{ reasonEnum: "ROUTED", reasonString: "Language: English" }];
await sendPayload("test-secret", "BOOKING_CREATED", new Date().toISOString(), webhook, {
title: "Test Booking",
startTime: "2024-01-01T10:00:00Z",
endTime: "2024-01-01T11:00:00Z",
organizer: {
email: "[email protected]",
name: "Organizer",
timeZone: "UTC",
language: { locale: "en" },
},
attendees: [],
type: "test-event",
description: "",
assignmentReason: reasons,
} as unknown as Parameters<typeof sendPayload>[4]);
const [, options] = mockFetch.mock.calls[0];
const body = JSON.parse(options.body);
expect(body.payload.assignmentReason).toEqual(reasons);
});
});
});
+22 -9
View File
@@ -1,12 +1,15 @@
import { createHmac } from "node:crypto";
import { compile } from "handlebars";
import type { TGetTranscriptAccessLink } from "@calcom/app-store/dailyvideo/zod";
import { getHumanReadableLocationValue } from "@calcom/app-store/locations";
import type { WebhookSubscriber, PaymentData } from "@calcom/features/webhooks/lib/dto/types";
import { DelegationCredentialErrorPayloadType } from "@calcom/features/webhooks/lib/dto/types";
import type {
DelegationCredentialErrorPayloadType,
PaymentData,
WebhookSubscriber,
} from "@calcom/features/webhooks/lib/dto/types";
import { getUTCOffsetByTimezone } from "@calcom/lib/dayjs";
import type { CalendarEvent, Person } from "@calcom/types/Calendar";
import { compile } from "handlebars";
import { z } from "zod";
// Minimal webhook shape for sending payloads (subset of WebhookSubscriber)
type WebhookForPayload = Pick<WebhookSubscriber, "subscriberUrl" | "appId" | "payloadTemplate" | "version">;
@@ -97,11 +100,7 @@ export type EventPayloadType = Omit<CalendarEvent, "assignmentReason"> &
cancelledBy?: string;
paymentData?: PaymentData;
requestReschedule?: boolean;
assignmentReason?:
| string
| { reasonEnum: string; reasonString: string }[]
| { category: string; details?: string | null }
| null;
assignmentReason?: string | { reasonEnum: string; reasonString: string }[] | null;
};
export type WebhookPayloadType =
@@ -222,6 +221,19 @@ export function isEventPayload(data: WebhookPayloadType): data is EventPayloadTy
return !isNoShowPayload(data) && !isOOOEntryPayload(data) && !isDelegationCredentialErrorPayload(data);
}
const webhookAssignmentReasonSchema = z.union([
z.string(),
z.array(z.object({ reasonEnum: z.string(), reasonString: z.string() })),
z.null(),
z.undefined(),
]);
export function sanitizeAssignmentReasonForWebhook(data: EventPayloadType): EventPayloadType {
const result = webhookAssignmentReasonSchema.safeParse(data.assignmentReason);
if (result.success) return data;
return { ...data, assignmentReason: undefined };
}
const sendPayload = async (
secretKey: string | null,
triggerEvent: string,
@@ -239,6 +251,7 @@ const sendPayload = async (
let body;
/* Zapier id is hardcoded in the DB, we send the raw data for this case */
if (isEventPayload(data)) {
data = sanitizeAssignmentReasonForWebhook(data);
data.description = data.description || data.additionalNotes;
if (appId === "zapier") {
body = getZapierPayload({ ...data, createdAt });