65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { hash } from "bcryptjs";
|
|
import { prisma } from "../lib/prisma";
|
|
import { DEFAULT_SETTINGS } from "../lib/constants";
|
|
import { assertSecureSeedConfig } from "../lib/security/config-guard";
|
|
|
|
function slugify(value: string) {
|
|
return value
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, "-")
|
|
.replace(/(^-|-$)+/g, "");
|
|
}
|
|
|
|
async function main() {
|
|
assertSecureSeedConfig();
|
|
|
|
const adminName = process.env.ADMIN_NAME ?? "CalBook Admin";
|
|
const adminEmail = process.env.ADMIN_EMAIL ?? "admin@calbook.local";
|
|
const adminPassword = process.env.ADMIN_PASSWORD!;
|
|
const slug = slugify(adminName) || "admin";
|
|
|
|
const existingSlug = await prisma.user.findUnique({ where: { slug } });
|
|
const fallbackSlug = existingSlug && existingSlug.email !== adminEmail ? `${slug}-1` : slug;
|
|
|
|
const admin = await prisma.user.upsert({
|
|
where: { email: adminEmail },
|
|
create: {
|
|
name: adminName,
|
|
email: adminEmail,
|
|
hashedPassword: await hash(adminPassword, 12),
|
|
role: "ADMIN",
|
|
slug: fallbackSlug,
|
|
timezone: process.env.DEFAULT_TIMEZONE ?? "Europe/Berlin",
|
|
isActive: true,
|
|
bio: "Ich begleite dich durch dein Gespräch und kläre die nächsten Schritte."
|
|
},
|
|
update: {
|
|
name: adminName,
|
|
hashedPassword: await hash(adminPassword, 12),
|
|
role: "ADMIN",
|
|
isActive: true,
|
|
timezone: process.env.DEFAULT_TIMEZONE ?? "Europe/Berlin",
|
|
bio: "Ich begleite dich durch dein Gespräch und kläre die nächsten Schritte."
|
|
}
|
|
});
|
|
|
|
for (const [key, value] of Object.entries(DEFAULT_SETTINGS)) {
|
|
await prisma.setting.upsert({
|
|
where: { key },
|
|
create: { key, value },
|
|
update: { value }
|
|
});
|
|
}
|
|
|
|
console.log(`Seed abgeschlossen. Admin: ${admin.email}`);
|
|
}
|
|
|
|
main()
|
|
.catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|