fix(header): fix missing Divider, Badge, IconUserShield and navigate
This commit is contained in:
403
prisma/seed.ts
403
prisma/seed.ts
@@ -1,150 +1,325 @@
|
||||
import "dotenv/config";
|
||||
import { hash } from "bcryptjs";
|
||||
import { generateId } from "better-auth";
|
||||
import { prisma } from "@/utils/db";
|
||||
import {
|
||||
ActivityStatus,
|
||||
ComplaintCategory,
|
||||
ComplaintStatus,
|
||||
EventType,
|
||||
Gender,
|
||||
Priority,
|
||||
PrismaClient,
|
||||
Religion,
|
||||
} from "../generated/prisma";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function seedAdminUser() {
|
||||
// Load environment variables
|
||||
const adminEmail = process.env.ADMIN_EMAIL;
|
||||
const adminEmail = process.env.ADMIN_EMAIL || "admin@example.com";
|
||||
const adminPassword = process.env.ADMIN_PASSWORD || "admin123";
|
||||
|
||||
if (!adminEmail) {
|
||||
console.log(
|
||||
"No ADMIN_EMAIL environment variable found. Skipping admin user creation.",
|
||||
);
|
||||
return;
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if admin user already exists
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: adminEmail },
|
||||
});
|
||||
const hashedPassword = await hash(adminPassword, 12);
|
||||
const userId = generateId();
|
||||
|
||||
if (existingUser) {
|
||||
// Update existing user to have admin role if they don't already
|
||||
if (existingUser.role !== "admin") {
|
||||
await prisma.user.update({
|
||||
where: { email: adminEmail },
|
||||
data: { role: "admin" },
|
||||
});
|
||||
console.log(`User with email ${adminEmail} updated to admin role.`);
|
||||
} else {
|
||||
console.log(`User with email ${adminEmail} already has admin role.`);
|
||||
}
|
||||
} else {
|
||||
// Create new admin user
|
||||
const hashedPassword = await hash(adminPassword, 12);
|
||||
const userId = generateId();
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: userId,
|
||||
email: adminEmail,
|
||||
name: "Admin User",
|
||||
role: "admin",
|
||||
emailVerified: true,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.account.create({
|
||||
data: {
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: userId,
|
||||
email: adminEmail,
|
||||
name: "Admin Desa Darmasaba",
|
||||
role: "admin",
|
||||
emailVerified: true,
|
||||
accounts: {
|
||||
create: {
|
||||
id: generateId(),
|
||||
userId,
|
||||
accountId: userId,
|
||||
providerId: "credential",
|
||||
password: hashedPassword,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`Admin user created with email: ${adminEmail}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error seeding admin user:", error);
|
||||
throw error;
|
||||
}
|
||||
console.log(`Admin user created: ${adminEmail}`);
|
||||
return userId;
|
||||
}
|
||||
|
||||
async function seedDemoUsers() {
|
||||
const demoUsers = [
|
||||
{ email: "demo1@example.com", name: "Demo User 1", role: "user" },
|
||||
{ email: "demo2@example.com", name: "Demo User 2", role: "user" },
|
||||
async function seedBanjars() {
|
||||
const banjars = [
|
||||
{
|
||||
email: "moderator@example.com",
|
||||
name: "Moderator User",
|
||||
role: "moderator",
|
||||
name: "Darmasaba",
|
||||
code: "DSB",
|
||||
totalPopulation: 1200,
|
||||
totalKK: 300,
|
||||
totalPoor: 45,
|
||||
},
|
||||
{
|
||||
name: "Manesa",
|
||||
code: "MNS",
|
||||
totalPopulation: 950,
|
||||
totalKK: 240,
|
||||
totalPoor: 32,
|
||||
},
|
||||
{
|
||||
name: "Cabe",
|
||||
code: "CBE",
|
||||
totalPopulation: 800,
|
||||
totalKK: 200,
|
||||
totalPoor: 28,
|
||||
},
|
||||
{
|
||||
name: "Penenjoan",
|
||||
code: "PNJ",
|
||||
totalPopulation: 1100,
|
||||
totalKK: 280,
|
||||
totalPoor: 50,
|
||||
},
|
||||
{
|
||||
name: "Baler Pasar",
|
||||
code: "BPS",
|
||||
totalPopulation: 850,
|
||||
totalKK: 210,
|
||||
totalPoor: 35,
|
||||
},
|
||||
{
|
||||
name: "Bucu",
|
||||
code: "BCU",
|
||||
totalPopulation: 734,
|
||||
totalKK: 184,
|
||||
totalPoor: 24,
|
||||
},
|
||||
];
|
||||
|
||||
for (const userData of demoUsers) {
|
||||
try {
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: userData.email },
|
||||
});
|
||||
console.log("Seeding Banjars...");
|
||||
for (const banjar of banjars) {
|
||||
await prisma.banjar.upsert({
|
||||
where: { name: banjar.name },
|
||||
update: banjar,
|
||||
create: banjar,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!existingUser) {
|
||||
const userId = generateId();
|
||||
const hashedPassword = await hash("demo123", 12);
|
||||
async function seedDivisions() {
|
||||
const divisions = [
|
||||
{
|
||||
name: "Pemerintahan",
|
||||
description: "Urusan administrasi dan tata kelola desa",
|
||||
color: "#1E3A5F",
|
||||
},
|
||||
{
|
||||
name: "Pembangunan",
|
||||
description: "Infrastruktur dan sarana prasarana desa",
|
||||
color: "#2E7D32",
|
||||
},
|
||||
{
|
||||
name: "Pemberdayaan",
|
||||
description: "Pemberdayaan ekonomi dan masyarakat",
|
||||
color: "#EF6C00",
|
||||
},
|
||||
{
|
||||
name: "Kesejahteraan",
|
||||
description: "Kesehatan, pendidikan, dan sosial",
|
||||
color: "#C62828",
|
||||
},
|
||||
];
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: userId,
|
||||
email: userData.email,
|
||||
name: userData.name,
|
||||
role: userData.role,
|
||||
emailVerified: true,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
console.log("Seeding Divisions...");
|
||||
const createdDivisions = [];
|
||||
for (const div of divisions) {
|
||||
const d = await prisma.division.upsert({
|
||||
where: { name: div.name },
|
||||
update: div,
|
||||
create: div,
|
||||
});
|
||||
createdDivisions.push(d);
|
||||
}
|
||||
return createdDivisions;
|
||||
}
|
||||
|
||||
await prisma.account.create({
|
||||
data: {
|
||||
id: generateId(),
|
||||
userId,
|
||||
accountId: userId,
|
||||
providerId: "credential",
|
||||
password: hashedPassword,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
async function seedResidents(banjarIds: string[]) {
|
||||
console.log("Seeding Residents...");
|
||||
const residents = [
|
||||
{
|
||||
nik: "5103010101700001",
|
||||
kk: "5103010101700000",
|
||||
name: "I Wayan Sudarsana",
|
||||
birthDate: new Date("1970-05-15"),
|
||||
birthPlace: "Badung",
|
||||
gender: Gender.LAKI_LAKI,
|
||||
religion: Religion.HINDU,
|
||||
occupation: "Wiraswasta",
|
||||
banjarId: banjarIds[0],
|
||||
rt: "001",
|
||||
rw: "000",
|
||||
address: "Jl. Raya Darmasaba No. 1",
|
||||
isHeadOfHousehold: true,
|
||||
},
|
||||
{
|
||||
nik: "5103010101850002",
|
||||
kk: "5103010101850000",
|
||||
name: "Ni Made Arianti",
|
||||
birthDate: new Date("1985-08-20"),
|
||||
birthPlace: "Denpasar",
|
||||
gender: Gender.PEREMPUAN,
|
||||
religion: Religion.HINDU,
|
||||
occupation: "Guru",
|
||||
banjarId: banjarIds[1],
|
||||
rt: "002",
|
||||
rw: "000",
|
||||
address: "Gg. Manesa No. 5",
|
||||
isPoor: true,
|
||||
},
|
||||
];
|
||||
|
||||
console.log(`Demo user created: ${userData.email}`);
|
||||
} else {
|
||||
console.log(`Demo user already exists: ${userData.email}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error seeding user ${userData.email}:`, error);
|
||||
}
|
||||
for (const res of residents) {
|
||||
await prisma.resident.upsert({
|
||||
where: { nik: res.nik },
|
||||
update: res,
|
||||
create: res,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function seedActivities(divisionIds: string[]) {
|
||||
console.log("Seeding Activities...");
|
||||
const activities = [
|
||||
{
|
||||
title: "Rapat Koordinasi 2025",
|
||||
description: "Penyusunan rencana kerja tahunan",
|
||||
divisionId: divisionIds[0],
|
||||
progress: 100,
|
||||
status: ActivityStatus.SELESAI,
|
||||
priority: Priority.TINGGI,
|
||||
},
|
||||
{
|
||||
title: "Pemutakhiran Indeks Desa",
|
||||
description: "Pendataan SDG's Desa 2025",
|
||||
divisionId: divisionIds[0],
|
||||
progress: 65,
|
||||
status: ActivityStatus.BERJALAN,
|
||||
priority: Priority.SEDANG,
|
||||
},
|
||||
{
|
||||
title: "Pembangunan Jalan Banjar Cabe",
|
||||
description: "Pengaspalan jalan utama",
|
||||
divisionId: divisionIds[1],
|
||||
progress: 40,
|
||||
status: ActivityStatus.BERJALAN,
|
||||
priority: Priority.DARURAT,
|
||||
},
|
||||
];
|
||||
|
||||
for (const act of activities) {
|
||||
await prisma.activity.create({
|
||||
data: act,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function seedComplaints(adminId: string) {
|
||||
console.log("Seeding Complaints...");
|
||||
const complaints = [
|
||||
{
|
||||
complaintNumber: `COMP-20250326-001`,
|
||||
title: "Lampu Jalan Mati",
|
||||
description:
|
||||
"Lampu jalan di depan Balai Banjar Manesa mati sejak 3 hari lalu.",
|
||||
category: ComplaintCategory.INFRASTRUKTUR,
|
||||
status: ComplaintStatus.BARU,
|
||||
priority: Priority.SEDANG,
|
||||
location: "Banjar Manesa",
|
||||
reporterId: adminId,
|
||||
},
|
||||
{
|
||||
complaintNumber: `COMP-20250326-002`,
|
||||
title: "Sampah Menumpuk",
|
||||
description: "Tumpukan sampah di area pasar Darmasaba belum diangkut.",
|
||||
category: ComplaintCategory.KETERTIBAN_UMUM,
|
||||
status: ComplaintStatus.DIPROSES,
|
||||
priority: Priority.TINGGI,
|
||||
location: "Pasar Darmasaba",
|
||||
assignedTo: adminId,
|
||||
},
|
||||
];
|
||||
|
||||
for (const comp of complaints) {
|
||||
await prisma.complaint.upsert({
|
||||
where: { complaintNumber: comp.complaintNumber },
|
||||
update: comp,
|
||||
create: comp,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function seedEvents(adminId: string) {
|
||||
console.log("Seeding Events...");
|
||||
const events = [
|
||||
{
|
||||
title: "Rapat Pleno Desa",
|
||||
description: "Pembahasan anggaran belanja desa",
|
||||
eventType: EventType.RAPAT,
|
||||
startDate: new Date(),
|
||||
location: "Balai Desa Darmasaba",
|
||||
createdBy: adminId,
|
||||
},
|
||||
{
|
||||
title: "Gotong Royong Kebersihan",
|
||||
description: "Kegiatan rutin mingguan",
|
||||
eventType: EventType.SOSIAL,
|
||||
startDate: new Date(Date.now() + 86400000), // Besok
|
||||
location: "Seluruh Banjar",
|
||||
createdBy: adminId,
|
||||
},
|
||||
];
|
||||
|
||||
for (const event of events) {
|
||||
await prisma.event.create({
|
||||
data: event,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log("Seeding database...");
|
||||
console.log("Starting seed...");
|
||||
|
||||
await seedAdminUser();
|
||||
await seedDemoUsers();
|
||||
const adminId = await seedAdminUser();
|
||||
await seedBanjars();
|
||||
const banjars = await prisma.banjar.findMany();
|
||||
const banjarIds = banjars.map((b) => b.id);
|
||||
|
||||
console.log("Database seeding completed.");
|
||||
const divisions = await seedDivisions();
|
||||
const divisionIds = divisions.map((d) => d.id);
|
||||
|
||||
await seedResidents(banjarIds);
|
||||
await seedActivities(divisionIds);
|
||||
await seedComplaints(adminId);
|
||||
await seedEvents(adminId);
|
||||
|
||||
console.log("Seed finished successfully!");
|
||||
}
|
||||
|
||||
// Only auto-execute when run directly (not when imported)
|
||||
const isMainModule =
|
||||
typeof require !== "undefined"
|
||||
? require.main === module
|
||||
: import.meta.path.endsWith("seed.ts");
|
||||
|
||||
if (isMainModule) {
|
||||
main().catch((error) => {
|
||||
console.error("Error during seeding:", error);
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
}
|
||||
|
||||
// Export for programmatic use
|
||||
export { seedAdminUser, seedDemoUsers, main as runSeed };
|
||||
|
||||
Reference in New Issue
Block a user