62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
/**
|
|
* Dev-only: regenerate lab case tasks from existing LabCaseToothProsthesis rows.
|
|
* Usage: npx ts-node prisma/regenerate-lab-tasks.ts
|
|
*
|
|
* The lab workflow refactor truncated lab_case_tasks. This rebuilds task sets
|
|
* (grouped by treatment detail + prosthesis type, one set per workflow step)
|
|
* for every already-sent case that still has prosthesis selections.
|
|
*/
|
|
import { PrismaClient } from '@prisma/client';
|
|
import { config } from 'dotenv';
|
|
import path from 'path';
|
|
import { generateLabCaseTasks } from '../src/modules/cases/lab-case-task.generator';
|
|
|
|
const envPath = path.join(__dirname, '..', '.env');
|
|
config({ path: envPath });
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
console.error('regenerate-lab-tasks is not allowed in production');
|
|
process.exit(1);
|
|
}
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
const cases = await prisma.labCase.findMany({
|
|
where: {
|
|
sentAt: { not: null },
|
|
toothProsthesis: { some: {} },
|
|
},
|
|
select: {
|
|
id: true,
|
|
treatment: { select: { organization: { select: { owner: { select: { language: true } } } } } },
|
|
},
|
|
});
|
|
|
|
console.log(`Regenerating tasks for ${cases.length} sent case(s)...`);
|
|
|
|
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 } });
|
|
const created = await prisma.$transaction((tx) =>
|
|
generateLabCaseTasks(tx, labCase.id, locale),
|
|
);
|
|
total += created;
|
|
console.log(` - ${labCase.id}: ${created} task(s)`);
|
|
}
|
|
|
|
console.log(`Done. ${total} task(s) created.`);
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|