improvement: fdi tooth chart selection modes polished. bugs related to teeth selecyion fixed.

This commit is contained in:
2026-07-18 00:02:40 +03:30
parent 4b2349228a
commit 0740493384
23 changed files with 384 additions and 161 deletions

View File

@@ -0,0 +1,15 @@
-- The tooth_selection_groups migration tried to drop
-- "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCode_stepOrder_key",
-- but PostgreSQL truncated/renamed that index to
-- "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCo_key"
-- (see 20260710150528). The old unique index (without selectionGroupId)
-- therefore remained and blocks multiple selection groups that share
-- the same prosthesis type + stepOrder.
DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCo_key";
DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCode_stepOrder_key";
DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCode_s";
-- Ensure the correct unique index exists (idempotent for DBs that already have it).
CREATE UNIQUE INDEX IF NOT EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_selectionGroupId_prosthesisTypeCode_stepOrder_key"
ON "lab_case_tasks"("labCaseId", "treatmentDetailId", "selectionGroupId", "prosthesisTypeCode", "stepOrder");

View File

@@ -0,0 +1,34 @@
-- Defensive cleanup for stage/prod (and any DB where the stale unique index
-- survived under a truncated/renamed Postgres identifier).
--
-- History:
-- 20260707130000 created unique (labCaseId, treatmentDetailId, prosthesisTypeCode, stepOrder)
-- → PG truncates the index name to 63 chars (...prosthesisTypeCode_s)
-- 20260710150528 renamed that truncated name to ...prosthesisTypeCo_key
-- 20260716120000 dropped the *untruncated* name (no-op) and added the correct
-- unique that includes selectionGroupId — leaving the stale unique in place
-- 20260717123000 drops known stale names; this migration also drops by definition
-- so any leftover old unique is removed regardless of identifier length/rename.
DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT indexname
FROM pg_indexes
WHERE schemaname = 'public'
AND tablename = 'lab_case_tasks'
AND indexdef ILIKE '%UNIQUE%'
AND indexdef ILIKE '%prosthesisTypeCode%'
AND indexdef ILIKE '%stepOrder%'
AND indexdef ILIKE '%treatmentDetailId%'
AND indexdef NOT ILIKE '%selectionGroupId%'
LOOP
EXECUTE format('DROP INDEX IF EXISTS %I', r.indexname);
END LOOP;
END $$;
-- Correct unique (includes selectionGroupId). Idempotent if already present.
CREATE UNIQUE INDEX IF NOT EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_selectionGroupId_prosthesisTypeCode_stepOrder_key"
ON "lab_case_tasks"("labCaseId", "treatmentDetailId", "selectionGroupId", "prosthesisTypeCode", "stepOrder");

View File

@@ -0,0 +1,40 @@
-- Restore merge-by-prosthesis uniqueness for lab case tasks.
-- Product rule: all teeth on a detail that share the same prosthesis type become
-- one task set (Scan/Design/…). selectionGroupId remains on the table for UI
-- but is not part of the unique key.
--
-- Drop the selectionGroupId-inclusive unique (by known names + by definition).
DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT indexname
FROM pg_indexes
WHERE schemaname = 'public'
AND tablename = 'lab_case_tasks'
AND indexdef ILIKE '%UNIQUE%'
AND indexdef ILIKE '%selectionGroupId%'
AND indexdef ILIKE '%prosthesisTypeCode%'
AND indexdef ILIKE '%stepOrder%'
LOOP
EXECUTE format('DROP INDEX IF EXISTS %I', r.indexname);
END LOOP;
END $$;
DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_selectionGroupId_prosthesisTypeCode_stepOrder_key";
-- Collapse rows that were split by selectionGroupId but share the merge key
-- (status events cascade via FK). Kept row may need regen for full teeth[]; new sends are correct.
DELETE FROM "lab_case_tasks" AS a
USING "lab_case_tasks" AS b
WHERE a."id" > b."id"
AND a."labCaseId" = b."labCaseId"
AND a."treatmentDetailId" = b."treatmentDetailId"
AND a."prosthesisTypeCode" = b."prosthesisTypeCode"
AND a."stepOrder" = b."stepOrder";
-- Short name stays under Postgres 63-char identifier limit.
CREATE UNIQUE INDEX IF NOT EXISTS "lab_case_tasks_case_detail_prosthesis_step_key"
ON "lab_case_tasks"("labCaseId", "treatmentDetailId", "prosthesisTypeCode", "stepOrder");

View File

@@ -379,7 +379,7 @@ model LabCaseTask {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([labCaseId, treatmentDetailId, selectionGroupId, prosthesisTypeCode, stepOrder])
@@unique([labCaseId, treatmentDetailId, prosthesisTypeCode, stepOrder], map: "lab_case_tasks_case_detail_prosthesis_step_key")
@@index([labCaseId, status])
@@index([assigneeUserId])
@@map("lab_case_tasks")