98 lines
2.2 KiB
JavaScript
98 lines
2.2 KiB
JavaScript
const bcrypt = require('bcrypt');
|
|
const { PrismaClient } = require('../src/generated/prisma');
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function seedAdmin() {
|
|
const adminSeederEmail = process.env.SEED_ADMIN_EMAIL?.trim() || 'admin';
|
|
const adminSeederPassword = process.env.SEED_ADMIN_PASSWORD?.trim();
|
|
|
|
if (!adminSeederPassword) {
|
|
throw new Error('SEED_ADMIN_PASSWORD belum diinputkan di environment variable');
|
|
}
|
|
|
|
const hashedPassword = await bcrypt.hash(adminSeederPassword, 10);
|
|
|
|
const admin = await prisma.admin.upsert({
|
|
where: {
|
|
email: adminSeederEmail,
|
|
},
|
|
update: {
|
|
password: hashedPassword,
|
|
role: 'admin',
|
|
},
|
|
create: {
|
|
nama_depan: 'Admin',
|
|
nama_belakang: '',
|
|
email: adminSeederEmail,
|
|
password: hashedPassword,
|
|
role: 'admin',
|
|
signature: `Admin-${Date.now()}`,
|
|
profile_picture: null,
|
|
professional_bio: 'System Administrator',
|
|
},
|
|
});
|
|
|
|
return admin;
|
|
}
|
|
|
|
async function seedActiveBatch() {
|
|
const batchNumber = Number(process.env.SEED_ACTIVE_BATCH_NUMBER || 7);
|
|
|
|
if (!Number.isInteger(batchNumber) || batchNumber < 1) {
|
|
throw new Error('SEED_ACTIVE_BATCH_NUMBER harus berupa angka positif');
|
|
}
|
|
|
|
const existingBatch = await prisma.batch.findFirst({
|
|
where: {
|
|
batch_number: batchNumber,
|
|
},
|
|
});
|
|
|
|
await prisma.batch.updateMany({
|
|
where: {
|
|
is_active: true,
|
|
},
|
|
data: {
|
|
is_active: false,
|
|
},
|
|
});
|
|
|
|
if (existingBatch) {
|
|
return prisma.batch.update({
|
|
where: {
|
|
id: existingBatch.id,
|
|
},
|
|
data: {
|
|
is_active: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
return prisma.batch.create({
|
|
data: {
|
|
batch_number: batchNumber,
|
|
is_active: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
const admin = await seedAdmin();
|
|
const activeBatch = await seedActiveBatch();
|
|
|
|
console.log('Production seed completed successfully');
|
|
console.log(`Admin username: ${admin.email}`);
|
|
console.log(`Admin password: ${process.env.SEED_ADMIN_PASSWORD}`);
|
|
console.log(`Active batch: ${activeBatch.batch_number}`);
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('Error seeding data:', e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|