Files
dashboard-desaplus-noc/prisma/seeders/seed-auth.ts
nico c216fa074d [darmasaba-dashboard][2026-03-27] feat: complete all seeders and update Phase 2 schema
Schema Updates:
- Added fields to Umkm model (name, owner, productType, description, timestamps)
- Added fields to Posyandu model (name, location, schedule, type, timestamps)
- Added fields to SecurityReport model (reportNumber, title, description, location, reportedBy, status, timestamps)
- Added fields to EmploymentRecord model (companyName, position, startDate, endDate, isActive, timestamps)
- Added fields to PopulationDynamic model (type, residentName, eventDate, description, timestamps)
- Added fields to BudgetTransaction model (transactionNumber, type, category, amount, description, date, timestamps)
- Added fields to HealthRecord model (type, notes, timestamps)

New Seeders:
- seed-discussions.ts: Documents, Discussions, DivisionMetrics
- seed-phase2.ts: UMKM, Posyandu, SecurityReports, EmploymentRecords, PopulationDynamics, BudgetTransactions

Enhanced Seeders:
- seed-auth.ts: Added seedApiKeys() function
- seed-public-services.ts: Added seedComplaintUpdates() and getComplaintIds()

New NPM Scripts:
- seed:documents - Seed documents and discussions
- seed:phase2 - Seed Phase 2+ features

All 33 Prisma models now have seeder coverage (82% direct, 12% stubs, 6% auto-managed)

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-03-27 14:05:15 +08:00

162 lines
3.3 KiB
TypeScript

import "dotenv/config";
import { hash } from "bcryptjs";
import { generateId } from "better-auth";
import { PrismaClient } from "../../generated/prisma";
const prisma = new PrismaClient();
/**
* Seed Admin User
* Creates or updates the admin user account
*/
export async function seedAdminUser() {
const adminEmail = process.env.ADMIN_EMAIL || "admin@example.com";
const adminPassword = process.env.ADMIN_PASSWORD || "admin123";
console.log(`Checking admin user: ${adminEmail}`);
const existingUser = await prisma.user.findUnique({
where: { email: adminEmail },
});
if (existingUser) {
if (existingUser.role !== "admin") {
await prisma.user.update({
where: { email: adminEmail },
data: { role: "admin" },
});
console.log("Updated existing user to admin role.");
}
return existingUser.id;
}
const hashedPassword = await hash(adminPassword, 12);
const userId = generateId();
await prisma.user.create({
data: {
id: userId,
email: adminEmail,
name: "Admin Desa Darmasaba",
role: "admin",
emailVerified: true,
accounts: {
create: {
id: generateId(),
accountId: userId,
providerId: "credential",
password: hashedPassword,
},
},
},
});
console.log(`✅ Admin user created: ${adminEmail}`);
return userId;
}
/**
* Seed Demo Users
* Creates demo users for testing (user, moderator roles)
*/
export async function seedDemoUsers() {
const demoUsers = [
{
email: "demo1@example.com",
name: "Demo User 1",
password: "demo123",
role: "user",
},
{
email: "demo2@example.com",
name: "Demo User 2",
password: "demo123",
role: "user",
},
{
email: "moderator@example.com",
name: "Moderator Desa",
password: "demo123",
role: "moderator",
},
];
console.log("Seeding Demo Users...");
for (const demo of demoUsers) {
const existingUser = await prisma.user.findUnique({
where: { email: demo.email },
});
if (existingUser) {
console.log(`⏭️ Demo user exists: ${demo.email}`);
continue;
}
const hashedPassword = await hash(demo.password, 12);
const userId = generateId();
await prisma.user.create({
data: {
id: userId,
email: demo.email,
name: demo.name,
role: demo.role,
emailVerified: true,
accounts: {
create: {
id: generateId(),
accountId: userId,
providerId: "credential",
password: hashedPassword,
},
},
},
});
console.log(`✅ Demo user created: ${demo.email}`);
}
}
/**
* Seed API Keys
* Creates sample API keys for testing API access
*/
export async function seedApiKeys(adminId: string) {
console.log("Seeding API Keys...");
const existingKeys = await prisma.apiKey.findMany({
where: { userId: adminId },
});
if (existingKeys.length > 0) {
console.log("⏭️ API keys already exist, skipping");
return;
}
const apiKeys = [
{
name: "Development Key",
key: "dev_key_" + generateId(),
userId: adminId,
isActive: true,
expiresAt: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year
},
{
name: "Production Key",
key: "prod_key_" + generateId(),
userId: adminId,
isActive: true,
expiresAt: null,
},
];
for (const apiKey of apiKeys) {
await prisma.apiKey.create({
data: apiKey,
});
}
console.log("✅ API Keys seeded successfully");
}