92 lines
2.5 KiB
TypeScript
92 lines
2.5 KiB
TypeScript
|
|
import { LabTaskStatus, Prisma } from '@prisma/client';
|
||
|
|
import { normalizeTeeth } from '../treatments/treatment.utils';
|
||
|
|
|
||
|
|
type TransactionClient = Prisma.TransactionClient;
|
||
|
|
|
||
|
|
export async function generateLabCaseTasks(
|
||
|
|
tx: TransactionClient,
|
||
|
|
labCaseId: string,
|
||
|
|
): Promise<number> {
|
||
|
|
const existingCount = await tx.labCaseTask.count({ where: { labCaseId } });
|
||
|
|
if (existingCount > 0) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
const labCase = await tx.labCase.findUnique({
|
||
|
|
where: { id: labCaseId },
|
||
|
|
include: {
|
||
|
|
details: {
|
||
|
|
include: {
|
||
|
|
detail: {
|
||
|
|
select: { id: true, treatmentType: true, teeth: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!labCase?.details.length) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
const treatmentTypeCodes = [...new Set(labCase.details.map((d) => d.detail.treatmentType))];
|
||
|
|
|
||
|
|
const labDependentTypes = await tx.treatmentType.findMany({
|
||
|
|
where: { code: { in: treatmentTypeCodes }, labDependent: true },
|
||
|
|
select: { id: true, code: true },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (labDependentTypes.length === 0) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
const labDependentCodes = new Set(labDependentTypes.map((t) => t.code));
|
||
|
|
|
||
|
|
const workflowSteps = await tx.treatmentWorkflowStep.findMany({
|
||
|
|
where: { treatmentTypeId: { in: labDependentTypes.map((t) => t.id) } },
|
||
|
|
orderBy: [{ treatmentTypeId: 'asc' }, { stepOrder: 'asc' }],
|
||
|
|
include: { treatmentType: { select: { code: true } } },
|
||
|
|
});
|
||
|
|
|
||
|
|
const stepsByTypeCode = new Map<string, { stepOrder: number; label: string }[]>();
|
||
|
|
for (const step of workflowSteps) {
|
||
|
|
const code = step.treatmentType.code;
|
||
|
|
const list = stepsByTypeCode.get(code) ?? [];
|
||
|
|
list.push({ stepOrder: step.stepOrder, label: step.label });
|
||
|
|
stepsByTypeCode.set(code, list);
|
||
|
|
}
|
||
|
|
|
||
|
|
const taskRows: Prisma.LabCaseTaskCreateManyInput[] = [];
|
||
|
|
|
||
|
|
for (const link of labCase.details) {
|
||
|
|
const detail = link.detail;
|
||
|
|
if (!labDependentCodes.has(detail.treatmentType)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
const teeth = normalizeTeeth(detail.teeth);
|
||
|
|
const typeSteps = stepsByTypeCode.get(detail.treatmentType) ?? [];
|
||
|
|
|
||
|
|
for (const tooth of teeth) {
|
||
|
|
for (const step of typeSteps) {
|
||
|
|
taskRows.push({
|
||
|
|
labCaseId,
|
||
|
|
treatmentDetailId: detail.id,
|
||
|
|
tooth,
|
||
|
|
treatmentType: detail.treatmentType,
|
||
|
|
stepOrder: step.stepOrder,
|
||
|
|
stepLabel: step.label,
|
||
|
|
status: LabTaskStatus.PENDING,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (taskRows.length === 0) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
await tx.labCaseTask.createMany({ data: taskRows });
|
||
|
|
return taskRows.length;
|
||
|
|
}
|