TreatmentType and ProsthesisType database shcema and data updated. Lab dispatch wired through new data.
This commit is contained in:
@@ -3,6 +3,14 @@ import { PrismaClient } from '@prisma/client';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { config } from 'dotenv';
|
||||
import path from 'path';
|
||||
import {
|
||||
TREATMENT_TYPES,
|
||||
LEGACY_TREATMENT_TYPES,
|
||||
LAB_WORKFLOW_STEPS,
|
||||
PROSTHESIS_TYPES,
|
||||
CATALOG_TRANSLATIONS,
|
||||
buildProsthesisStepCodes,
|
||||
} from './catalog-seed-data';
|
||||
|
||||
// Load environment variables from the correct path
|
||||
const envPath = path.join(__dirname, '..', '.env');
|
||||
@@ -128,57 +136,118 @@ async function main() {
|
||||
}
|
||||
console.log('✅ Created features and permissions');
|
||||
|
||||
const workflowSteps = [
|
||||
{ code: 'endo', stepOrder: 1, label: 'Access review' },
|
||||
{ code: 'endo', stepOrder: 2, label: 'Fabrication' },
|
||||
] as const;
|
||||
|
||||
const treatmentTypes = [
|
||||
{ code: 'consultation', labDependent: false, sortOrder: 1 },
|
||||
{ code: 'filling', labDependent: false, sortOrder: 2 },
|
||||
{ code: 'endo', labDependent: true, sortOrder: 3 },
|
||||
{ code: 'visit', labDependent: false, sortOrder: 4 },
|
||||
{ code: 'hygiene', labDependent: false, sortOrder: 5 },
|
||||
] as const;
|
||||
|
||||
for (const type of treatmentTypes) {
|
||||
for (const type of [...TREATMENT_TYPES, ...LEGACY_TREATMENT_TYPES]) {
|
||||
const isActive = TREATMENT_TYPES.some((t) => t.code === type.code);
|
||||
await prisma.treatmentType.upsert({
|
||||
where: { code: type.code },
|
||||
update: { labDependent: type.labDependent, sortOrder: type.sortOrder },
|
||||
update: {
|
||||
labDependent: type.labDependent,
|
||||
sortOrder: type.sortOrder,
|
||||
isActive,
|
||||
},
|
||||
create: {
|
||||
id: randomUUID(),
|
||||
code: type.code,
|
||||
labDependent: type.labDependent,
|
||||
sortOrder: type.sortOrder,
|
||||
isActive,
|
||||
},
|
||||
});
|
||||
}
|
||||
console.log('✅ Seeded treatment type catalog');
|
||||
|
||||
for (const step of workflowSteps) {
|
||||
const treatmentType = await prisma.treatmentType.findUniqueOrThrow({
|
||||
for (const step of LAB_WORKFLOW_STEPS) {
|
||||
await prisma.labWorkflowStep.upsert({
|
||||
where: { code: step.code },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
await prisma.treatmentWorkflowStep.upsert({
|
||||
where: {
|
||||
treatmentTypeId_stepOrder: {
|
||||
treatmentTypeId: treatmentType.id,
|
||||
stepOrder: step.stepOrder,
|
||||
},
|
||||
},
|
||||
update: { label: step.label },
|
||||
update: { sortOrder: step.sortOrder },
|
||||
create: {
|
||||
id: randomUUID(),
|
||||
treatmentTypeId: treatmentType.id,
|
||||
stepOrder: step.stepOrder,
|
||||
label: step.label,
|
||||
code: step.code,
|
||||
sortOrder: step.sortOrder,
|
||||
},
|
||||
});
|
||||
}
|
||||
console.log('✅ Seeded lab workflow steps');
|
||||
|
||||
const workflowStepByCode = new Map(
|
||||
(
|
||||
await prisma.labWorkflowStep.findMany({
|
||||
select: { id: true, code: true },
|
||||
})
|
||||
).map((s) => [s.code, s.id]),
|
||||
);
|
||||
|
||||
for (const type of PROSTHESIS_TYPES) {
|
||||
const prosthesisType = await prisma.prosthesisType.upsert({
|
||||
where: { code: type.code },
|
||||
update: {
|
||||
sortOrder: type.sortOrder,
|
||||
skipPackingShipping: type.skipPackingShipping ?? false,
|
||||
isActive: true,
|
||||
},
|
||||
create: {
|
||||
id: randomUUID(),
|
||||
code: type.code,
|
||||
sortOrder: type.sortOrder,
|
||||
skipPackingShipping: type.skipPackingShipping ?? false,
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
|
||||
const stepCodes = buildProsthesisStepCodes(type);
|
||||
for (const [index, stepCode] of stepCodes.entries()) {
|
||||
const labWorkflowStepId = workflowStepByCode.get(stepCode);
|
||||
if (!labWorkflowStepId) {
|
||||
throw new Error(`Unknown workflow step code: ${stepCode}`);
|
||||
}
|
||||
|
||||
await prisma.prosthesisTypeStep.upsert({
|
||||
where: {
|
||||
prosthesisTypeId_stepOrder: {
|
||||
prosthesisTypeId: prosthesisType.id,
|
||||
stepOrder: index + 1,
|
||||
},
|
||||
},
|
||||
update: { labWorkflowStepId },
|
||||
create: {
|
||||
id: randomUUID(),
|
||||
prosthesisTypeId: prosthesisType.id,
|
||||
labWorkflowStepId,
|
||||
stepOrder: index + 1,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log('✅ Seeded prosthesis types and workflow mappings');
|
||||
|
||||
for (const tr of CATALOG_TRANSLATIONS) {
|
||||
const existing = await prisma.catalogTranslation.findFirst({
|
||||
where: {
|
||||
entityKind: tr.entityKind,
|
||||
entityCode: tr.entityCode,
|
||||
locale: tr.locale,
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
await prisma.catalogTranslation.update({
|
||||
where: { id: existing.id },
|
||||
data: { label: tr.label },
|
||||
});
|
||||
} else {
|
||||
await prisma.catalogTranslation.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
entityKind: tr.entityKind,
|
||||
entityCode: tr.entityCode,
|
||||
locale: tr.locale,
|
||||
label: tr.label,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log('✅ Seeded catalog translations');
|
||||
|
||||
console.log('🌱 Seeding completed successfully!');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user