Add prisma seed tests and refactor seed script
This commit is contained in:
117
prisma/seed.ts
117
prisma/seed.ts
@@ -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();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user