metallkart-erp/scripts/seed-test-supervisor.ts
louis 11cc64698e test(supervisor): 10/10 detectors validated + cleanup complete
Seed 10 tagged cases (D1-D10), triggered via admin route in digest
mode: 10 AgentAction EXECUTED, 25 NotificationDelivery across 4
channels (ERP_IN_APP/PWA_PUSH/TELEGRAM/EMAIL). Escalation chain
validated. All test data cleaned up (81 rows deleted, 0 remaining).
Override function kept for future test runs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-29 02:26:35 +04:00

324 lines
9.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient } from '@prisma/client';
const TAG = 'TEST-SUPERVISOR-280526';
const LOUIS_ID = 'ebdd4d55-9aeb-45c3-a0b4-577dafbab887';
const prisma = new PrismaClient();
function daysAgo(n: number): Date {
const d = new Date();
d.setDate(d.getDate() - n);
return d;
}
async function main() {
const existing = await prisma.order.count({ where: { notes: { contains: TAG } } });
if (existing > 0) {
console.log(`[SKIP] ${existing} orders already tagged with ${TAG}. Run cleanup first.`);
process.exit(0);
}
console.log(`=== Seed ${TAG} ===`);
// --- Shared entities ---
const client = await prisma.client.create({
data: {
companyName: `ООО Тест Супервайзер ${TAG}`,
inn: '7799280501',
notes: TAG,
},
});
console.log(`Client #${client.id}`);
const supplier = await prisma.supplier.create({
data: {
companyName: `ООО Тест Поставщик ${TAG}`,
inn: '7799280502',
notes: TAG,
},
});
console.log(`Supplier #${supplier.id}`);
// --- D1: Order AWAITING_ESTIMATE sans smeta ---
const o1 = await prisma.order.create({
data: {
orderCode: `${TAG}-D1`,
clientId: client.id,
productName: 'Тест D1 ожидание сметы',
status: 'AWAITING_ESTIMATE',
managerId: LOUIS_ID,
notes: TAG,
createdAt: daysAgo(3),
},
});
await prisma.orderStatusHistory.create({
data: { orderId: o1.id, fromStatus: 'DRAFT', toStatus: 'AWAITING_ESTIMATE', changedById: LOUIS_ID, createdAt: daysAgo(3) },
});
console.log(`D1: Order #${o1.id} (${o1.orderCode}) AWAITING_ESTIMATE`);
// --- D2: Order + Smeta FINANCIAL_REVIEW ---
const o2 = await prisma.order.create({
data: {
orderCode: `${TAG}-D2`,
clientId: client.id,
productName: 'Тест D2 смета на проверке',
status: 'AWAITING_ESTIMATE',
managerId: LOUIS_ID,
notes: TAG,
},
});
await prisma.smeta.create({
data: {
orderId: o2.id,
status: 'FINANCIAL_REVIEW',
submittedAt: daysAgo(3),
submittedById: LOUIS_ID,
},
});
console.log(`D2: Order #${o2.id} + Smeta FINANCIAL_REVIEW`);
// --- D3: Order LAUNCHED + Smeta DRAFT (not APPROVED/LOCKED) ---
const o3 = await prisma.order.create({
data: {
orderCode: `${TAG}-D3`,
clientId: client.id,
productName: 'Тест D3 запуск без сметы',
status: 'LAUNCHED',
managerId: LOUIS_ID,
notes: TAG,
},
});
await prisma.orderStatusHistory.create({
data: { orderId: o3.id, fromStatus: 'DRAFT', toStatus: 'LAUNCHED', changedById: LOUIS_ID, createdAt: daysAgo(4) },
});
await prisma.smeta.create({
data: { orderId: o3.id, status: 'DRAFT' },
});
console.log(`D3: Order #${o3.id} LAUNCHED + Smeta DRAFT`);
// --- D4: Tender DRAFT stale ---
const o4 = await prisma.order.create({
data: {
orderCode: `${TAG}-D4`,
clientId: client.id,
productName: 'Тест D4 тендер черновик',
status: 'IN_PRODUCTION',
managerId: LOUIS_ID,
notes: TAG,
},
});
const t4 = await prisma.tender.create({
data: {
referenceCode: `${TAG}-T4`,
createdById: LOUIS_ID,
status: 'DRAFT',
createdAt: daysAgo(3),
},
});
await prisma.purchaseRequirement.create({
data: {
materialCategory: 'METAL',
description: `${TAG} PR D4`,
quantity: 10,
orderId: o4.id,
tenderId: t4.id,
status: 'IN_TENDER',
notes: TAG,
},
});
console.log(`D4: Tender #${t4.id} DRAFT (${t4.referenceCode})`);
// --- D5: Tender SENT sans offres ---
const o5 = await prisma.order.create({
data: {
orderCode: `${TAG}-D5`,
clientId: client.id,
productName: 'Тест D5 тендер без предложений',
status: 'IN_PRODUCTION',
managerId: LOUIS_ID,
notes: TAG,
},
});
const t5 = await prisma.tender.create({
data: {
referenceCode: `${TAG}-T5`,
createdById: LOUIS_ID,
status: 'SENT',
createdAt: daysAgo(5),
updatedAt: daysAgo(4),
},
});
await prisma.purchaseRequirement.create({
data: {
materialCategory: 'FASTENERS',
description: `${TAG} PR D5`,
quantity: 50,
orderId: o5.id,
tenderId: t5.id,
status: 'IN_TENDER',
notes: TAG,
},
});
console.log(`D5: Tender #${t5.id} SENT no offers (${t5.referenceCode})`);
// --- D6: PO DRAFT stale ---
const o6 = await prisma.order.create({
data: {
orderCode: `${TAG}-D6`,
clientId: client.id,
productName: 'Тест D6 ЗП черновик',
status: 'IN_PRODUCTION',
managerId: LOUIS_ID,
notes: TAG,
},
});
const po6 = await prisma.purchaseOrder.create({
data: {
orderCode: `${TAG}-PO6`,
supplierId: supplier.id,
items: [{ description: 'Test item D6', qty: 1, price: 1000 }],
totalAmount: 1000,
createdById: LOUIS_ID,
clientOrderId: o6.id,
status: 'DRAFT',
createdAt: daysAgo(3),
notes: TAG,
},
});
console.log(`D6: PO #${po6.id} DRAFT (${po6.orderCode})`);
// --- D7: PO CONFIRMED emailSentAt=null ---
const o7 = await prisma.order.create({
data: {
orderCode: `${TAG}-D7`,
clientId: client.id,
productName: 'Тест D7 ЗП подтверждён',
status: 'IN_PRODUCTION',
managerId: LOUIS_ID,
notes: TAG,
},
});
const po7 = await prisma.purchaseOrder.create({
data: {
orderCode: `${TAG}-PO7`,
supplierId: supplier.id,
items: [{ description: 'Test item D7', qty: 5, price: 2000 }],
totalAmount: 10000,
createdById: LOUIS_ID,
clientOrderId: o7.id,
status: 'CONFIRMED',
createdAt: daysAgo(3),
updatedAt: daysAgo(3),
notes: TAG,
},
});
console.log(`D7: PO #${po7.id} CONFIRMED no email (${po7.orderCode})`);
// --- D8: PO SENT past delivery ---
const o8 = await prisma.order.create({
data: {
orderCode: `${TAG}-D8`,
clientId: client.id,
productName: 'Тест D8 ЗП отправлен',
status: 'IN_PRODUCTION',
managerId: LOUIS_ID,
notes: TAG,
},
});
const po8 = await prisma.purchaseOrder.create({
data: {
orderCode: `${TAG}-PO8`,
supplierId: supplier.id,
items: [{ description: 'Test item D8', qty: 3, price: 5000 }],
totalAmount: 15000,
createdById: LOUIS_ID,
clientOrderId: o8.id,
status: 'SENT',
emailSentAt: daysAgo(7),
expectedDeliveryDate: daysAgo(5),
createdAt: daysAgo(7),
notes: TAG,
},
});
console.log(`D8: PO #${po8.id} SENT overdue (${po8.orderCode})`);
// --- D9: SDN DRAFT ---
const o9 = await prisma.order.create({
data: {
orderCode: `${TAG}-D9`,
clientId: client.id,
productName: 'Тест D9 ТТН не подписана',
status: 'IN_PRODUCTION',
managerId: LOUIS_ID,
notes: TAG,
},
});
const sdn9 = await prisma.supplierDeliveryNote.create({
data: {
number: `${TAG}-SDN9`,
signedDate: new Date(),
supplierId: supplier.id,
status: 'DRAFT',
createdAt: daysAgo(3),
notes: TAG,
},
});
console.log(`D9: SDN #${sdn9.id} DRAFT (ТТН${sdn9.number})`);
// --- D10: ClientInstallment overdue ---
const o10 = await prisma.order.create({
data: {
orderCode: `${TAG}-D10`,
clientId: client.id,
productName: 'Тест D10 просрочка оплаты',
status: 'SHIPPED',
managerId: LOUIS_ID,
notes: TAG,
},
});
const inv10 = await prisma.clientInvoice.create({
data: {
orderId: o10.id,
invoiceNumber: `${TAG}-INV10`,
invoiceDate: daysAgo(20),
amount: 500000,
status: 'ISSUED',
},
});
await prisma.clientInstallment.create({
data: {
orderId: o10.id,
invoiceId: inv10.id,
amount: 500000,
dueDate: daysAgo(10),
trigger: 'ORDER_DATE',
label: `${TAG} Предоплата D10`,
status: 'PENDING',
},
});
console.log(`D10: Installment overdue 10d (Order #${o10.id})`);
// --- Inventory ---
const counts = await Promise.all([
prisma.order.count({ where: { notes: { contains: TAG } } }),
prisma.smeta.count({ where: { order: { notes: { contains: TAG } } } }),
prisma.purchaseRequirement.count({ where: { notes: { contains: TAG } } }),
prisma.tender.count({ where: { referenceCode: { contains: TAG } } }),
prisma.purchaseOrder.count({ where: { notes: { contains: TAG } } }),
prisma.supplierDeliveryNote.count({ where: { notes: { contains: TAG } } }),
prisma.clientInvoice.count({ where: { invoiceNumber: { contains: TAG } } }),
prisma.clientInstallment.count({ where: { label: { contains: TAG } } }),
prisma.client.count({ where: { notes: { contains: TAG } } }),
prisma.supplier.count({ where: { notes: { contains: TAG } } }),
]);
console.log('\n=== Inventory ===');
['orders', 'smetas', 'PRs', 'tenders', 'POs', 'SDNs', 'invoices', 'installments', 'clients', 'suppliers']
.forEach((t, i) => console.log(` ${t}: ${counts[i]}`));
console.log(` TOTAL: ${counts.reduce((a, b) => a + b, 0)}`);
console.log(`\n=== Seed complete. Tag: ${TAG} ===`);
}
main()
.catch((e) => { console.error(e); process.exit(1); })
.finally(() => prisma.$disconnect());