Files
2026-11-27 17:49:44 +07:00

199 lines
5.8 KiB
JavaScript

const { PrismaClient } = require("@prisma/client");
const bcrypt = require("bcryptjs");
const path = require("path");
const dbPath = path.resolve(__dirname, "live.db");
const dbUrl = process.env.LIVE_DATABASE_URL || process.env.DATABASE_URL || `file:${dbPath}`;
const prisma = new PrismaClient({
datasources: { db: { url: dbUrl } },
});
const DUMMY_USERS = [
{
name: "Super Admin",
email: "admin@erp.local",
password: "Admin123!",
role: "ADMIN",
notifStockIn: true,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: true,
notifRma: true,
},
{
name: "Manager Operasional",
email: "manager@erp.local",
password: "Manager123!",
role: "MANAGER",
notifStockIn: true,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: true,
notifRma: true,
},
{
name: "Staf Keuangan",
email: "finance@erp.local",
password: "Finance123!",
role: "ACCOUNTANT",
notifStockIn: false,
notifStockOut: false,
notifMoneyIn: true,
notifMoneyOut: true,
notifRma: false,
},
{
name: "Staf Sales",
email: "sales@erp.local",
password: "Sales123!",
role: "SALES",
notifStockIn: false,
notifStockOut: true,
notifMoneyIn: true,
notifMoneyOut: false,
notifRma: true,
},
{
name: "Staf Purchasing",
email: "purchasing@erp.local",
password: "Purchasing123!",
role: "PURCHASING",
notifStockIn: true,
notifStockOut: false,
notifMoneyIn: false,
notifMoneyOut: true,
notifRma: false,
},
{
name: "Staf Gudang & Teknisi",
email: "gudang@erp.local",
password: "Gudang123!",
role: "INVENTORY",
notifStockIn: true,
notifStockOut: true,
notifMoneyIn: false,
notifMoneyOut: false,
notifRma: true,
},
];
async function main() {
const isReset = process.argv.includes("--reset");
const existingUsers = await prisma.user.count();
if (!isReset && existingUsers > 0) {
console.log(`Database sudah berisi ${existingUsers} pengguna. Skip seed (gunakan --reset untuk mengosongkan total).`);
return;
}
if (isReset) {
console.log("Mengosongkan seluruh data transaksi, perusahaan, & master...");
await prisma.deviceRecord.deleteMany();
await prisma.salesItemBatchAllocation.deleteMany();
await prisma.salesOrderItem.deleteMany();
await prisma.salesOrder.deleteMany();
await prisma.purchaseBatch.deleteMany();
await prisma.purchaseOrderItem.deleteMany();
await prisma.purchaseOrder.deleteMany();
await prisma.purchaseRequisitionItem.deleteMany();
await prisma.purchaseRequisition.deleteMany();
await prisma.stockMovement.deleteMany();
await prisma.stockItem.deleteMany();
await prisma.customerProductPrice.deleteMany();
await prisma.product.deleteMany();
await prisma.category.deleteMany();
await prisma.customer.deleteMany();
await prisma.supplier.deleteMany();
await prisma.warehouse.deleteMany();
await prisma.journalLine.deleteMany();
await prisma.journalEntry.deleteMany();
await prisma.payrollRecord.deleteMany();
await prisma.payrollEmployee.deleteMany();
await prisma.account.deleteMany();
await prisma.notificationLog.deleteMany();
await prisma.auditLog.deleteMany();
await prisma.systemSetting.deleteMany();
await prisma.user.deleteMany();
}
// 1. Default Warehouse (Gudang Utama - netral tanpa nama PT bawaan sistem)
await prisma.warehouse.upsert({
where: { code: "WH-01" },
update: {},
create: {
code: "WH-01",
name: "Gudang Utama",
address: "Gudang Operasional",
companyName: "",
},
});
// 2. Pengaturan Awal (Daftar PT & Profil Perusahaan KOSONG — ditambahkan manual oleh user!)
const defaultSettings = [
{ key: "PAYROLL_COMPANIES", value: JSON.stringify([]), category: "FINANCE" },
{ key: "COMPANY_PROFILES", value: JSON.stringify({}), category: "FINANCE" },
{ key: "EMAIL_ENABLED", value: "false", category: "EMAIL" },
{ key: "TELEGRAM_ENABLED", value: "false", category: "TELEGRAM" },
{ key: "NOTIF_STOCK_IN_ENABLED", value: "true", category: "NOTIFICATION" },
{ key: "NOTIF_STOCK_OUT_ENABLED", value: "true", category: "NOTIFICATION" },
{ key: "NOTIF_MONEY_IN_ENABLED", value: "true", category: "NOTIFICATION" },
{ key: "NOTIF_MONEY_OUT_ENABLED", value: "true", category: "NOTIFICATION" },
{ key: "NOTIF_RMA_ENABLED", value: "true", category: "NOTIFICATION" },
];
for (const s of defaultSettings) {
await prisma.systemSetting.upsert({
where: { key: s.key },
update: { value: s.value },
create: s,
});
}
// 3. Create 1 Dummy User for Each of the 6 RBAC Roles
for (const u of DUMMY_USERS) {
const hashedPassword = await bcrypt.hash(u.password, 10);
await prisma.user.upsert({
where: { email: u.email },
update: {
name: u.name,
password: hashedPassword,
role: u.role,
isActive: true,
notifyEmail: true,
notifyTelegram: false,
notifStockIn: u.notifStockIn,
notifStockOut: u.notifStockOut,
notifMoneyIn: u.notifMoneyIn,
notifMoneyOut: u.notifMoneyOut,
notifRma: u.notifRma,
},
create: {
name: u.name,
email: u.email,
password: hashedPassword,
role: u.role,
isActive: true,
notifyEmail: true,
notifyTelegram: false,
notifStockIn: u.notifStockIn,
notifStockOut: u.notifStockOut,
notifMoneyIn: u.notifMoneyIn,
notifMoneyOut: u.notifMoneyOut,
notifRma: u.notifRma,
mfaEnabled: false,
},
});
}
console.log("✓ Database bersih siap pakai (0 PT bawaan sistem, 6 akun dummy untuk semua role RBAC)!");
}
main()
.catch((e) => {
console.error("Seed error:", e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});