111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
import { PrismaClient, Role, ZoneCode } from '@prisma/client';
|
|
import bcrypt from 'bcrypt';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const SEED_USERS: Array<{
|
|
email: string;
|
|
fullName: string;
|
|
role: Role;
|
|
passwordEnv: string;
|
|
passwordFallback: string;
|
|
}> = [
|
|
{
|
|
email: 'manager@local',
|
|
fullName: 'Иван Управляющий',
|
|
role: Role.MANAGER,
|
|
passwordEnv: 'SEED_PASSWORD_MANAGER',
|
|
passwordFallback: 'manager123',
|
|
},
|
|
{
|
|
email: 'senior@local',
|
|
fullName: 'Мария Старший Администратор',
|
|
role: Role.SENIOR_ADMIN,
|
|
passwordEnv: 'SEED_PASSWORD_SENIOR',
|
|
passwordFallback: 'senior123',
|
|
},
|
|
{
|
|
email: 'security@local',
|
|
fullName: 'Пётр Безопасность',
|
|
role: Role.SECURITY,
|
|
passwordEnv: 'SEED_PASSWORD_SECURITY',
|
|
passwordFallback: 'security123',
|
|
},
|
|
{
|
|
email: 'admin@local',
|
|
fullName: 'Анна Админ Системы',
|
|
role: Role.SYSADMIN,
|
|
passwordEnv: 'SEED_PASSWORD_SYSADMIN',
|
|
passwordFallback: 'admin123',
|
|
},
|
|
];
|
|
|
|
const SEED_ZONES: Array<{ code: ZoneCode; name: string }> = [
|
|
{ code: ZoneCode.A, name: 'Вход в клинику' },
|
|
{ code: ZoneCode.B, name: 'Коридор / зона ожидания' },
|
|
{ code: ZoneCode.C, name: 'Стойка рецепции' },
|
|
{ code: ZoneCode.D, name: 'Перед кабинетом врача' },
|
|
{ code: ZoneCode.E, name: 'В кабинете врача' },
|
|
];
|
|
|
|
const SEED_CAMERAS: Array<{ name: string; zoneCode: ZoneCode }> = [
|
|
{ name: 'cam-entrance', zoneCode: ZoneCode.A },
|
|
{ name: 'cam-corridor', zoneCode: ZoneCode.B },
|
|
// На рецепции 4 рабочих места — отдельная камера на каждое (С1…С4).
|
|
{ name: 'cam-reception-1', zoneCode: ZoneCode.C },
|
|
{ name: 'cam-reception-2', zoneCode: ZoneCode.C },
|
|
{ name: 'cam-reception-3', zoneCode: ZoneCode.C },
|
|
{ name: 'cam-reception-4', zoneCode: ZoneCode.C },
|
|
{ name: 'cam-doctor-waiting', zoneCode: ZoneCode.D },
|
|
{ name: 'cam-doctor-office', zoneCode: ZoneCode.E },
|
|
];
|
|
|
|
async function main() {
|
|
console.log('🌱 Seeding reception database...');
|
|
|
|
// Users
|
|
for (const u of SEED_USERS) {
|
|
const password = process.env[u.passwordEnv] ?? u.passwordFallback;
|
|
const passwordHash = await bcrypt.hash(password, 10);
|
|
await prisma.user.upsert({
|
|
where: { email: u.email },
|
|
update: { fullName: u.fullName, role: u.role, passwordHash, isActive: true },
|
|
create: { email: u.email, fullName: u.fullName, role: u.role, passwordHash, isActive: true },
|
|
});
|
|
console.log(` ✓ user ${u.email} (${u.role})`);
|
|
}
|
|
|
|
// Zones
|
|
for (const z of SEED_ZONES) {
|
|
await prisma.zone.upsert({
|
|
where: { code: z.code },
|
|
update: { name: z.name },
|
|
create: z,
|
|
});
|
|
console.log(` ✓ zone ${z.code} — ${z.name}`);
|
|
}
|
|
|
|
// Cameras (bound to zones)
|
|
for (const c of SEED_CAMERAS) {
|
|
const zone = await prisma.zone.findUniqueOrThrow({ where: { code: c.zoneCode } });
|
|
await prisma.camera.upsert({
|
|
where: { name: c.name },
|
|
update: { zoneId: zone.id },
|
|
create: { name: c.name, zoneId: zone.id },
|
|
});
|
|
console.log(` ✓ camera ${c.name} → zone ${c.zoneCode}`);
|
|
}
|
|
|
|
console.log('✅ Seed complete.');
|
|
}
|
|
|
|
main()
|
|
.then(async () => {
|
|
await prisma.$disconnect();
|
|
})
|
|
.catch(async (e) => {
|
|
console.error(e);
|
|
await prisma.$disconnect();
|
|
process.exit(1);
|
|
});
|