- Override function in supervisorTick: routes all reminders to SUPERVISOR_TEST_TARGET_USER_ID when both env vars are set - Seed script creates 10 tagged cases (D1-D10), one per detector - Cleanup script deletes all tagged data + agent artifacts - Tag: TEST-SUPERVISOR-280526 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
118 lines
4.3 KiB
TypeScript
118 lines
4.3 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
|
|
const TAG = 'TEST-SUPERVISOR-280526';
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log(`=== Cleanup ${TAG} ===\n`);
|
|
|
|
const taggedOrderIds = (await prisma.order.findMany({ where: { notes: { contains: TAG } }, select: { id: true } })).map(o => o.id);
|
|
const taggedTenderIds = (await prisma.tender.findMany({ where: { referenceCode: { contains: TAG } }, select: { id: true } })).map(t => t.id);
|
|
const taggedPoIds = (await prisma.purchaseOrder.findMany({ where: { notes: { contains: TAG } }, select: { id: true } })).map(p => p.id);
|
|
const taggedSdnIds = (await prisma.supplierDeliveryNote.findMany({ where: { notes: { contains: TAG } }, select: { id: true } })).map(s => s.id);
|
|
|
|
async function del(label: string, fn: () => Promise<{ count: number }>) {
|
|
const r = await fn();
|
|
console.log(` ${label}: ${r.count} deleted`);
|
|
return r.count;
|
|
}
|
|
|
|
let total = 0;
|
|
|
|
// Agent data (orphans from supervisor reminders)
|
|
if (taggedOrderIds.length > 0 || taggedPoIds.length > 0 || taggedTenderIds.length > 0 || taggedSdnIds.length > 0) {
|
|
const entityIds = [...taggedOrderIds, ...taggedPoIds, ...taggedTenderIds, ...taggedSdnIds];
|
|
total += await del('AgentEngagement', () => prisma.agentEngagement.deleteMany({
|
|
where: { agentAction: { relatedEntityId: { in: entityIds } } },
|
|
}));
|
|
total += await del('AgentAuditLog', () => prisma.agentAuditLog.deleteMany({
|
|
where: { agentAction: { relatedEntityId: { in: entityIds } } },
|
|
}));
|
|
total += await del('AgentAction', () => prisma.agentAction.deleteMany({
|
|
where: { relatedEntityId: { in: entityIds } },
|
|
}));
|
|
}
|
|
|
|
// ClientInstallment
|
|
total += await del('ClientInstallment', () => prisma.clientInstallment.deleteMany({
|
|
where: { label: { contains: TAG } },
|
|
}));
|
|
|
|
// ClientInvoice
|
|
total += await del('ClientInvoice', () => prisma.clientInvoice.deleteMany({
|
|
where: { invoiceNumber: { contains: TAG } },
|
|
}));
|
|
|
|
// SDN
|
|
total += await del('SupplierDeliveryNote', () => prisma.supplierDeliveryNote.deleteMany({
|
|
where: { notes: { contains: TAG } },
|
|
}));
|
|
|
|
// PO lines (if any)
|
|
if (taggedPoIds.length > 0) {
|
|
total += await del('PurchaseOrderLine', () => prisma.purchaseOrderLine.deleteMany({
|
|
where: { purchaseOrderId: { in: taggedPoIds } },
|
|
}));
|
|
}
|
|
|
|
// PO
|
|
total += await del('PurchaseOrder', () => prisma.purchaseOrder.deleteMany({
|
|
where: { notes: { contains: TAG } },
|
|
}));
|
|
|
|
// PR
|
|
total += await del('PurchaseRequirement', () => prisma.purchaseRequirement.deleteMany({
|
|
where: { notes: { contains: TAG } },
|
|
}));
|
|
|
|
// Tender
|
|
total += await del('Tender', () => prisma.tender.deleteMany({
|
|
where: { referenceCode: { contains: TAG } },
|
|
}));
|
|
|
|
// Smeta (linked to tagged orders)
|
|
if (taggedOrderIds.length > 0) {
|
|
total += await del('Smeta', () => prisma.smeta.deleteMany({
|
|
where: { orderId: { in: taggedOrderIds } },
|
|
}));
|
|
total += await del('OrderStatusHistory', () => prisma.orderStatusHistory.deleteMany({
|
|
where: { orderId: { in: taggedOrderIds } },
|
|
}));
|
|
}
|
|
|
|
// Orders
|
|
total += await del('Order', () => prisma.order.deleteMany({
|
|
where: { notes: { contains: TAG } },
|
|
}));
|
|
|
|
// Client + Supplier
|
|
total += await del('Client', () => prisma.client.deleteMany({
|
|
where: { notes: { contains: TAG } },
|
|
}));
|
|
total += await del('Supplier', () => prisma.supplier.deleteMany({
|
|
where: { notes: { contains: TAG } },
|
|
}));
|
|
|
|
// Verify zero
|
|
const remaining = await Promise.all([
|
|
prisma.order.count({ where: { notes: { contains: TAG } } }),
|
|
prisma.purchaseOrder.count({ where: { notes: { contains: TAG } } }),
|
|
prisma.tender.count({ where: { referenceCode: { contains: TAG } } }),
|
|
prisma.supplierDeliveryNote.count({ where: { notes: { contains: TAG } } }),
|
|
prisma.client.count({ where: { notes: { contains: TAG } } }),
|
|
prisma.supplier.count({ where: { notes: { contains: TAG } } }),
|
|
]);
|
|
const sum = remaining.reduce((a, b) => a + b, 0);
|
|
|
|
console.log(`\n=== Total deleted: ${total} | Remaining tagged: ${sum} ===`);
|
|
if (sum > 0) {
|
|
console.error('WARNING: Some tagged rows remain!');
|
|
process.exit(1);
|
|
}
|
|
console.log('Cleanup complete.');
|
|
}
|
|
|
|
main()
|
|
.catch((e) => { console.error(e); process.exit(1); })
|
|
.finally(() => prisma.$disconnect());
|