69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { config as loadEnv } from 'dotenv';
|
|
|
|
// Load repo-root .env so DATABASE_URL is visible when this script runs from
|
|
// any CWD (pnpm invokes it from packages/db).
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
loadEnv({ path: path.resolve(here, '../../../.env') });
|
|
|
|
const { PrismaClient, UserRole } = await import('@prisma/client');
|
|
const prisma = new PrismaClient();
|
|
|
|
const DEMO_TENANT_NAME = 'Demo Factory';
|
|
const DEMO_ADMIN_EMAIL = 'admin@demo.local';
|
|
|
|
const OPERATOR_EMAILS = ['op1@demo.local', 'op2@demo.local', 'op3@demo.local'];
|
|
|
|
const WORKSTATIONS = [
|
|
{ code: 'CTR04', name: 'Controlo 04', area: 'Montagem' },
|
|
{ code: 'QVN_RTL_2', name: 'Retificação Visual 2', area: 'Qualidade' },
|
|
{ code: 'MTG_01', name: 'Montagem 01', area: 'Montagem' },
|
|
];
|
|
|
|
async function main() {
|
|
// Idempotent: if a prior run created the demo tenant, wipe it and recreate.
|
|
// Cascade deletes on the relations handle the children.
|
|
const existing = await prisma.tenant.findFirst({ where: { name: DEMO_TENANT_NAME } });
|
|
if (existing) {
|
|
await prisma.tenant.delete({ where: { id: existing.id } });
|
|
}
|
|
|
|
const tenant = await prisma.tenant.create({
|
|
data: { name: DEMO_TENANT_NAME },
|
|
});
|
|
|
|
await prisma.user.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
email: DEMO_ADMIN_EMAIL,
|
|
role: UserRole.ADMIN,
|
|
},
|
|
});
|
|
|
|
await prisma.user.createMany({
|
|
data: OPERATOR_EMAILS.map((email) => ({
|
|
tenantId: tenant.id,
|
|
email,
|
|
role: UserRole.OPERATOR,
|
|
})),
|
|
});
|
|
|
|
await prisma.workstation.createMany({
|
|
data: WORKSTATIONS.map((ws) => ({ tenantId: tenant.id, ...ws })),
|
|
});
|
|
|
|
console.warn(
|
|
`Seed complete — tenant=${tenant.id} (${tenant.name}), admin=${DEMO_ADMIN_EMAIL}, operators=${OPERATOR_EMAILS.length}, workstations=${WORKSTATIONS.length}`,
|
|
);
|
|
}
|
|
|
|
main()
|
|
.catch((err) => {
|
|
console.error('Seed failed:', err);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|