improvement: v1 standalone treatment/case creation made possible.

This commit is contained in:
2026-08-19 15:05:58 +03:30
parent e5c39c6b7e
commit 8bfa8c88fe
39 changed files with 3296 additions and 387 deletions

View File

@@ -0,0 +1,79 @@
-- AlterTable
ALTER TABLE "patients" ADD COLUMN "isWalkIn" BOOLEAN NOT NULL DEFAULT false;
CREATE INDEX "patients_createdByOrganizationId_isWalkIn_idx" ON "patients"("createdByOrganizationId", "isWalkIn");
CREATE UNIQUE INDEX "patients_one_walkin_per_org" ON "patients"("createdByOrganizationId") WHERE "isWalkIn" = true;
-- CreateEnum
CREATE TYPE "LabCaseOrigin" AS ENUM ('CLINIC_DISPATCH', 'LAB_INTERNAL');
-- AlterTable
ALTER TABLE "lab_cases" ALTER COLUMN "treatmentId" DROP NOT NULL;
ALTER TABLE "lab_cases" ALTER COLUMN "sortOrder" SET DEFAULT 0;
ALTER TABLE "lab_cases" ADD COLUMN "startedAt" TIMESTAMP(3);
ALTER TABLE "lab_cases" ADD COLUMN "origin" "LabCaseOrigin" NOT NULL DEFAULT 'CLINIC_DISPATCH';
ALTER TABLE "lab_cases" ADD COLUMN "referringClinicName" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "referringDentistName" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "patientDisplayName" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "patientDisplayMobile" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "partnerClinicOrganizationId" TEXT;
CREATE INDEX "lab_cases_destinationOrganizationId_origin_idx" ON "lab_cases"("destinationOrganizationId", "origin");
CREATE INDEX "lab_cases_partnerClinicOrganizationId_idx" ON "lab_cases"("partnerClinicOrganizationId");
ALTER TABLE "lab_cases" ADD CONSTRAINT "lab_cases_partnerClinicOrganizationId_fkey" FOREIGN KEY ("partnerClinicOrganizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- CreateTable
CREATE TABLE "lab_case_lines" (
"id" TEXT NOT NULL,
"labCaseId" TEXT NOT NULL,
"clientKey" TEXT,
"sortOrder" INTEGER NOT NULL,
"treatmentType" TEXT NOT NULL DEFAULT 'prosthesis',
"teeth" JSONB NOT NULL,
"toothSelectionGroups" JSONB,
"comment" TEXT,
CONSTRAINT "lab_case_lines_pkey" PRIMARY KEY ("id")
);
CREATE INDEX "lab_case_lines_labCaseId_sortOrder_idx" ON "lab_case_lines"("labCaseId", "sortOrder");
ALTER TABLE "lab_case_lines" ADD CONSTRAINT "lab_case_lines_labCaseId_fkey" FOREIGN KEY ("labCaseId") REFERENCES "lab_cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- Tooth prosthesis: nullable detail, lineId, sourceKey, treatmentType
DROP INDEX IF EXISTS "lab_case_tooth_prosthesis_labCaseId_treatmentDetailId_tooth_key";
ALTER TABLE "lab_case_tooth_prosthesis" ALTER COLUMN "treatmentDetailId" DROP NOT NULL;
ALTER TABLE "lab_case_tooth_prosthesis" ADD COLUMN "lineId" TEXT;
ALTER TABLE "lab_case_tooth_prosthesis" ADD COLUMN "sourceKey" TEXT;
ALTER TABLE "lab_case_tooth_prosthesis" ADD COLUMN "treatmentType" TEXT NOT NULL DEFAULT 'prosthesis';
UPDATE "lab_case_tooth_prosthesis" SET "sourceKey" = "treatmentDetailId" WHERE "sourceKey" IS NULL AND "treatmentDetailId" IS NOT NULL;
ALTER TABLE "lab_case_tooth_prosthesis" ALTER COLUMN "sourceKey" SET NOT NULL;
CREATE UNIQUE INDEX "lab_case_tooth_prosthesis_labCaseId_sourceKey_tooth_key" ON "lab_case_tooth_prosthesis"("labCaseId", "sourceKey", "tooth");
ALTER TABLE "lab_case_tooth_prosthesis" ADD CONSTRAINT "lab_case_tooth_prosthesis_lineId_fkey" FOREIGN KEY ("lineId") REFERENCES "lab_case_lines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- Tasks: nullable detail, lineId, sourceKey
ALTER TABLE "lab_case_tasks" DROP CONSTRAINT IF EXISTS "lab_case_tasks_case_detail_prosthesis_step_key";
DROP INDEX IF EXISTS "lab_case_tasks_case_detail_prosthesis_step_key";
ALTER TABLE "lab_case_tasks" ALTER COLUMN "treatmentDetailId" DROP NOT NULL;
ALTER TABLE "lab_case_tasks" ADD COLUMN "lineId" TEXT;
ALTER TABLE "lab_case_tasks" ADD COLUMN "sourceKey" TEXT;
UPDATE "lab_case_tasks" SET "sourceKey" = "treatmentDetailId" WHERE "sourceKey" IS NULL AND "treatmentDetailId" IS NOT NULL;
ALTER TABLE "lab_case_tasks" ALTER COLUMN "sourceKey" SET NOT NULL;
CREATE UNIQUE INDEX "lab_case_tasks_case_source_prosthesis_step_key" ON "lab_case_tasks"("labCaseId", "sourceKey", "prosthesisTypeCode", "stepOrder");
ALTER TABLE "lab_case_tasks" ADD CONSTRAINT "lab_case_tasks_lineId_fkey" FOREIGN KEY ("lineId") REFERENCES "lab_case_lines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
CREATE INDEX "treatments_organizationId_providerUserId_treatmentAt_idx" ON "treatments"("organizationId", "providerUserId", "treatmentAt");
ALTER TABLE "treatment_detail_attachments" ADD COLUMN "treatmentId" TEXT;
CREATE INDEX "treatment_detail_attachments_treatmentId_detailClientKey_idx" ON "treatment_detail_attachments"("treatmentId", "detailClientKey");

View File

@@ -37,6 +37,7 @@ async function main() {
let total = 0;
for (const labCase of cases) {
if (!labCase.treatment) continue;
const locale = labCase.treatment.organization.owner.language ?? 'en';
// Clear any stale tasks first so the generator's "already exists" guard passes.
await prisma.labCaseTask.deleteMany({ where: { labCaseId: labCase.id } });

View File

@@ -90,6 +90,7 @@ model Organization {
treatments Treatment[]
labCaseSends LabCaseSend[]
labCaseComments LabCaseComment[]
partnerLabCases LabCase[] @relation("LabCasePartnerClinic")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -106,6 +107,7 @@ model Patient {
dateOfBirth DateTime?
notes String?
isActive Boolean @default(true)
isWalkIn Boolean @default(false)
createdByOrganizationId String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -115,6 +117,7 @@ model Patient {
appointments Appointment[]
@@index([lastName, firstName])
@@index([createdByOrganizationId, isWalkIn])
@@map("patients")
}
@@ -149,6 +152,11 @@ enum LabCaseCommentSide {
CLINIC
}
enum LabCaseOrigin {
CLINIC_DISPATCH
LAB_INTERNAL
}
model Treatment {
id String @id @default(uuid())
organizationId String
@@ -168,6 +176,7 @@ model Treatment {
updatedAt DateTime @updatedAt
@@index([patientId, treatmentAt])
@@index([organizationId, providerUserId, treatmentAt])
@@map("treatments")
}
@@ -196,6 +205,7 @@ model TreatmentDetailAttachment {
id String @id @default(uuid())
detailId String?
appointmentId String?
treatmentId String?
detailClientKey String?
fileName String
mimeType String
@@ -208,25 +218,35 @@ model TreatmentDetailAttachment {
createdAt DateTime @default(now())
@@index([appointmentId, detailClientKey])
@@index([treatmentId, detailClientKey])
@@index([detailId])
@@map("treatment_detail_attachments")
}
model LabCase {
id String @id @default(uuid())
treatmentId String
id String @id @default(uuid())
treatmentId String?
clientKey String?
sortOrder Int
sortOrder Int @default(0)
destinationOrganizationId String?
sentAt DateTime?
startedAt DateTime?
dueDate DateTime?
isImportant Boolean @default(false)
isImportant Boolean @default(false)
origin LabCaseOrigin @default(CLINIC_DISPATCH)
/// Optional code from an external lab app (e.g. exocad) for print/matching.
externalCode String?
accessToken String? @unique
accessToken String? @unique
referringClinicName String?
referringDentistName String?
patientDisplayName String?
patientDisplayMobile String?
partnerClinicOrganizationId String?
treatment Treatment @relation(fields: [treatmentId], references: [id], onDelete: Cascade)
treatment Treatment? @relation(fields: [treatmentId], references: [id], onDelete: Cascade)
partnerClinic Organization? @relation("LabCasePartnerClinic", fields: [partnerClinicOrganizationId], references: [id], onDelete: SetNull)
details LabCaseDetail[]
lines LabCaseLine[]
sends LabCaseSend[]
tasks LabCaseTask[]
toothProsthesis LabCaseToothProsthesis[]
@@ -236,9 +256,30 @@ model LabCase {
userReadStates LabCaseUserReadState[]
@@index([treatmentId, sortOrder])
@@index([destinationOrganizationId, origin])
@@index([partnerClinicOrganizationId])
@@map("lab_cases")
}
/// Lab-origin work lines (analog of TreatmentDetail). Clinic-sent cases do not use this table.
model LabCaseLine {
id String @id @default(uuid())
labCaseId String
clientKey String?
sortOrder Int
treatmentType String @default("prosthesis")
teeth Json
toothSelectionGroups Json?
comment String?
labCase LabCase @relation(fields: [labCaseId], references: [id], onDelete: Cascade)
toothProsthesis LabCaseToothProsthesis[]
tasks LabCaseTask[]
@@index([labCaseId, sortOrder])
@@map("lab_case_lines")
}
model LabCaseAttachment {
labCaseId String
attachmentId String
@@ -343,23 +384,31 @@ model ProsthesisTypeStep {
model LabCaseToothProsthesis {
id String @id @default(uuid())
labCaseId String
treatmentDetailId String
treatmentDetailId String?
lineId String?
/// treatmentDetailId (clinic) or lineId (lab-origin) — required unique grouping key.
sourceKey String
tooth String
prosthesisTypeCode String
/** Links to TreatmentDetail.toothSelectionGroups[].groupId for task grouping. */
selectionGroupId String @default("")
treatmentType String @default("prosthesis")
/** Links to TreatmentDetail/LabCaseLine toothSelectionGroups[].groupId for task grouping. */
selectionGroupId String @default("")
labCase LabCase @relation(fields: [labCaseId], references: [id], onDelete: Cascade)
detail TreatmentDetail @relation(fields: [treatmentDetailId], references: [id], onDelete: Cascade)
labCase LabCase @relation(fields: [labCaseId], references: [id], onDelete: Cascade)
detail TreatmentDetail? @relation(fields: [treatmentDetailId], references: [id], onDelete: Cascade)
line LabCaseLine? @relation(fields: [lineId], references: [id], onDelete: Cascade)
@@unique([labCaseId, treatmentDetailId, tooth])
@@unique([labCaseId, sourceKey, tooth])
@@map("lab_case_tooth_prosthesis")
}
model LabCaseTask {
id String @id @default(uuid())
labCaseId String
treatmentDetailId String
treatmentDetailId String?
lineId String?
/// treatmentDetailId (clinic) or lineId (lab-origin) — required unique grouping key.
sourceKey String
teeth Json
treatmentType String
prosthesisTypeCode String
@@ -374,16 +423,17 @@ model LabCaseTask {
lastStatusChangedByUserId String?
lastStatusChangedAt DateTime?
labCase LabCase @relation(fields: [labCaseId], references: [id], onDelete: Cascade)
detail TreatmentDetail @relation(fields: [treatmentDetailId], references: [id], onDelete: Cascade)
assignee User? @relation("LabCaseTaskAssignee", fields: [assigneeUserId], references: [id], onDelete: SetNull)
labCase LabCase @relation(fields: [labCaseId], references: [id], onDelete: Cascade)
detail TreatmentDetail? @relation(fields: [treatmentDetailId], references: [id], onDelete: Cascade)
line LabCaseLine? @relation(fields: [lineId], references: [id], onDelete: Cascade)
assignee User? @relation("LabCaseTaskAssignee", fields: [assigneeUserId], references: [id], onDelete: SetNull)
lastStatusChangedBy User? @relation("LabCaseTaskLastStatusChangedBy", fields: [lastStatusChangedByUserId], references: [id], onDelete: SetNull)
statusEvents LabCaseTaskStatusEvent[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([labCaseId, treatmentDetailId, prosthesisTypeCode, stepOrder], map: "lab_case_tasks_case_detail_prosthesis_step_key")
@@unique([labCaseId, sourceKey, prosthesisTypeCode, stepOrder], map: "lab_case_tasks_case_source_prosthesis_step_key")
@@index([labCaseId, status])
@@index([assigneeUserId])
@@map("lab_case_tasks")