Dealer_Onboarding_Backend/scripts/sync-all-hierarchy.ts

47 lines
1.7 KiB
TypeScript

import 'dotenv/config';
import db from '../src/database/models/index.js';
import { syncLocationManagers, syncRegionManager, syncZoneManager } from '../src/modules/master/syncHierarchy.service.js';
async function syncAll() {
console.log('--- Starting Master Hierarchy Synchronization ---');
try {
await db.sequelize.authenticate();
console.log('✓ Database connected.');
// 1. Sync all Zones
console.log('Syncing Zones...');
const zones = await db.Zone.findAll({ attributes: ['id', 'name'] });
for (const zone of zones) {
await syncZoneManager(zone.id);
console.log(` - Synced Zone: ${zone.name}`);
}
// 2. Sync all Regions
console.log('Syncing Regions...');
const regions = await db.Region.findAll({ attributes: ['id', 'name'] });
for (const region of regions) {
await syncRegionManager(region.id);
console.log(` - Synced Region: ${region.name}`);
}
// 3. Sync all Districts
console.log('Syncing Districts (This may take a moment)...');
const locations = await db.District.findAll({ attributes: ['id', 'name'] });
let count = 0;
for (const loc of locations) {
await syncLocationManagers(loc.id);
count++;
if (count % 50 === 0) console.log(` - Synced ${count} districts...`);
}
console.log(`✓ Total Districts Synced: ${count}`);
console.log('\n--- Synchronization Complete ---');
process.exit(0);
} catch (error) {
console.error('❌ Synchronization failed:', error);
process.exit(1);
}
}
syncAll();