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:
authored and GitHub committed 2026-02-10 08:00:01 -03:00
1 parent c3a8301b0c
commit c0504993a6
12 files changed
+513 -2

No files matched your search

@@ -0,0 +1,48 @@
-- CreateEnum
CREATE TYPE "public"."WrongAssignmentReportStatus" AS ENUM ('PENDING', 'REVIEWED', 'RESOLVED', 'DISMISSED');
-- CreateTable
CREATE TABLE "public"."WrongAssignmentReport" (
"id" UUID NOT NULL,
"bookingUid" TEXT NOT NULL,
"reportedById" INTEGER,
"correctAssignee" TEXT,
"additionalNotes" TEXT NOT NULL,
"teamId" INTEGER,
"routingFormId" TEXT,
"status" "public"."WrongAssignmentReportStatus" NOT NULL DEFAULT 'PENDING',
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "WrongAssignmentReport_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "WrongAssignmentReport_bookingUid_key" ON "public"."WrongAssignmentReport"("bookingUid");
-- CreateIndex
CREATE INDEX "WrongAssignmentReport_reportedById_idx" ON "public"."WrongAssignmentReport"("reportedById");
-- CreateIndex
CREATE INDEX "WrongAssignmentReport_teamId_idx" ON "public"."WrongAssignmentReport"("teamId");
-- CreateIndex
CREATE INDEX "WrongAssignmentReport_routingFormId_idx" ON "public"."WrongAssignmentReport"("routingFormId");
-- CreateIndex
CREATE INDEX "WrongAssignmentReport_status_idx" ON "public"."WrongAssignmentReport"("status");
-- CreateIndex
CREATE INDEX "WrongAssignmentReport_createdAt_idx" ON "public"."WrongAssignmentReport"("createdAt");
-- AddForeignKey
ALTER TABLE "public"."WrongAssignmentReport" ADD CONSTRAINT "WrongAssignmentReport_bookingUid_fkey" FOREIGN KEY ("bookingUid") REFERENCES "public"."Booking"("uid") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "public"."WrongAssignmentReport" ADD CONSTRAINT "WrongAssignmentReport_reportedById_fkey" FOREIGN KEY ("reportedById") REFERENCES "public"."users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "public"."WrongAssignmentReport" ADD CONSTRAINT "WrongAssignmentReport_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "public"."Team"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "public"."WrongAssignmentReport" ADD CONSTRAINT "WrongAssignmentReport_routingFormId_fkey" FOREIGN KEY ("routingFormId") REFERENCES "public"."App_RoutingForms_Form"("id") ON DELETE SET NULL ON UPDATE CASCADE;
+34
View File
@@ -498,6 +498,7 @@ model User {
createdTranslations EventTypeTranslation[] @relation("CreatedEventTypeTranslations")
updatedTranslations EventTypeTranslation[] @relation("UpdatedEventTypeTranslations")
reportedBookings BookingReport[] @relation("ReportedBy")
wrongAssignmentReports WrongAssignmentReport[] @relation("WrongAssignmentReportedBy")
BookingInternalNote BookingInternalNote[]
creationSource CreationSource?
createdOrganizationOnboardings OrganizationOnboarding[] @relation("CreatedOrganizationOnboardings")
@@ -624,6 +625,7 @@ model Team {
calAiPhoneNumbers CalAiPhoneNumber[]
agents Agent[]
bookingReports BookingReport[]
wrongAssignmentReports WrongAssignmentReport[]
features TeamFeatures[]
@@ -923,6 +925,7 @@ model Booking {
routingFormResponses RoutingFormResponseDenormalized[]
expenseLogs CreditExpenseLog[]
report BookingReport?
wrongAssignmentReports WrongAssignmentReport[]
routingTrace RoutingTrace?
// @@partial_index([reassignById])
@@ -1332,6 +1335,7 @@ model App_RoutingForms_Form {
settings Json?
incompleteBookingActions App_RoutingForms_IncompleteBookingActions[]
workflows WorkflowsOnRoutingForms[]
wrongAssignmentReports WrongAssignmentReport[]
@@index([userId])
@@index([disabled])
@@ -2555,6 +2559,36 @@ model BookingReport {
@@index([createdAt])
}
enum WrongAssignmentReportStatus {
PENDING // Initial state - awaiting review
REVIEWED // Admin has seen it
RESOLVED // Corrective action taken
DISMISSED // Admin decided no action needed
}
model WrongAssignmentReport {
id String @id @default(uuid()) @db.Uuid
bookingUid String @unique
booking Booking @relation(fields: [bookingUid], references: [uid], onDelete: Cascade)
reportedById Int?
reportedBy User? @relation("WrongAssignmentReportedBy", fields: [reportedById], references: [id], onDelete: SetNull)
correctAssignee String?
additionalNotes String
teamId Int?
team Team? @relation(fields: [teamId], references: [id], onDelete: SetNull)
routingFormId String?
routingForm App_RoutingForms_Form? @relation(fields: [routingFormId], references: [id], onDelete: SetNull)
status WrongAssignmentReportStatus @default(PENDING)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([reportedById])
@@index([teamId])
@@index([routingFormId])
@@index([status])
@@index([createdAt])
}
enum BillingPeriod {
MONTHLY
ANNUALLY