Add prisma seed tests and refactor seed script

This commit is contained in:
bipproduction
2026-02-05 16:33:43 +08:00
parent 049bddeba1
commit 98b134e72a
7 changed files with 238 additions and 38 deletions

View File

@@ -1,32 +1,93 @@
import { prisma } from "@/server/lib/prisma";
const user = [
{
name: "wibu",
email: "wibu@bip.com",
password: "Production_123",
}
];
export async function seed() {
console.log("🌱 Starting seeding...");
; (async () => {
for (const u of user) {
await prisma.user.upsert({
where: { email: u.email },
create: u,
update: u,
// 1. Seed User from environment variables or defaults
const adminEmail = process.env.ADMIN_EMAIL || "admin@example.com";
const adminPassword = process.env.ADMIN_PASSWORD || "admin123";
const user = await prisma.user.upsert({
where: { email: adminEmail },
update: {},
create: {
name: "Administrator",
email: adminEmail,
password: adminPassword,
},
});
console.log(`✅ User seeded: ${user.email}`);
// 2. Seed Default API Key
const defaultKey = "wajs_sk_live_default_key_2026";
await prisma.apiKey.upsert({
where: { key: defaultKey },
update: {},
create: {
name: "Default API Key",
key: defaultKey,
userId: user.id,
description: "Auto-generated default API key",
},
});
console.log("✅ Default API Key seeded");
// 3. Seed Sample Webhook
const webhookUrl = "https://webhook.site/wajs-test";
const existingWebhook = await prisma.webHook.findFirst({
where: { url: webhookUrl },
});
if (!existingWebhook) {
await prisma.webHook.create({
data: {
name: "Sample Webhook",
url: webhookUrl,
description: "Test webhook for capturing events",
enabled: true,
method: "POST",
},
});
console.log("✅ Sample Webhook seeded");
} else {
console.log(" Sample Webhook already exists, skipping");
}
// 4. Seed Initial ChatFlow
const flowUrl = "initial-flow";
await prisma.chatFlows.upsert({
where: { flowUrl: flowUrl },
update: {},
create: {
flowUrl: flowUrl,
flows: {
nodes: [
{
id: "1",
type: "input",
data: { label: "Start" },
position: { x: 250, y: 5 },
},
],
edges: [],
},
active: true,
defaultFlow: "Main Flow",
},
});
console.log("✅ Initial ChatFlow seeded");
console.log("✨ Seeding completed successfully!");
return { user, defaultKey, webhookUrl, flowUrl };
}
if (import.meta.main) {
seed()
.catch((e) => {
console.error("❌ Seeding failed:", e);
process.exit(1);
})
console.log(`✅ User ${u.email} seeded successfully`)
}
})().catch((e) => {
console.error(e)
process.exit(1)
}).finally(() => {
console.log("✅ Seeding completed successfully ")
process.exit(0)
})
// use fix-code
.finally(async () => {
await prisma.$disconnect();
});
}