60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
/* eslint-disable no-console */
|
|
import { PrismaClient, AdminRole } from '@prisma/client';
|
|
import * as bcrypt from 'bcryptjs';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const DEFAULT_TENANT_SLUG = 'default';
|
|
const SEED_ADMIN_EMAIL = process.env['SEED_ADMIN_EMAIL'] ?? 'admin@tower.local';
|
|
const SEED_ADMIN_PASSWORD = process.env['SEED_ADMIN_PASSWORD'] ?? 'tower_dev_password';
|
|
const SUPER_ADMIN_EMAIL = process.env['SUPER_ADMIN_EMAIL'] ?? 'super@tower.local';
|
|
const SUPER_ADMIN_PASSWORD = process.env['SUPER_ADMIN_PASSWORD'] ?? 'super_dev_password';
|
|
const BCRYPT_ROUNDS = Number(process.env['BCRYPT_ROUNDS'] ?? '10');
|
|
|
|
async function main(): Promise<void> {
|
|
const tenant = await prisma.tenant.upsert({
|
|
where: { slug: DEFAULT_TENANT_SLUG },
|
|
update: {},
|
|
create: { slug: DEFAULT_TENANT_SLUG, name: 'Default Tenant' },
|
|
});
|
|
|
|
const passwordHash = await bcrypt.hash(SEED_ADMIN_PASSWORD, BCRYPT_ROUNDS);
|
|
const admin = await prisma.admin.upsert({
|
|
where: { tenantId_email: { tenantId: tenant.id, email: SEED_ADMIN_EMAIL } },
|
|
update: { passwordHash, role: AdminRole.OWNER },
|
|
create: {
|
|
tenantId: tenant.id,
|
|
email: SEED_ADMIN_EMAIL,
|
|
passwordHash,
|
|
role: AdminRole.OWNER,
|
|
},
|
|
});
|
|
|
|
const superPasswordHash = await bcrypt.hash(SUPER_ADMIN_PASSWORD, BCRYPT_ROUNDS);
|
|
const superAdmin = await prisma.superAdmin.upsert({
|
|
where: { email: SUPER_ADMIN_EMAIL },
|
|
update: { passwordHash: superPasswordHash },
|
|
create: {
|
|
email: SUPER_ADMIN_EMAIL,
|
|
passwordHash: superPasswordHash,
|
|
name: 'Super Admin',
|
|
},
|
|
});
|
|
|
|
console.log('Seed complete:');
|
|
console.log(` Tenant: ${tenant.slug} (${tenant.id})`);
|
|
console.log(` Admin: ${admin.email} (${admin.id}) role=${admin.role}`);
|
|
console.log(` SuperAdmin: ${superAdmin.email} (${superAdmin.id})`);
|
|
console.log(` Password: ${SEED_ADMIN_PASSWORD} (dev only — change for production)`);
|
|
console.log(` Super pwd: ${SUPER_ADMIN_PASSWORD} (dev only — change for production)`);
|
|
}
|
|
|
|
main()
|
|
.catch((err) => {
|
|
console.error('Seed failed:', err);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|