45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import argon2 from "argon2";
|
|
import { PrismaClient, UserRole } from "@prisma/client";
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const tenantName = process.env.SEED_TENANT ?? "Default Tenant";
|
|
const email = process.env.SEED_ADMIN_EMAIL ?? process.env.SEED_EMAIL ?? "admin@example.com";
|
|
const password = process.env.SEED_ADMIN_PASSWORD ?? process.env.SEED_PASSWORD ?? "change-me-please";
|
|
const role = (process.env.SEED_ROLE as UserRole | undefined) ?? "ADMIN";
|
|
|
|
const main = async () => {
|
|
const hash = await argon2.hash(password);
|
|
const tenantId = process.env.SEED_TENANT_ID ?? "seed-tenant";
|
|
const tenant = await prisma.tenant.upsert({
|
|
where: { id: tenantId },
|
|
update: { name: tenantName },
|
|
create: { id: tenantId, name: tenantName }
|
|
});
|
|
|
|
const existing = await prisma.user.findUnique({ where: { email } });
|
|
if (existing) {
|
|
await prisma.user.update({
|
|
where: { email },
|
|
data: { password: hash, role, tenantId: tenant.id }
|
|
});
|
|
process.stdout.write("Seed admin updated.\n");
|
|
return;
|
|
}
|
|
|
|
await prisma.user.create({
|
|
data: { tenantId: tenant.id, email, password: hash, role }
|
|
});
|
|
|
|
process.stdout.write("Seeded admin user.\n");
|
|
};
|
|
|
|
main()
|
|
.catch((err) => {
|
|
process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|