uup
This commit is contained in:
168
prisma/seed.ts
Normal file
168
prisma/seed.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import { seedMaintenanceTypes } from './maintenanceTypeSeed';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('🌱 Seeding database...');
|
||||
|
||||
// Check if superadmin already exists
|
||||
const existingSuperadmin = await prisma.user.findFirst({
|
||||
where: { authLevel: 1 }
|
||||
});
|
||||
|
||||
if (!existingSuperadmin) {
|
||||
// Create superadmin account
|
||||
const hashedPassword = await bcrypt.hash('admin123', 10);
|
||||
|
||||
const superadmin = await prisma.user.create({
|
||||
data: {
|
||||
name: 'Super Administrator',
|
||||
username: 'superadmin',
|
||||
email: 'admin@carmaintenance.com',
|
||||
password: hashedPassword,
|
||||
status: 'active',
|
||||
authLevel: 1,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('✅ Created superadmin user:', superadmin.username);
|
||||
} else {
|
||||
console.log('ℹ️ Superadmin user already exists');
|
||||
}
|
||||
|
||||
// Seed maintenance types using the dedicated seed function
|
||||
await seedMaintenanceTypes();
|
||||
|
||||
// Seed some sample data for development
|
||||
const sampleCustomer = await prisma.customer.upsert({
|
||||
where: { id: 1 },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'أحمد محمد',
|
||||
phone: '+966501234567',
|
||||
email: 'ahmed@example.com',
|
||||
address: 'الرياض، المملكة العربية السعودية',
|
||||
},
|
||||
});
|
||||
|
||||
const sampleVehicle = await prisma.vehicle.upsert({
|
||||
where: { id: 1 },
|
||||
update: {},
|
||||
create: {
|
||||
plateNumber: 'ABC-1234',
|
||||
bodyType: 'سيدان',
|
||||
manufacturer: 'تويوتا',
|
||||
model: 'كامري',
|
||||
trim: 'GLE',
|
||||
year: 2022,
|
||||
transmission: 'Automatic',
|
||||
fuel: 'Gasoline',
|
||||
cylinders: 4,
|
||||
engineDisplacement: 2.5,
|
||||
useType: 'personal',
|
||||
ownerId: sampleCustomer.id,
|
||||
},
|
||||
});
|
||||
|
||||
// Get maintenance types for sample visits
|
||||
const periodicMaintenanceType = await prisma.maintenanceType.findFirst({
|
||||
where: { name: 'صيانة دورية' }
|
||||
});
|
||||
const brakeRepairType = await prisma.maintenanceType.findFirst({
|
||||
where: { name: 'إصلاح الفرامل' }
|
||||
});
|
||||
|
||||
// Create some sample maintenance visits
|
||||
const sampleVisit1 = await prisma.maintenanceVisit.upsert({
|
||||
where: { id: 1 },
|
||||
update: {},
|
||||
create: {
|
||||
vehicleId: sampleVehicle.id,
|
||||
customerId: sampleCustomer.id,
|
||||
maintenanceTypeId: periodicMaintenanceType!.id,
|
||||
description: 'تغيير زيت المحرك وفلتر الزيت وفحص شامل للمركبة',
|
||||
cost: 250.00,
|
||||
paymentStatus: 'paid',
|
||||
kilometers: 45000,
|
||||
visitDate: new Date('2024-01-15'),
|
||||
nextVisitDelay: 6,
|
||||
},
|
||||
});
|
||||
|
||||
const sampleVisit2 = await prisma.maintenanceVisit.upsert({
|
||||
where: { id: 2 },
|
||||
update: {},
|
||||
create: {
|
||||
vehicleId: sampleVehicle.id,
|
||||
customerId: sampleCustomer.id,
|
||||
maintenanceTypeId: brakeRepairType!.id,
|
||||
description: 'تغيير أقراص الفرامل الأمامية وتيل الفرامل',
|
||||
cost: 450.00,
|
||||
paymentStatus: 'paid',
|
||||
kilometers: 47500,
|
||||
visitDate: new Date('2024-03-20'),
|
||||
nextVisitDelay: 12,
|
||||
},
|
||||
});
|
||||
|
||||
const sampleVisit3 = await prisma.maintenanceVisit.upsert({
|
||||
where: { id: 3 },
|
||||
update: {},
|
||||
create: {
|
||||
vehicleId: sampleVehicle.id,
|
||||
customerId: sampleCustomer.id,
|
||||
maintenanceTypeId: periodicMaintenanceType!.id,
|
||||
description: 'تغيير زيت المحرك وفلتر الهواء وفحص البطارية',
|
||||
cost: 180.00,
|
||||
paymentStatus: 'pending',
|
||||
kilometers: 52000,
|
||||
visitDate: new Date('2024-07-15'),
|
||||
nextVisitDelay: 6,
|
||||
},
|
||||
});
|
||||
|
||||
// Create income records for the maintenance visits
|
||||
await prisma.income.upsert({
|
||||
where: { id: 1 },
|
||||
update: {},
|
||||
create: {
|
||||
maintenanceVisitId: sampleVisit1.id,
|
||||
amount: sampleVisit1.cost,
|
||||
incomeDate: sampleVisit1.visitDate,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.income.upsert({
|
||||
where: { id: 2 },
|
||||
update: {},
|
||||
create: {
|
||||
maintenanceVisitId: sampleVisit2.id,
|
||||
amount: sampleVisit2.cost,
|
||||
incomeDate: sampleVisit2.visitDate,
|
||||
},
|
||||
});
|
||||
|
||||
// Update the vehicle with last visit information
|
||||
await prisma.vehicle.update({
|
||||
where: { id: sampleVehicle.id },
|
||||
data: {
|
||||
lastVisitDate: sampleVisit3.visitDate,
|
||||
suggestedNextVisitDate: new Date('2025-01-15'), // 6 months after last visit
|
||||
},
|
||||
});
|
||||
|
||||
console.log('✅ Created sample customer, vehicle, and maintenance visits');
|
||||
|
||||
console.log('🎉 Database seeded successfully!');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error('❌ Error seeding database:', e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user