feat: Write wrong assignment reports to the database (#27405)
* Add DB table for wrong assignment reports * When report is submitted write to the db * Prevent duplicate reportings * test: add migration and tests for WrongAssignmentReport table Co-Authored-By: [email protected] <[email protected]> * fix: add unique constraint on bookingUid and booking access check for hasWrongAssignmentReport - Add @unique constraint on bookingUid in WrongAssignmentReport model to prevent duplicate reports at DB level - Add booking ownership check using BookingAccessService in hasWrongAssignmentReport endpoint - Refactor hasWrongAssignmentReport into separate handler and schema files Addresses Cubic AI review feedback on PR #27405 Co-Authored-By: unknown <> * feat: add routingFormId to WrongAssignmentReport and fix Select clearing - Add routingFormId field to WrongAssignmentReport model in schema.prisma - Add relation to App_RoutingForms_Form with SetNull on delete - Update WrongAssignmentReportRepository.createReport to accept routingFormId - Update BookingRepository.findByUidIncludeEventTypeAndTeamAndAssignmentReason to include routedFromRoutingFormReponse - Extract routingFormId from booking in reportWrongAssignment handler - Fix Select clearing issue: handle null case when user clears team member selection - Update tests to include routingFormId field Co-Authored-By: [email protected] <[email protected]> * chore: add migration for routingFormId in WrongAssignmentReport Co-Authored-By: [email protected] <[email protected]> * fix: address Udit's review comments - hasWrongAssignmentReport: throw UNAUTHORIZED error instead of returning false - reportWrongAssignment: add try-catch for Prisma P2002 unique constraint error - WrongAssignmentReport: add Team relation to teamId field - WrongAssignmentReportRepository: use findUnique instead of findFirst - reportWrongAssignment: use i18n for error messages Co-Authored-By: [email protected] <[email protected]> * fix: revert hasWrongAssignmentReport to return false when user lacks access Per PR checklist, hasWrongAssignmentReport should return { hasReport: false } when user lacks access to booking, not throw an error. This allows the UI to gracefully treat 'no access' as 'no report exists'. Co-Authored-By: [email protected] <[email protected]> * test: update mocks for findUnique and i18n in unit tests Co-Authored-By: [email protected] <[email protected]> * fix: throw UNAUTHORIZED in hasWrongAssignmentReport and squash migrations Co-Authored-By: [email protected] <[email protected]> * fix: change User relation onDelete from Cascade to SetNull in WrongAssignmentReport Address Hariom's review feedback: - Changed reportedById from Int to Int? (nullable) - Changed reportedBy relation from onDelete: Cascade to onDelete: SetNull - Updated migration SQL to reflect these changes This preserves wrong assignment reports even when the reporting user is deleted, as the data is still useful for analysis. 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:
12 files changed
+513
-2
No files matched your search
@@ -2162,6 +2162,11 @@ export class BookingRepository implements IBookingRepository {
|
||||
reasonEnum: true,
|
||||
},
|
||||
},
|
||||
routedFromRoutingFormReponse: {
|
||||
select: {
|
||||
formId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
import { WrongAssignmentReportRepository } from "./WrongAssignmentReportRepository";
|
||||
|
||||
describe("WrongAssignmentReportRepository", () => {
|
||||
let repository: WrongAssignmentReportRepository;
|
||||
|
||||
const mockPrisma = {
|
||||
wrongAssignmentReport: {
|
||||
findUnique: vi.fn(),
|
||||
create: vi.fn(),
|
||||
},
|
||||
} as unknown as PrismaClient;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
repository = new WrongAssignmentReportRepository(mockPrisma);
|
||||
});
|
||||
|
||||
describe("existsByBookingUid", () => {
|
||||
it("should return true when a report exists for the booking", async () => {
|
||||
mockPrisma.wrongAssignmentReport.findUnique.mockResolvedValue({ id: "report-123" });
|
||||
|
||||
const result = await repository.existsByBookingUid("test-booking-uid");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockPrisma.wrongAssignmentReport.findUnique).toHaveBeenCalledWith({
|
||||
where: { bookingUid: "test-booking-uid" },
|
||||
select: { id: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("should return false when no report exists for the booking", async () => {
|
||||
mockPrisma.wrongAssignmentReport.findUnique.mockResolvedValue(null);
|
||||
|
||||
const result = await repository.existsByBookingUid("non-existent-booking-uid");
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(mockPrisma.wrongAssignmentReport.findUnique).toHaveBeenCalledWith({
|
||||
where: { bookingUid: "non-existent-booking-uid" },
|
||||
select: { id: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle empty string bookingUid", async () => {
|
||||
mockPrisma.wrongAssignmentReport.findUnique.mockResolvedValue(null);
|
||||
|
||||
const result = await repository.existsByBookingUid("");
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(mockPrisma.wrongAssignmentReport.findUnique).toHaveBeenCalledWith({
|
||||
where: { bookingUid: "" },
|
||||
select: { id: true },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("createReport", () => {
|
||||
it("should create a report with all fields", async () => {
|
||||
const input = {
|
||||
bookingUid: "test-booking-uid",
|
||||
reportedById: 1,
|
||||
correctAssignee: "[email protected]",
|
||||
additionalNotes: "This booking was assigned incorrectly",
|
||||
teamId: 5,
|
||||
routingFormId: "routing-form-123",
|
||||
};
|
||||
|
||||
const mockCreatedReport = { id: "report-uuid-123" };
|
||||
mockPrisma.wrongAssignmentReport.create.mockResolvedValue(mockCreatedReport);
|
||||
|
||||
const result = await repository.createReport(input);
|
||||
|
||||
expect(result).toEqual({ id: "report-uuid-123" });
|
||||
expect(mockPrisma.wrongAssignmentReport.create).toHaveBeenCalledWith({
|
||||
data: input,
|
||||
select: { id: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a report with null optional fields", async () => {
|
||||
const input = {
|
||||
bookingUid: "test-booking-uid-2",
|
||||
reportedById: 2,
|
||||
correctAssignee: null,
|
||||
additionalNotes: "Wrong person",
|
||||
teamId: null,
|
||||
routingFormId: null,
|
||||
};
|
||||
|
||||
const mockCreatedReport = { id: "report-uuid-456" };
|
||||
mockPrisma.wrongAssignmentReport.create.mockResolvedValue(mockCreatedReport);
|
||||
|
||||
const result = await repository.createReport(input);
|
||||
|
||||
expect(result).toEqual({ id: "report-uuid-456" });
|
||||
expect(mockPrisma.wrongAssignmentReport.create).toHaveBeenCalledWith({
|
||||
data: input,
|
||||
select: { id: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a report with empty additionalNotes", async () => {
|
||||
const input = {
|
||||
bookingUid: "test-booking-uid-3",
|
||||
reportedById: 3,
|
||||
correctAssignee: "[email protected]",
|
||||
additionalNotes: "",
|
||||
teamId: 10,
|
||||
routingFormId: "routing-form-456",
|
||||
};
|
||||
|
||||
const mockCreatedReport = { id: "report-uuid-789" };
|
||||
mockPrisma.wrongAssignmentReport.create.mockResolvedValue(mockCreatedReport);
|
||||
|
||||
const result = await repository.createReport(input);
|
||||
|
||||
expect(result).toEqual({ id: "report-uuid-789" });
|
||||
expect(mockPrisma.wrongAssignmentReport.create).toHaveBeenCalledWith({
|
||||
data: input,
|
||||
select: { id: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("should propagate database errors", async () => {
|
||||
const input = {
|
||||
bookingUid: "test-booking-uid",
|
||||
reportedById: 1,
|
||||
correctAssignee: null,
|
||||
additionalNotes: "Notes",
|
||||
teamId: null,
|
||||
routingFormId: null,
|
||||
};
|
||||
|
||||
mockPrisma.wrongAssignmentReport.create.mockRejectedValue(new Error("Database connection failed"));
|
||||
|
||||
await expect(repository.createReport(input)).rejects.toThrow("Database connection failed");
|
||||
});
|
||||
|
||||
it("should propagate foreign key constraint errors", async () => {
|
||||
const input = {
|
||||
bookingUid: "non-existent-booking",
|
||||
reportedById: 999999,
|
||||
correctAssignee: null,
|
||||
additionalNotes: "Notes",
|
||||
teamId: null,
|
||||
routingFormId: null,
|
||||
};
|
||||
|
||||
mockPrisma.wrongAssignmentReport.create.mockRejectedValue(
|
||||
new Error("Foreign key constraint failed on the field: `bookingUid`")
|
||||
);
|
||||
|
||||
await expect(repository.createReport(input)).rejects.toThrow("Foreign key constraint failed");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
export class WrongAssignmentReportRepository {
|
||||
constructor(private readonly prismaClient: PrismaClient) {}
|
||||
|
||||
async existsByBookingUid(bookingUid: string): Promise<boolean> {
|
||||
const report = await this.prismaClient.wrongAssignmentReport.findUnique({
|
||||
where: { bookingUid },
|
||||
select: { id: true },
|
||||
});
|
||||
return !!report;
|
||||
}
|
||||
|
||||
async createReport(input: {
|
||||
bookingUid: string;
|
||||
reportedById: number;
|
||||
correctAssignee: string | null;
|
||||
additionalNotes: string;
|
||||
teamId: number | null;
|
||||
routingFormId: string | null;
|
||||
}): Promise<{ id: string }> {
|
||||
return this.prismaClient.wrongAssignmentReport.create({
|
||||
data: input,
|
||||
select: { id: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user