Merge pull request 'Fix Seeder' (#17) from nico/26-nov-25 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/desa-darmasaba/pulls/17
This commit is contained in:
179
prisma/seed.ts
179
prisma/seed.ts
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
import prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
import profilePejabatDesa from "./data/landing-page/profile/profile.json";
|
import profilePejabatDesa from "./data/landing-page/profile/profile.json";
|
||||||
@@ -61,64 +62,101 @@ import users from "./data/user/users.json";
|
|||||||
import { safeSeedUnique } from "./safeseedUnique";
|
import { safeSeedUnique } from "./safeseedUnique";
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
// =========== USER ===========
|
|
||||||
console.log("🔄 Seeding user...");
|
|
||||||
for (const u of users) {
|
|
||||||
await safeSeedUnique(
|
|
||||||
"user",
|
|
||||||
{ id: u.id },
|
|
||||||
{
|
|
||||||
username: u.username,
|
|
||||||
nomor: u.nomor,
|
|
||||||
roleId: u.roleId.toString(),
|
|
||||||
isActive: u.isActive,
|
|
||||||
sessionInvalid: false,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("✅ Roles seeded");
|
|
||||||
// =========== ROLE ===========
|
|
||||||
// In your seed.ts
|
|
||||||
// =========== ROLES ===========
|
|
||||||
console.log("🔄 Seeding roles...");
|
console.log("🔄 Seeding roles...");
|
||||||
for (const r of roles) {
|
|
||||||
await safeSeedUnique(
|
|
||||||
"role",
|
|
||||||
{ id: r.id },
|
|
||||||
{
|
|
||||||
name: r.name,
|
|
||||||
description: r.description,
|
|
||||||
isActive: r.isActive,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("✅ Roles seeded");
|
// Check for duplicate names in roles data
|
||||||
|
const roleNames = new Set();
|
||||||
|
const duplicateRoleNames = roles.filter((r) => {
|
||||||
|
if (roleNames.has(r.name)) {
|
||||||
|
console.warn(`⚠️ Duplicate role name found: ${r.name}`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
roleNames.add(r.name);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const r of roles) {
|
||||||
|
try {
|
||||||
|
await safeSeedUnique(
|
||||||
|
"role",
|
||||||
|
{ id: r.id },
|
||||||
|
{
|
||||||
|
name: r.name,
|
||||||
|
description: r.description,
|
||||||
|
isActive: r.isActive,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
console.log(`✅ Seeded role -> ${r.name}`);
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.code === "P2002") {
|
||||||
|
console.warn(`⚠️ Role already exists (skipping): ${r.name}`);
|
||||||
|
} else {
|
||||||
|
console.error(`❌ Failed to seed role ${r.name}:`, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("✅ Roles seeding completed");
|
||||||
|
// =========== USER ===========
|
||||||
|
console.log("🔄 Seeding users...");
|
||||||
|
for (const u of users) {
|
||||||
|
try {
|
||||||
|
// Verify role exists
|
||||||
|
const roleExists = await prisma.role.findUnique({
|
||||||
|
where: { id: u.roleId.toString() },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!roleExists) {
|
||||||
|
console.error(
|
||||||
|
`❌ Role with id ${u.roleId} not found for user ${u.username}`
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await safeSeedUnique(
|
||||||
|
"user",
|
||||||
|
{ id: u.id },
|
||||||
|
{
|
||||||
|
username: u.username,
|
||||||
|
nomor: u.nomor,
|
||||||
|
roleId: u.roleId.toString(),
|
||||||
|
isActive: u.isActive,
|
||||||
|
sessionInvalid: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
console.log(`✅ Seeded user -> ${u.username}`);
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`❌ Failed to seed user ${u.username}:`, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("✅ Users seeding completed");
|
||||||
|
|
||||||
// =========== FILE STORAGE ===========
|
// =========== FILE STORAGE ===========
|
||||||
console.log("🔄 Seeding file storage...");
|
console.log("🔄 Seeding file storage...");
|
||||||
for (const f of fileStorage) {
|
for (const f of fileStorage) {
|
||||||
await prisma.fileStorage.upsert({
|
try {
|
||||||
where: { id: f.id },
|
await prisma.fileStorage.upsert({
|
||||||
update: {
|
where: { id: f.id },
|
||||||
name: f.name,
|
update: {
|
||||||
realName: f.realName,
|
name: f.name,
|
||||||
path: f.path,
|
realName: f.realName,
|
||||||
mimeType: f.mimeType,
|
path: f.path,
|
||||||
link: f.link,
|
mimeType: f.mimeType,
|
||||||
category: f.category,
|
link: f.link,
|
||||||
},
|
category: f.category,
|
||||||
create: {
|
},
|
||||||
id: f.id,
|
create: {
|
||||||
name: f.name,
|
id: f.id,
|
||||||
realName: f.realName,
|
name: f.name,
|
||||||
path: f.path,
|
realName: f.realName,
|
||||||
mimeType: f.mimeType,
|
path: f.path,
|
||||||
link: f.link,
|
mimeType: f.mimeType,
|
||||||
category: f.category,
|
link: f.link,
|
||||||
},
|
category: f.category,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`❌ Failed to seed file storage ${f.name}:`, error.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
console.log("✅ File storage seeded");
|
console.log("✅ File storage seeded");
|
||||||
// =========== LANDING PAGE ===========
|
// =========== LANDING PAGE ===========
|
||||||
@@ -537,15 +575,40 @@ import { safeSeedUnique } from "./safeseedUnique";
|
|||||||
console.log("posisi organisasi berhasil");
|
console.log("posisi organisasi berhasil");
|
||||||
|
|
||||||
// =========== PEGAWAI PPID ===========
|
// =========== PEGAWAI PPID ===========
|
||||||
|
console.log("🔄 Seeding pegawai PPID...");
|
||||||
const flattenedPegawai = pegawaiPPID.flat();
|
const flattenedPegawai = pegawaiPPID.flat();
|
||||||
|
|
||||||
|
// Check for duplicate emails
|
||||||
|
const emails = new Set();
|
||||||
for (const p of flattenedPegawai) {
|
for (const p of flattenedPegawai) {
|
||||||
await prisma.pegawaiPPID.upsert({
|
if (emails.has(p.email)) {
|
||||||
where: { id: p.id },
|
console.warn(`⚠️ Duplicate email found in pegawaiPPID: ${p.email}`);
|
||||||
update: p,
|
}
|
||||||
create: p,
|
emails.add(p.email);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
console.log("pegawai berhasil");
|
|
||||||
|
for (const p of flattenedPegawai) {
|
||||||
|
try {
|
||||||
|
await prisma.pegawaiPPID.upsert({
|
||||||
|
where: { id: p.id },
|
||||||
|
update: p,
|
||||||
|
create: p,
|
||||||
|
});
|
||||||
|
console.log(`✅ Seeded pegawai PPID -> ${p.namaLengkap}`);
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.code === "P2002") {
|
||||||
|
console.warn(
|
||||||
|
`⚠️ Pegawai PPID with duplicate email (skipping): ${p.email}`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.error(
|
||||||
|
`❌ Failed to seed pegawai PPID ${p.namaLengkap}:`,
|
||||||
|
error.message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("✅ pegawai PPID seeding completed");
|
||||||
|
|
||||||
// =========== SUBMENU VISI MISI PPID ===========
|
// =========== SUBMENU VISI MISI PPID ===========
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user