From c0504993a60ce8fcdd252aac3d3884cdb89fcf4e Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Tue, 10 Feb 2026 06:00:01 -0500 Subject: [PATCH] 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: joe@cal.com * 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: joe@cal.com * chore: add migration for routingFormId in WrongAssignmentReport Co-Authored-By: joe@cal.com * 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: joe@cal.com * 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: joe@cal.com * test: update mocks for findUnique and i18n in unit tests Co-Authored-By: joe@cal.com * fix: throw UNAUTHORIZED in hasWrongAssignmentReport and squash migrations Co-Authored-By: joe@cal.com * 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: joe@cal.com --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../dialog/WrongAssignmentDialog.tsx | 14 +- apps/web/public/static/locales/en/common.json | 1 + .../repositories/BookingRepository.ts | 5 + .../WrongAssignmentReportRepository.test.ts | 159 ++++++++++++++++++ .../WrongAssignmentReportRepository.ts | 27 +++ .../migration.sql | 48 ++++++ packages/prisma/schema.prisma | 34 ++++ .../routers/viewer/bookings/_router.tsx | 11 ++ .../hasWrongAssignmentReport.handler.ts | 33 ++++ .../hasWrongAssignmentReport.schema.ts | 7 + .../reportWrongAssignment.handler.test.ts | 140 +++++++++++++++ .../bookings/reportWrongAssignment.handler.ts | 36 ++++ 12 files changed, 513 insertions(+), 2 deletions(-) create mode 100644 packages/features/bookings/repositories/WrongAssignmentReportRepository.test.ts create mode 100644 packages/features/bookings/repositories/WrongAssignmentReportRepository.ts create mode 100644 packages/prisma/migrations/20260129205827_add_wrong_assignment_report_table/migration.sql create mode 100644 packages/trpc/server/routers/viewer/bookings/hasWrongAssignmentReport.handler.ts create mode 100644 packages/trpc/server/routers/viewer/bookings/hasWrongAssignmentReport.schema.ts diff --git a/apps/web/components/dialog/WrongAssignmentDialog.tsx b/apps/web/components/dialog/WrongAssignmentDialog.tsx index bb780e87dd..1f3a73c484 100644 --- a/apps/web/components/dialog/WrongAssignmentDialog.tsx +++ b/apps/web/components/dialog/WrongAssignmentDialog.tsx @@ -164,7 +164,7 @@ function AssigneeSection(props: AssigneeSectionProps): JSX.Element { field: ControllerRenderProps; }): JSX.Element => { const handleChange = (option: TeamMemberOption | null): void => { - if (option) field.onChange(option.value); + field.onChange(option ? option.value : ""); }; return (