Files
calendar/packages/trpc/server/routers/viewer/bookings/reportWrongAssignment.handler.test.ts
Benny JooGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
ab21c7f805 refactor: Cal.diy (#28903)
* feat: Cal.diy — community-driven MIT-licensed fork of Cal.com

This squashed commit contains all Cal.diy changes applied on top of calcom/cal.com main:

- Rebrand Cal.com to Cal.diy across the entire codebase
- Remove Enterprise Edition (EE) features, license checks, and AGPL restrictions
- Switch license from AGPL-3.0 to MIT
- Remove docs/ directory (migrated to Nextra at cal.diy)
- Remove dead code: org tests, EE tips, platform nav, premium username, SAML/SSO, etc.
- Clean up .env.example for self-hosted Cal.diy
- Update Docker image references to calcom/cal.diy
- Update README, CONTRIBUTING.md, and issue templates for Cal.diy community fork
- Add PR welcome bot for Cal.diy contributors
- Fix API v2 breaking changes oasdiff ignore entries
- Replace Blacksmith CI runners with default GitHub Actions

3893 files changed, 20789 insertions(+), 411020 deletions(-)

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

* refactor: remove org-specific /organizations/:orgId endpoints from API v2 atoms controllers (#1701)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: revert Cal.diy Inc to Cal.com, Inc. in license files, copyright notices, and package metadata (#1702)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* rip out org related comments in api v2

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-04-15 09:52:36 -03:00

533 lines
17 KiB
TypeScript

import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import { sendGenericWebhookPayload } from "@calcom/features/webhooks/lib/sendPayload";
import { ErrorWithCode } from "@calcom/lib/errors";
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { reportWrongAssignmentHandler } from "./reportWrongAssignment.handler";
const mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason = vi.fn();
const mockDoesUserIdHaveAccessToBooking = vi.fn();
const mockExistsByBookingUid = vi.fn();
const mockCreateReport = vi.fn();
vi.mock("@calcom/features/bookings/repositories/BookingRepository", () => {
return {
BookingRepository: class MockBookingRepository {
findByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason =
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason;
},
};
});
vi.mock("@calcom/features/bookings/repositories/WrongAssignmentReportRepository", () => {
return {
WrongAssignmentReportRepository: class MockWrongAssignmentReportRepository {
existsByBookingUid = mockExistsByBookingUid;
createReport = mockCreateReport;
},
};
});
vi.mock("@calcom/features/bookings/services/BookingAccessService", () => {
return {
BookingAccessService: class MockBookingAccessService {
doesUserIdHaveAccessToBooking = mockDoesUserIdHaveAccessToBooking;
},
};
});
vi.mock("@calcom/features/webhooks/lib/getWebhooks");
vi.mock("@calcom/features/webhooks/lib/sendPayload");
vi.mock("@calcom/prisma", () => ({
default: {},
}));
vi.mock("@calcom/i18n/server", () => ({
getTranslation: vi.fn().mockResolvedValue((key: string) => {
const translations: Record<string, string> = {
wrong_assignment_already_reported:
"A wrong assignment report has already been submitted for this booking",
wrong_assignment_reported: "Wrong assignment reported successfully",
};
return translations[key] || key;
}),
}));
vi.mock("@calcom/lib/logger", () => ({
default: {
getSubLogger: () => ({
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
},
}));
describe("reportWrongAssignmentHandler", () => {
const mockUser = {
id: 1,
email: "[email protected]",
name: "Reporter User",
};
const mockInput = {
bookingUid: "test-booking-uid",
additionalNotes: "This booking was assigned to me incorrectly",
};
const mockBooking = {
id: 100,
uid: "test-booking-uid",
title: "Test Booking",
startTime: new Date("2025-12-01T10:00:00Z"),
endTime: new Date("2025-12-01T11:00:00Z"),
status: BookingStatus.ACCEPTED,
userId: 2,
user: {
id: 2,
email: "[email protected]",
name: "Host User",
},
eventType: {
id: 10,
title: "Test Event",
slug: "test-event",
team: {
id: 5,
parentId: 1,
},
},
attendees: [{ email: "[email protected]" }],
assignmentReason: [{ reasonString: "Matched by round-robin" }],
};
beforeEach(() => {
vi.clearAllMocks();
mockExistsByBookingUid.mockResolvedValue(false);
mockCreateReport.mockResolvedValue({ id: "report-uuid-123" });
vi.mocked(getWebhooks).mockResolvedValue([]);
vi.mocked(sendGenericWebhookPayload).mockResolvedValue({ ok: true, status: 200 });
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
});
describe("access control", () => {
it("should throw FORBIDDEN when BookingAccessService denies access", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(false);
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
})
).rejects.toThrow(TRPCError);
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
})
).rejects.toMatchObject({
code: "FORBIDDEN",
message: "You don't have access to this booking",
});
});
it("should throw NOT_FOUND when booking doesn't exist", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(null);
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
})
).rejects.toThrow(ErrorWithCode);
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
})
).rejects.toMatchObject({
message: "Booking not found",
});
});
it("should allow access when BookingAccessService grants access", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
});
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(result.success).toBe(true);
expect(mockDoesUserIdHaveAccessToBooking).toHaveBeenCalledWith({
userId: mockUser.id,
bookingUid: mockInput.bookingUid,
});
});
it("should throw BAD_REQUEST when a report already exists for the booking", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
mockExistsByBookingUid.mockResolvedValue(true);
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
additionalNotes: "Duplicate report",
},
})
).rejects.toThrow(ErrorWithCode);
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
additionalNotes: "Duplicate report",
},
})
).rejects.toMatchObject({
message: "A wrong assignment report has already been submitted for this booking",
});
expect(mockCreateReport).not.toHaveBeenCalled();
});
});
describe("successful report", () => {
it("should successfully report with all fields", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
});
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
...mockInput,
correctAssignee: "[email protected]",
additionalNotes: "This booking should have gone to the sales team",
},
});
expect(result.success).toBe(true);
expect(result.message).toBe("Wrong assignment reported successfully");
});
it("should successfully report with only required fields (no correctAssignee)", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
});
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
...mockInput,
additionalNotes: "Wrong person received this booking",
},
});
expect(result.success).toBe(true);
expect(result.message).toBe("Wrong assignment reported successfully");
});
});
describe("webhook integration", () => {
it("should send webhook with correct payload structure", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
});
vi.mocked(getWebhooks).mockResolvedValue([
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
] as never);
await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
...mockInput,
correctAssignee: "[email protected]",
additionalNotes: "Wrong assignment notes",
},
});
expect(getWebhooks).toHaveBeenCalledWith({
userId: mockUser.id,
teamId: mockBooking.eventType.team.id,
orgId: mockBooking.eventType.team.parentId,
triggerEvent: WebhookTriggerEvents.WRONG_ASSIGNMENT_REPORT,
});
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
expect.objectContaining({
triggerEvent: WebhookTriggerEvents.WRONG_ASSIGNMENT_REPORT,
data: expect.objectContaining({
booking: expect.objectContaining({
uid: mockBooking.uid,
id: mockBooking.id,
title: mockBooking.title,
}),
report: expect.objectContaining({
reportedBy: expect.objectContaining({
id: mockUser.id,
email: mockUser.email,
}),
firstAssignmentReason: mockBooking.assignmentReason[0].reasonString,
guest: mockBooking.attendees[0].email,
host: expect.objectContaining({
email: mockBooking.user.email,
}),
correctAssignee: "[email protected]",
additionalNotes: "Wrong assignment notes",
}),
}),
})
);
});
it("should handle webhook failures gracefully", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
});
vi.mocked(getWebhooks).mockResolvedValue([
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
] as never);
vi.mocked(sendGenericWebhookPayload).mockRejectedValue(new Error("Webhook delivery failed"));
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(result.success).toBe(true);
expect(result.message).toBe("Wrong assignment reported successfully");
});
it("should not fail when getWebhooks throws", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
});
vi.mocked(getWebhooks).mockRejectedValue(new Error("Database error"));
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(result.success).toBe(true);
});
});
describe("database persistence", () => {
it("should persist report to database with all fields", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
correctAssignee: "[email protected]",
additionalNotes: "This booking should have gone to the sales team",
},
});
expect(mockCreateReport).toHaveBeenCalledWith({
bookingUid: "test-booking-uid",
reportedById: mockUser.id,
correctAssignee: "[email protected]",
additionalNotes: "This booking should have gone to the sales team",
teamId: mockBooking.eventType.team.id,
});
});
it("should persist report with null optional fields", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
eventType: null,
});
await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
additionalNotes: "Wrong person",
},
});
expect(mockCreateReport).toHaveBeenCalledWith({
bookingUid: "test-booking-uid",
reportedById: mockUser.id,
correctAssignee: null,
additionalNotes: "Wrong person",
teamId: null,
});
});
it("should return reportId in response", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
additionalNotes: "Notes",
},
});
expect(result.reportId).toBe("report-uuid-123");
});
it("should throw if database write fails", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
mockCreateReport.mockRejectedValue(new Error("Database error"));
await expect(
reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
additionalNotes: "Notes",
},
})
).rejects.toThrow("Database error");
});
it("should persist report even when webhooks fail", async () => {
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
vi.mocked(getWebhooks).mockRejectedValue(new Error("Webhook service down"));
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: {
bookingUid: "test-booking-uid",
additionalNotes: "Notes",
},
});
expect(mockCreateReport).toHaveBeenCalled();
expect(result.success).toBe(true);
});
});
describe("edge cases", () => {
it("should handle booking without assignmentReason", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
assignmentReason: [],
});
vi.mocked(getWebhooks).mockResolvedValue([
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
] as never);
await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
report: expect.objectContaining({
firstAssignmentReason: null,
}),
}),
})
);
});
it("should handle booking without user", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: null,
user: null,
attendees: [{ email: mockUser.email }],
});
const result = await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(result.success).toBe(true);
expect(getWebhooks).toHaveBeenCalledWith(
expect.objectContaining({
userId: null,
})
);
});
it("should handle booking without eventType", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
eventType: null,
});
vi.mocked(getWebhooks).mockResolvedValue([
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
] as never);
await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(getWebhooks).toHaveBeenCalledWith(
expect.objectContaining({
teamId: null,
orgId: null,
})
);
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
booking: expect.objectContaining({
eventType: null,
}),
}),
})
);
});
it("should handle booking without attendees", async () => {
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
...mockBooking,
userId: mockUser.id,
attendees: [],
});
vi.mocked(getWebhooks).mockResolvedValue([
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
] as never);
await reportWrongAssignmentHandler({
ctx: { user: mockUser },
input: mockInput,
});
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
report: expect.objectContaining({
guest: null,
}),
}),
})
);
});
});
});