fix: Improve wrong assignment report (#27162)
* Pass already fetched booking * Fetch the assignment reason * Update tests * Label as first assignment reason * Pass single booking object * Remove unused repository method * Only pass bookingUid to handler * Rename booking `assignmentReason` to `assignmentReasonSortedByCreatedAt` * fix: use renamed assignmentReasonSortedByCreatedAt property Co-Authored-By: [email protected] <[email protected]> * Remove take statement * fix: address Udit's review comments - Restore BookingAccessService for access control checks - Rename repository method from findByUidForWrongAssignmentReport to findByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason Co-Authored-By: [email protected] <[email protected]> * test: update tests to use BookingAccessService mock Co-Authored-By: [email protected] <[email protected]> * fix: use latest assignment reason in BookingDetailsSheet for consistency 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:
11 files changed
+277
-232
No files matched your search
@@ -2123,54 +2123,6 @@ export class BookingRepository implements IBookingRepository {
|
||||
});
|
||||
}
|
||||
|
||||
async findByUidIncludeEventTypeAndTeamAndAssignmentReason({ bookingUid }: { bookingUid: string }) {
|
||||
return await this.prismaClient.booking.findUnique({
|
||||
where: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
uid: true,
|
||||
title: true,
|
||||
startTime: true,
|
||||
endTime: true,
|
||||
status: true,
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
attendees: {
|
||||
select: {
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
eventType: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
slug: true,
|
||||
teamId: true,
|
||||
},
|
||||
},
|
||||
assignmentReason: {
|
||||
select: {
|
||||
reasonString: true,
|
||||
reasonEnum: true,
|
||||
},
|
||||
},
|
||||
routedFromRoutingFormReponse: {
|
||||
select: {
|
||||
formId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async updateRecordedStatus({
|
||||
bookingUid,
|
||||
isRecorded,
|
||||
@@ -2183,4 +2135,58 @@ export class BookingRepository implements IBookingRepository {
|
||||
data: { isRecorded },
|
||||
});
|
||||
}
|
||||
|
||||
async findByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason({ bookingUid }: { bookingUid: string }) {
|
||||
return await this.prismaClient.booking.findUnique({
|
||||
where: { uid: bookingUid },
|
||||
select: {
|
||||
id: true,
|
||||
uid: true,
|
||||
title: true,
|
||||
startTime: true,
|
||||
endTime: true,
|
||||
status: true,
|
||||
userId: true,
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
eventType: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
slug: true,
|
||||
team: {
|
||||
select: {
|
||||
id: true,
|
||||
parentId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
attendees: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
assignmentReason: {
|
||||
select: {
|
||||
reasonString: true,
|
||||
},
|
||||
take: 1,
|
||||
orderBy: {
|
||||
createdAt: "asc" as const,
|
||||
},
|
||||
},
|
||||
routedFromRoutingFormReponse: {
|
||||
select: {
|
||||
formId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -631,9 +631,8 @@ export async function getBookings({
|
||||
.selectFrom("AssignmentReason")
|
||||
.selectAll()
|
||||
.whereRef("AssignmentReason.bookingId", "=", "Booking.id")
|
||||
.orderBy("AssignmentReason.createdAt", "desc")
|
||||
.limit(1)
|
||||
).as("assignmentReason"),
|
||||
.orderBy("AssignmentReason.createdAt", "asc")
|
||||
).as("assignmentReasonSortedByCreatedAt"),
|
||||
jsonObjectFrom(
|
||||
eb
|
||||
.selectFrom("BookingReport")
|
||||
|
||||
+145
-121
@@ -1,6 +1,3 @@
|
||||
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
|
||||
import { WrongAssignmentReportRepository } from "@calcom/features/bookings/repositories/WrongAssignmentReportRepository";
|
||||
import { BookingAccessService } from "@calcom/features/bookings/services/BookingAccessService";
|
||||
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
|
||||
import { sendGenericWebhookPayload } from "@calcom/features/webhooks/lib/sendPayload";
|
||||
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
|
||||
@@ -8,12 +5,39 @@ import { TRPCError } from "@trpc/server";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { reportWrongAssignmentHandler } from "./reportWrongAssignment.handler";
|
||||
|
||||
vi.mock("@calcom/features/bookings/repositories/BookingRepository");
|
||||
vi.mock("@calcom/features/bookings/repositories/WrongAssignmentReportRepository");
|
||||
vi.mock("@calcom/features/bookings/services/BookingAccessService");
|
||||
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/prisma", () => ({
|
||||
default: {},
|
||||
}));
|
||||
vi.mock("@calcom/lib/server/i18n", () => ({
|
||||
getTranslation: vi.fn().mockResolvedValue((key: string) => {
|
||||
const translations: Record<string, string> = {
|
||||
@@ -39,6 +63,11 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
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",
|
||||
@@ -46,74 +75,51 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
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",
|
||||
},
|
||||
attendees: [{ email: "[email protected]" }],
|
||||
eventType: {
|
||||
id: 10,
|
||||
title: "Test Event",
|
||||
slug: "test-event",
|
||||
teamId: 5,
|
||||
team: {
|
||||
id: 5,
|
||||
parentId: 1,
|
||||
},
|
||||
},
|
||||
attendees: [{ email: "[email protected]" }],
|
||||
assignmentReason: [{ reasonString: "Matched by round-robin" }],
|
||||
routedFromRoutingFormReponse: null,
|
||||
};
|
||||
|
||||
const mockBookingRepo = {
|
||||
findByUidIncludeEventTypeAndTeamAndAssignmentReason: vi.fn(),
|
||||
};
|
||||
|
||||
const mockBookingAccessService = {
|
||||
doesUserIdHaveAccessToBooking: vi.fn(),
|
||||
};
|
||||
|
||||
const mockWrongAssignmentReportRepo = {
|
||||
existsByBookingUid: vi.fn().mockResolvedValue(false),
|
||||
createReport: vi.fn().mockResolvedValue({ id: "report-uuid-123" }),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockWrongAssignmentReportRepo.existsByBookingUid.mockResolvedValue(false);
|
||||
mockWrongAssignmentReportRepo.createReport.mockResolvedValue({ id: "report-uuid-123" });
|
||||
|
||||
vi.mocked(BookingRepository).mockImplementation(function () {
|
||||
return mockBookingRepo;
|
||||
});
|
||||
vi.mocked(WrongAssignmentReportRepository).mockImplementation(function () {
|
||||
return mockWrongAssignmentReportRepo;
|
||||
});
|
||||
vi.mocked(BookingAccessService).mockImplementation(function () {
|
||||
return mockBookingAccessService;
|
||||
});
|
||||
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 user doesn't have access to booking", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(false);
|
||||
it("should throw FORBIDDEN when BookingAccessService denies access", async () => {
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(false);
|
||||
|
||||
await expect(
|
||||
reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "This booking was assigned to me incorrectly",
|
||||
},
|
||||
input: mockInput,
|
||||
})
|
||||
).rejects.toThrow(TRPCError);
|
||||
|
||||
await expect(
|
||||
reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "This booking was assigned to me incorrectly",
|
||||
},
|
||||
input: mockInput,
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: "FORBIDDEN",
|
||||
@@ -122,16 +128,20 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should throw NOT_FOUND when booking doesn't exist", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(null);
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "This booking was assigned to me incorrectly",
|
||||
},
|
||||
input: mockInput,
|
||||
})
|
||||
).rejects.toThrow(TRPCError);
|
||||
|
||||
await expect(
|
||||
reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: mockInput,
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: "NOT_FOUND",
|
||||
@@ -139,10 +149,29 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
});
|
||||
|
||||
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 () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockWrongAssignmentReportRepo.existsByBookingUid.mockResolvedValue(true);
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockExistsByBookingUid.mockResolvedValue(true);
|
||||
|
||||
await expect(
|
||||
reportWrongAssignmentHandler({
|
||||
@@ -157,19 +186,21 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
message: "A wrong assignment report has already been submitted for this booking",
|
||||
});
|
||||
|
||||
expect(mockWrongAssignmentReportRepo.createReport).not.toHaveBeenCalled();
|
||||
expect(mockCreateReport).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("successful report", () => {
|
||||
it("should successfully report with all fields", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
});
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
...mockInput,
|
||||
correctAssignee: "[email protected]",
|
||||
additionalNotes: "This booking should have gone to the sales team",
|
||||
},
|
||||
@@ -180,13 +211,15 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should successfully report with only required fields (no correctAssignee)", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
});
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
...mockInput,
|
||||
additionalNotes: "Wrong person received this booking",
|
||||
},
|
||||
});
|
||||
@@ -198,8 +231,10 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
|
||||
describe("webhook integration", () => {
|
||||
it("should send webhook with correct payload structure", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
});
|
||||
vi.mocked(getWebhooks).mockResolvedValue([
|
||||
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
|
||||
] as never);
|
||||
@@ -207,15 +242,16 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
...mockInput,
|
||||
correctAssignee: "[email protected]",
|
||||
additionalNotes: "Wrong assignment notes",
|
||||
},
|
||||
});
|
||||
|
||||
expect(getWebhooks).toHaveBeenCalledWith({
|
||||
userId: mockBooking.user.id,
|
||||
teamId: mockBooking.eventType.teamId,
|
||||
userId: mockUser.id,
|
||||
teamId: mockBooking.eventType.team.id,
|
||||
orgId: mockBooking.eventType.team.parentId,
|
||||
triggerEvent: WebhookTriggerEvents.WRONG_ASSIGNMENT_REPORT,
|
||||
});
|
||||
|
||||
@@ -233,10 +269,10 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
id: mockUser.id,
|
||||
email: mockUser.email,
|
||||
}),
|
||||
routingReason: "Matched by round-robin",
|
||||
guest: "[email protected]",
|
||||
firstAssignmentReason: mockBooking.assignmentReason[0].reasonString,
|
||||
guest: mockBooking.attendees[0].email,
|
||||
host: expect.objectContaining({
|
||||
email: "[email protected]",
|
||||
email: mockBooking.user.email,
|
||||
}),
|
||||
correctAssignee: "[email protected]",
|
||||
additionalNotes: "Wrong assignment notes",
|
||||
@@ -247,8 +283,10 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should handle webhook failures gracefully", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
});
|
||||
vi.mocked(getWebhooks).mockResolvedValue([
|
||||
{ id: "webhook-1", subscriberUrl: "https://example.com/webhook", secret: "secret123" },
|
||||
] as never);
|
||||
@@ -256,10 +294,7 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "Notes",
|
||||
},
|
||||
input: mockInput,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
@@ -267,16 +302,15 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should not fail when getWebhooks throws", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
});
|
||||
vi.mocked(getWebhooks).mockRejectedValue(new Error("Database error"));
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "Notes",
|
||||
},
|
||||
input: mockInput,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
@@ -285,8 +319,8 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
|
||||
describe("database persistence", () => {
|
||||
it("should persist report to database with all fields", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
|
||||
await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
@@ -297,19 +331,19 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockWrongAssignmentReportRepo.createReport).toHaveBeenCalledWith({
|
||||
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.teamId,
|
||||
teamId: mockBooking.eventType.team.id,
|
||||
routingFormId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("should persist report with null optional fields", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue({
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
eventType: null,
|
||||
});
|
||||
@@ -322,7 +356,7 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockWrongAssignmentReportRepo.createReport).toHaveBeenCalledWith({
|
||||
expect(mockCreateReport).toHaveBeenCalledWith({
|
||||
bookingUid: "test-booking-uid",
|
||||
reportedById: mockUser.id,
|
||||
correctAssignee: null,
|
||||
@@ -333,8 +367,8 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should return reportId in response", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
@@ -348,9 +382,9 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should throw if database write fails", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockWrongAssignmentReportRepo.createReport.mockRejectedValue(new Error("Database error"));
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockCreateReport.mockRejectedValue(new Error("Database error"));
|
||||
|
||||
await expect(
|
||||
reportWrongAssignmentHandler({
|
||||
@@ -364,8 +398,8 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should persist report even when webhooks fail", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
mockDoesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue(mockBooking);
|
||||
vi.mocked(getWebhooks).mockRejectedValue(new Error("Webhook service down"));
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
@@ -376,16 +410,16 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockWrongAssignmentReportRepo.createReport).toHaveBeenCalled();
|
||||
expect(mockCreateReport).toHaveBeenCalled();
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("edge cases", () => {
|
||||
it("should handle booking without assignmentReason", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue({
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
assignmentReason: [],
|
||||
});
|
||||
vi.mocked(getWebhooks).mockResolvedValue([
|
||||
@@ -394,17 +428,14 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
|
||||
await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "Notes",
|
||||
},
|
||||
input: mockInput,
|
||||
});
|
||||
|
||||
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
report: expect.objectContaining({
|
||||
routingReason: null,
|
||||
firstAssignmentReason: null,
|
||||
}),
|
||||
}),
|
||||
})
|
||||
@@ -412,18 +443,16 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should handle booking without user", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue({
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: null,
|
||||
user: null,
|
||||
attendees: [{ email: mockUser.email }],
|
||||
});
|
||||
|
||||
const result = await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "Notes",
|
||||
},
|
||||
input: mockInput,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
@@ -435,9 +464,9 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should handle booking without eventType", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue({
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
eventType: null,
|
||||
});
|
||||
vi.mocked(getWebhooks).mockResolvedValue([
|
||||
@@ -446,15 +475,13 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
|
||||
await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "Notes",
|
||||
},
|
||||
input: mockInput,
|
||||
});
|
||||
|
||||
expect(getWebhooks).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
teamId: null,
|
||||
orgId: null,
|
||||
})
|
||||
);
|
||||
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
|
||||
@@ -469,9 +496,9 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
});
|
||||
|
||||
it("should handle booking without attendees", async () => {
|
||||
mockBookingAccessService.doesUserIdHaveAccessToBooking.mockResolvedValue(true);
|
||||
mockBookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason.mockResolvedValue({
|
||||
mockFindByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason.mockResolvedValue({
|
||||
...mockBooking,
|
||||
userId: mockUser.id,
|
||||
attendees: [],
|
||||
});
|
||||
vi.mocked(getWebhooks).mockResolvedValue([
|
||||
@@ -480,17 +507,14 @@ describe("reportWrongAssignmentHandler", () => {
|
||||
|
||||
await reportWrongAssignmentHandler({
|
||||
ctx: { user: mockUser },
|
||||
input: {
|
||||
bookingUid: "test-booking-uid",
|
||||
additionalNotes: "Notes",
|
||||
},
|
||||
input: mockInput,
|
||||
});
|
||||
|
||||
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
report: expect.objectContaining({
|
||||
guest: "",
|
||||
guest: null,
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -28,7 +28,7 @@ export const reportWrongAssignmentHandler = async ({ ctx, input }: ReportWrongAs
|
||||
const { bookingUid, correctAssignee, additionalNotes } = input;
|
||||
|
||||
const t = await getTranslation(user.locale ?? "en", "common");
|
||||
const bookingRepo = new BookingRepository(prisma);
|
||||
const bookingRepository = new BookingRepository(prisma);
|
||||
const wrongAssignmentReportRepo = new WrongAssignmentReportRepository(prisma);
|
||||
const bookingAccessService = new BookingAccessService(prisma);
|
||||
|
||||
@@ -41,19 +41,16 @@ export const reportWrongAssignmentHandler = async ({ ctx, input }: ReportWrongAs
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "You don't have access to this booking" });
|
||||
}
|
||||
|
||||
const booking = await bookingRepo.findByUidIncludeEventTypeAndTeamAndAssignmentReason({ bookingUid });
|
||||
const booking = await bookingRepository.findByUidIncludeUserAndEventTypeTeamAndAttendeesAndAssignmentReason({
|
||||
bookingUid,
|
||||
});
|
||||
|
||||
if (!booking) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Booking not found" });
|
||||
}
|
||||
|
||||
const teamId = booking.eventType?.teamId || null;
|
||||
const bookingUserId = booking.user?.id || null;
|
||||
|
||||
const assignmentReason = booking.assignmentReason?.[0];
|
||||
const guestEmail = booking.attendees[0]?.email || "";
|
||||
const hostEmail = booking.user?.email || "";
|
||||
const hostName = booking.user?.name || null;
|
||||
const teamId = booking.eventType?.team?.id ?? null;
|
||||
const orgId = booking.eventType?.team?.parentId ?? null;
|
||||
const routingFormId = booking.routedFromRoutingFormReponse?.formId || null;
|
||||
|
||||
const alreadyReported = await wrongAssignmentReportRepo.existsByBookingUid(booking.uid);
|
||||
@@ -98,7 +95,7 @@ export const reportWrongAssignmentHandler = async ({ ctx, input }: ReportWrongAs
|
||||
id: booking.eventType.id,
|
||||
title: booking.eventType.title,
|
||||
slug: booking.eventType.slug,
|
||||
teamId: booking.eventType.teamId,
|
||||
teamId: teamId,
|
||||
}
|
||||
: null,
|
||||
},
|
||||
@@ -108,11 +105,11 @@ export const reportWrongAssignmentHandler = async ({ ctx, input }: ReportWrongAs
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
},
|
||||
routingReason: assignmentReason?.reasonString || null,
|
||||
guest: guestEmail,
|
||||
firstAssignmentReason: booking.assignmentReason[0]?.reasonString ?? null,
|
||||
guest: booking.attendees[0]?.email ?? null,
|
||||
host: {
|
||||
email: hostEmail,
|
||||
name: hostName,
|
||||
email: booking.user?.email ?? null,
|
||||
name: booking.user?.name ?? null,
|
||||
},
|
||||
correctAssignee: correctAssignee || null,
|
||||
additionalNotes: additionalNotes || null,
|
||||
@@ -121,8 +118,9 @@ export const reportWrongAssignmentHandler = async ({ ctx, input }: ReportWrongAs
|
||||
|
||||
try {
|
||||
const webhooks = await getWebhooks({
|
||||
userId: bookingUserId,
|
||||
userId: booking.userId,
|
||||
teamId,
|
||||
orgId,
|
||||
triggerEvent: WebhookTriggerEvents.WRONG_ASSIGNMENT_REPORT,
|
||||
});
|
||||
|
||||
@@ -141,9 +139,9 @@ export const reportWrongAssignmentHandler = async ({ ctx, input }: ReportWrongAs
|
||||
|
||||
await Promise.allSettled(webhookPromises);
|
||||
|
||||
log.info(`Wrong assignment report sent for booking ${bookingUid}`, {
|
||||
log.info(`Wrong assignment report sent for booking ${booking.uid}`, {
|
||||
teamId,
|
||||
userId: bookingUserId,
|
||||
userId: booking.userId,
|
||||
webhookCount: webhooks.length,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { z } from "zod";
|
||||
|
||||
export const ZReportWrongAssignmentInputSchema = z.object({
|
||||
bookingUid: z.string(),
|
||||
correctAssignee: z.string().email().optional(),
|
||||
correctAssignee: z.union([z.string().email(), z.literal("").transform(() => undefined)]).optional(),
|
||||
additionalNotes: z.string().min(1),
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user