uup
This commit is contained in:
264
prisma/maintenanceTypeSeed.ts
Normal file
264
prisma/maintenanceTypeSeed.ts
Normal file
@@ -0,0 +1,264 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const maintenanceTypes = [
|
||||
{
|
||||
name: 'صيانة دورية',
|
||||
description: 'صيانة دورية شاملة للمركبة تشمل فحص جميع الأنظمة الأساسية',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'تغيير زيت المحرك',
|
||||
description: 'تغيير زيت المحرك وفلتر الزيت وفحص مستوى السوائل',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح الفرامل',
|
||||
description: 'صيانة وإصلاح نظام الفرامل بما في ذلك الأقراص والتيل والسوائل',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح المحرك',
|
||||
description: 'إصلاح وصيانة المحرك وأجزائه الداخلية والخارجية',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح ناقل الحركة',
|
||||
description: 'صيانة وإصلاح ناقل الحركة الأوتوماتيكي أو اليدوي',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح التكييف',
|
||||
description: 'صيانة وإصلاح نظام التكييف والتبريد في المركبة',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح الإطارات',
|
||||
description: 'تغيير وإصلاح الإطارات وضبط الهواء والتوازن',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح الكهرباء',
|
||||
description: 'إصلاح الأنظمة الكهربائية والإلكترونية في المركبة',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح التعليق',
|
||||
description: 'صيانة وإصلاح نظام التعليق والممتصات',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح العادم',
|
||||
description: 'إصلاح وتغيير نظام العادم والكاتم',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح الرادياتير',
|
||||
description: 'صيانة وإصلاح نظام التبريد والرادياتير',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح البطارية',
|
||||
description: 'فحص وتغيير البطارية ونظام الشحن',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح المصابيح',
|
||||
description: 'إصلاح وتغيير المصابيح الأمامية والخلفية',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح الزجاج',
|
||||
description: 'إصلاح وتغيير الزجاج الأمامي والخلفي والجانبي',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'إصلاح الهيكل',
|
||||
description: 'إصلاح أضرار الهيكل والصدمات والخدوش',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'تنظيف شامل',
|
||||
description: 'تنظيف شامل للمركبة من الداخل والخارج',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'فحص دوري',
|
||||
description: 'فحص دوري شامل لجميع أنظمة المركبة',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'فحص ما قبل السفر',
|
||||
description: 'فحص شامل للمركبة قبل السفر الطويل',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'صيانة طارئة',
|
||||
description: 'صيانة طارئة لحل مشاكل عاجلة في المركبة',
|
||||
isActive: true,
|
||||
},
|
||||
{
|
||||
name: 'أخرى',
|
||||
description: 'أنواع صيانة أخرى غير مدرجة في القائمة',
|
||||
isActive: true,
|
||||
},
|
||||
];
|
||||
|
||||
async function seedMaintenanceTypes() {
|
||||
console.log('🔧 Seeding maintenance types...');
|
||||
|
||||
try {
|
||||
// Check if there are any maintenance visits that might reference maintenance types in JSON
|
||||
const visitCount = await prisma.maintenanceVisit.count();
|
||||
|
||||
if (visitCount > 0) {
|
||||
console.log(`⚠️ Found ${visitCount} maintenance visits in database.`);
|
||||
console.log('🔄 Analyzing maintenance jobs in existing visits...');
|
||||
|
||||
// Get all maintenance visits to check their JSON maintenance jobs
|
||||
const visits = await prisma.maintenanceVisit.findMany({
|
||||
select: { maintenanceJobs: true },
|
||||
});
|
||||
|
||||
const referencedTypeIds = new Set<number>();
|
||||
|
||||
// Parse JSON maintenance jobs to find referenced type IDs
|
||||
visits.forEach(visit => {
|
||||
try {
|
||||
const jobs = JSON.parse(visit.maintenanceJobs);
|
||||
if (Array.isArray(jobs)) {
|
||||
jobs.forEach(job => {
|
||||
if (job.typeId && typeof job.typeId === 'number') {
|
||||
referencedTypeIds.add(job.typeId);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// Skip invalid JSON
|
||||
}
|
||||
});
|
||||
|
||||
if (referencedTypeIds.size > 0) {
|
||||
console.log(`📋 Found ${referencedTypeIds.size} maintenance type IDs referenced in visits`);
|
||||
|
||||
// Delete only maintenance types that are NOT referenced
|
||||
const deletedTypes = await prisma.maintenanceType.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
notIn: Array.from(referencedTypeIds),
|
||||
},
|
||||
},
|
||||
});
|
||||
console.log(`🗑️ Deleted ${deletedTypes.count} unreferenced maintenance types`);
|
||||
|
||||
// Update referenced maintenance types to match our seed data
|
||||
console.log('🔄 Updating referenced maintenance types...');
|
||||
for (const type of maintenanceTypes) {
|
||||
const existingType = await prisma.maintenanceType.findUnique({
|
||||
where: { name: type.name },
|
||||
});
|
||||
|
||||
if (existingType && referencedTypeIds.has(existingType.id)) {
|
||||
await prisma.maintenanceType.update({
|
||||
where: { id: existingType.id },
|
||||
data: {
|
||||
description: type.description,
|
||||
isActive: type.isActive,
|
||||
},
|
||||
});
|
||||
console.log(`✅ Updated referenced type: ${type.name}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No maintenance types are actually referenced, safe to delete all
|
||||
const deletedCount = await prisma.maintenanceType.deleteMany();
|
||||
console.log(`🗑️ Deleted ${deletedCount.count} maintenance types`);
|
||||
}
|
||||
} else {
|
||||
// No maintenance visits exist, safe to delete all maintenance types
|
||||
const deletedCount = await prisma.maintenanceType.deleteMany();
|
||||
console.log(`🗑️ Deleted ${deletedCount.count} existing maintenance types`);
|
||||
}
|
||||
|
||||
// Reset the auto-increment counter for SQLite
|
||||
console.log('🔄 Resetting ID counter...');
|
||||
await prisma.$executeRaw`DELETE FROM sqlite_sequence WHERE name = 'maintenance_types'`;
|
||||
console.log('✅ ID counter reset to start from 1');
|
||||
|
||||
console.log('📝 Inserting fresh maintenance types...');
|
||||
|
||||
let createdCount = 0;
|
||||
let updatedCount = 0;
|
||||
|
||||
for (const type of maintenanceTypes) {
|
||||
try {
|
||||
console.log(`Processing: ${type.name}`);
|
||||
|
||||
const result = await prisma.maintenanceType.upsert({
|
||||
where: { name: type.name },
|
||||
update: {
|
||||
description: type.description,
|
||||
isActive: type.isActive,
|
||||
},
|
||||
create: {
|
||||
name: type.name,
|
||||
description: type.description,
|
||||
isActive: type.isActive,
|
||||
},
|
||||
});
|
||||
|
||||
if (result.createdDate.getTime() === result.updateDate.getTime()) {
|
||||
createdCount++;
|
||||
console.log(`✅ Created: ${type.name} (ID: ${result.id})`);
|
||||
} else {
|
||||
updatedCount++;
|
||||
console.log(`✅ Updated: ${type.name} (ID: ${result.id})`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error processing "${type.name}":`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n📊 Summary:`);
|
||||
console.log(` Created: ${createdCount} maintenance types`);
|
||||
console.log(` Updated: ${updatedCount} maintenance types`);
|
||||
console.log(` Total: ${maintenanceTypes.length} maintenance types processed`);
|
||||
|
||||
// Display all maintenance types
|
||||
const allTypes = await prisma.maintenanceType.findMany({
|
||||
orderBy: { name: 'asc' },
|
||||
});
|
||||
|
||||
console.log(`\n📋 All maintenance types in database (${allTypes.length}):`);
|
||||
allTypes.forEach((type, index) => {
|
||||
const status = type.isActive ? '🟢' : '🔴';
|
||||
console.log(` ${index + 1}. ${status} ${type.name}`);
|
||||
if (type.description) {
|
||||
console.log(` 📝 ${type.description}`);
|
||||
}
|
||||
});
|
||||
|
||||
console.log('\n🎉 Maintenance types seeding completed successfully!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error during maintenance types seeding:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
await seedMaintenanceTypes();
|
||||
} catch (error) {
|
||||
console.error('❌ Error seeding maintenance types:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Always run the main function when this file is executed
|
||||
main();
|
||||
|
||||
export { seedMaintenanceTypes };
|
||||
Reference in New Issue
Block a user