146 lines
4.6 KiB
JavaScript
146 lines
4.6 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
// Production seed script that doesn't require tsx or other dev dependencies
|
|
import { PrismaClient } from '@prisma/client';
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function seedDatabase() {
|
|
try {
|
|
console.log('🌱 Starting database seeding...');
|
|
|
|
// Check if database is already seeded
|
|
const existingAdmin = await prisma.employee.findFirst({
|
|
where: { authLevel: 3 }
|
|
});
|
|
|
|
if (existingAdmin) {
|
|
console.log('✅ Database already seeded, skipping...');
|
|
return;
|
|
}
|
|
|
|
// Seed Areas
|
|
console.log('📍 Seeding areas...');
|
|
const areas = [
|
|
{ name: 'Petra' },
|
|
{ name: 'Jarash' },
|
|
{ name: 'Rum' }
|
|
];
|
|
|
|
for (const area of areas) {
|
|
await prisma.area.upsert({
|
|
where: { name: area.name },
|
|
update: {},
|
|
create: area
|
|
});
|
|
}
|
|
|
|
// Seed DredgerLocations
|
|
console.log('🚢 Seeding dredger locations...');
|
|
const dredgerLocations = [
|
|
{ name: 'SP1-1', class: 'SP' },
|
|
{ name: 'SP1-2', class: 'SP' },
|
|
{ name: 'C01', class: 'C' },
|
|
{ name: 'D1', class: 'D' }
|
|
];
|
|
|
|
for (const location of dredgerLocations) {
|
|
await prisma.dredgerLocation.upsert({
|
|
where: { name: location.name },
|
|
update: {},
|
|
create: location
|
|
});
|
|
}
|
|
|
|
// Seed ReclamationLocations
|
|
console.log('🏖️ Seeding reclamation locations...');
|
|
const reclamationLocations = [
|
|
{ name: 'Eastern Shoreline' },
|
|
{ name: 'Western Shoreline' }
|
|
];
|
|
|
|
for (const location of reclamationLocations) {
|
|
await prisma.reclamationLocation.upsert({
|
|
where: { name: location.name },
|
|
update: {},
|
|
create: location
|
|
});
|
|
}
|
|
|
|
// Seed Super Admin Employee
|
|
console.log('👤 Creating super admin user...');
|
|
const superAdminUsername = process.env.SUPER_ADMIN || 'superadmin';
|
|
const superAdminEmail = process.env.SUPER_ADMIN_EMAIL || 'admin@example.com';
|
|
const superAdminPassword = process.env.SUPER_ADMIN_PASSWORD || 'P@ssw0rd123';
|
|
|
|
await prisma.employee.upsert({
|
|
where: { username: superAdminUsername },
|
|
update: {},
|
|
create: {
|
|
name: 'Super Admin User',
|
|
authLevel: 3,
|
|
username: superAdminUsername,
|
|
email: superAdminEmail,
|
|
password: bcrypt.hashSync(superAdminPassword, 10)
|
|
}
|
|
});
|
|
|
|
// Seed Foreman
|
|
console.log('👷 Seeding foreman...');
|
|
await prisma.foreman.upsert({
|
|
where: { id: 1 },
|
|
update: {},
|
|
create: {
|
|
id: 1,
|
|
name: 'John Smith'
|
|
}
|
|
});
|
|
|
|
// Seed Equipment
|
|
console.log('🚜 Seeding equipment...');
|
|
const equipment = [
|
|
{ id: 1, category: 'Dozer', model: 'Dozer6', number: 1 },
|
|
{ id: 2, category: 'Dozer', model: 'Dozer6', number: 2 },
|
|
{ id: 3, category: 'Dozer', model: 'Dozer7', number: 1 },
|
|
{ id: 4, category: 'Dozer', model: 'Dozer8', number: 1 },
|
|
{ id: 5, category: 'Loader', model: 'Loader', number: 1 },
|
|
{ id: 6, category: 'Excavator', model: 'Exc.', number: 1 },
|
|
{ id: 7, category: 'Excavator', model: 'Exc.', number: 9 }
|
|
];
|
|
|
|
for (const item of equipment) {
|
|
await prisma.equipment.upsert({
|
|
where: { id: item.id },
|
|
update: {},
|
|
create: item
|
|
});
|
|
}
|
|
|
|
console.log('✅ Database seeded successfully!');
|
|
console.log(`📊 Summary:`);
|
|
console.log(` - ${areas.length} areas`);
|
|
console.log(` - ${dredgerLocations.length} dredger locations`);
|
|
console.log(` - ${reclamationLocations.length} reclamation locations`);
|
|
console.log(` - 1 super admin user`);
|
|
console.log(` - 1 foreman`);
|
|
console.log(` - ${equipment.length} equipment records`);
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error seeding database:', error);
|
|
throw error;
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
// Run the seeding
|
|
seedDatabase()
|
|
.then(() => {
|
|
console.log('🎉 Seeding completed successfully!');
|
|
process.exit(0);
|
|
})
|
|
.catch((error) => {
|
|
console.error('💥 Seeding failed:', error);
|
|
process.exit(1);
|
|
}); |