mosh-helpdesk/server/prisma/seed.ts

86 lines
2.2 KiB
TypeScript
Raw Normal View History

import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "../src/generated/prisma/client";
import { Role } from "../src/generated/prisma/client";
import { hashPassword } from "better-auth/crypto";
2026-03-02 17:48:06 +01:00
import { AI_AGENT_ID } from "core/constants/ai-agent.ts";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
async function main() {
const email = process.env.SEED_ADMIN_EMAIL;
const password = process.env.SEED_ADMIN_PASSWORD;
if (!email || !password) {
throw new Error(
"SEED_ADMIN_EMAIL and SEED_ADMIN_PASSWORD must be set in .env"
);
}
2026-03-02 17:48:06 +01:00
const now = new Date();
// Seed admin user
const existingAdmin = await prisma.user.findUnique({ where: { email } });
if (existingAdmin) {
console.log(`Admin user ${email} already exists — skipping.`);
2026-03-02 17:48:06 +01:00
} else {
const hashedPassword = await hashPassword(password);
const userId = crypto.randomUUID();
2026-03-02 17:48:06 +01:00
await prisma.$transaction([
prisma.user.create({
data: {
id: userId,
name: "Admin",
email,
emailVerified: false,
role: Role.admin,
createdAt: now,
updatedAt: now,
},
}),
prisma.account.create({
data: {
id: crypto.randomUUID(),
accountId: userId,
providerId: "credential",
userId,
password: hashedPassword,
createdAt: now,
updatedAt: now,
},
}),
]);
console.log(`Admin user ${email} created successfully.`);
}
2026-03-02 17:48:06 +01:00
// Seed AI agent user
const existingAI = await prisma.user.findUnique({
where: { id: AI_AGENT_ID },
});
if (existingAI) {
console.log("AI agent user already exists — skipping.");
} else {
await prisma.user.create({
data: {
2026-03-02 17:48:06 +01:00
id: AI_AGENT_ID,
name: "AI",
email: "ai@helpdesk.local",
emailVerified: false,
2026-03-02 17:48:06 +01:00
role: Role.agent,
createdAt: now,
updatedAt: now,
},
2026-03-02 17:48:06 +01:00
});
console.log("AI agent user created successfully.");
}
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(() => prisma.$disconnect());