Fixed seed.ts and wrote a SQL file for calculating totalHours and totalPrice for a work order

This commit is contained in:
lucast committed 2026-11-23 15:43:53 +01:00
1 parent 98fb8d2da2
commit 48b8d37e8f
3 files changed
+48 -17

No files matched your search

+22 -17
View File
@@ -64,8 +64,8 @@ async function main() {
));
const mechanicData = [
{ name: "Sofie Nielsen", hourlyRate: 425, skills: ["Diagnostics", "Engine"] },
{ name: "Lars Holm", hourlyRate: 390, skills: ["Brakes", "Diagnostics"] },
{ name: "Sofie Nielsen", hourlyRate: 42_500, skills: ["Diagnostics", "Engine"] },
{ name: "Lars Holm", hourlyRate: 39_000, skills: ["Brakes", "Diagnostics"] },
];
const mechanics = await Promise.all(mechanicData.map(async (data) => {
let mechanic = await prisma.mechanic.findFirst({ where: { name: data.name } });
@@ -81,12 +81,17 @@ async function main() {
const supplier = await prisma.supplier.findFirst({ where: { name: "Nordic Auto Parts" } })
?? await prisma.supplier.create({ data: { name: "Nordic Auto Parts" } });
const [oilFilter, brakePads] = await Promise.all([
const [oilFilter, airFilter, brakePads] = await Promise.all([
prisma.sparePart.upsert({
where: { supplierId_partNumber: { supplierId: supplier.id, partNumber: "OF-204" } },
update: {},
create: { name: "Oil filter", partNumber: "OF-204", supplierId: supplier.id },
}),
prisma.sparePart.upsert({
where: { supplierId_partNumber: { supplierId: supplier.id, partNumber: "AF-310" } },
update: {},
create: { name: "Air filter", partNumber: "AF-310", supplierId: supplier.id },
}),
prisma.sparePart.upsert({
where: { supplierId_partNumber: { supplierId: supplier.id, partNumber: "BP-118" } },
update: {},
@@ -105,8 +110,9 @@ async function main() {
workOrder = await prisma.workOrder.create({ data });
}
const mechanic = mechanics[index];
if (mechanic) {
const assignedMechanics = index === 0 ? mechanics : [mechanics[index]];
for (const mechanic of assignedMechanics) {
if (!mechanic) continue;
await prisma.workOrderToMechanic.upsert({
where: { mechanicId_workOrderId: { mechanicId: mechanic.id, workOrderId: workOrder.id } },
update: {},
@@ -114,23 +120,22 @@ async function main() {
});
}
const part = index === 0 ? oilFilter : brakePads;
await prisma.workOrderToSparePart.upsert({
where: { sparePartId_workOrderId: { sparePartId: part.id, workOrderId: workOrder.id } },
update: {},
create: {
sparePartId: part.id,
workOrderId: workOrder.id,
quantity: 1,
pricePerUnit: index === 0 ? 120 : 680,
},
});
const partAssignments = index === 0
? [{ part: oilFilter, quantity: 2, pricePerUnit: 12_000 }, { part: airFilter, quantity: 1, pricePerUnit: 18_000 }]
: [{ part: brakePads, quantity: 1, pricePerUnit: 68_000 }];
await Promise.all(partAssignments.map(({ part, quantity, pricePerUnit }) =>
prisma.workOrderToSparePart.upsert({
where: { sparePartId_workOrderId: { sparePartId: part.id, workOrderId: workOrder.id } },
update: { quantity, pricePerUnit },
create: { sparePartId: part.id, workOrderId: workOrder.id, quantity, pricePerUnit },
}),
));
if (index === 0) {
await prisma.invoice.upsert({
where: { workOrderId: workOrder.id },
update: {},
create: { workOrderId: workOrder.id, total: 1_250 },
create: { workOrderId: workOrder.id, total: 125_000 },
});
}
}