car_mms/prisma/seed.ts
2025-09-11 14:22:27 +03:00

168 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
});