import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { const techPeople = await prisma.employee.findMany({ where: { department: { deptType: 'TECH' } }, select: { name: true, department: { select: { deptType: true, name: true } } }, }); const names = techPeople.map((e) => e.name).filter((n) => n && n.length >= 2); const rows = await prisma.expenseClaim.findMany({ where: { tenantId: '1', costType: 'PROJECT', budgetCategory: 'TRAVEL' }, include: { applicant: { select: { employee: { select: { name: true, department: { select: { deptType: true } } } } } }, lines: { select: { remark: true } }, }, }); let n = 0; for (const row of rows) { const dept = row.applicant.employee?.department?.deptType || ''; const text = [row.purpose, row.remark, ...row.lines.map((l) => l.remark)].filter(Boolean).join(' '); const forTech = names.some((name) => text.includes(name)); if (dept !== 'TECH' && !forTech) continue; await prisma.expenseClaim.update({ where: { id: row.id }, data: { budgetCategory: 'TECH' } }); n += 1; } console.log(`reclassified ${n} / ${rows.length} travel expenses into tech budget; tech staff ${names.length}`); } main() .then(async () => prisma.$disconnect()) .catch(async (e) => { console.error(e); await prisma.$disconnect(); process.exit(1); });