Re_Backend/src/scripts/seed-dealers-table.ts

186 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

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.

/**
* Seed Dealers Table
* Populates the dealers table with sample dealer data
*
* Note: Update this script with your actual dealer data from the Excel/CSV file
*/
import { sequelize } from '../config/database';
import { Dealer } from '../models/Dealer';
import { Op } from 'sequelize';
import logger from '../utils/logger';
interface DealerSeedData {
salesCode?: string | null;
serviceCode?: string | null;
gearCode?: string | null;
gmaCode?: string | null;
region?: string | null;
dealership?: string | null;
state?: string | null;
district?: string | null;
city?: string | null;
location?: string | null;
cityCategoryPst?: string | null;
layoutFormat?: string | null;
tierCityCategory?: string | null;
onBoardingCharges?: string | null;
date?: string | null;
singleFormatMonthYear?: string | null;
domainId?: string | null;
replacement?: string | null;
terminationResignationStatus?: string | null;
dateOfTerminationResignation?: string | null;
lastDateOfOperations?: string | null;
oldCodes?: string | null;
branchDetails?: string | null;
dealerPrincipalName?: string | null;
dealerPrincipalEmailId?: string | null;
dpContactNumber?: string | null;
dpContacts?: string | null;
showroomAddress?: string | null;
showroomPincode?: string | null;
workshopAddress?: string | null;
workshopPincode?: string | null;
locationDistrict?: string | null;
stateWorkshop?: string | null;
noOfStudios?: number | null;
websiteUpdate?: string | null;
gst?: string | null;
pan?: string | null;
firmType?: string | null;
propManagingPartnersDirectors?: string | null;
totalPropPartnersDirectors?: string | null;
docsFolderLink?: string | null;
workshopGmaCodes?: string | null;
existingNew?: string | null;
dlrcode?: string | null;
}
// Sample data based on the provided table
// TODO: Replace with your actual dealer data from Excel/CSV
const dealersData: DealerSeedData[] = [
{
salesCode: '5124',
serviceCode: '5125',
gearCode: '5573',
gmaCode: '9430',
region: 'S3',
dealership: 'Accelerate Motors',
state: 'Karnataka',
district: 'Bengaluru',
city: 'Bengaluru',
location: 'RAJA RAJESHWARI NAGAR',
cityCategoryPst: 'A+',
layoutFormat: 'A+',
tierCityCategory: 'Tier 1 City',
onBoardingCharges: null,
date: '2014-09-30',
singleFormatMonthYear: 'Sep-2014',
domainId: 'acceleratemotors.rrnagar@dealer.royalenfield.com',
replacement: null,
terminationResignationStatus: null,
dateOfTerminationResignation: null,
lastDateOfOperations: null,
oldCodes: null,
branchDetails: null,
dealerPrincipalName: 'N. Shyam Charmanna',
dealerPrincipalEmailId: 'shyamcharmanna@yahoo.co.in',
dpContactNumber: '7022049621',
dpContacts: '7022049621',
showroomAddress: 'No.335, HVP RR Nagar Sector B, Ideal Homes Town Ship, Bangalore - 560098, Dist Bangalore, Karnataka',
showroomPincode: '560098',
workshopAddress: 'Works Shop No.460, 80ft Road, 2nd Phase R R Nagar, Bangalore - 560098, Dist Bangalore, Karnataka',
workshopPincode: '560098',
locationDistrict: 'Bangalore',
stateWorkshop: 'Karnataka',
noOfStudios: 0,
websiteUpdate: 'Yes',
gst: '29ARCPS1311D1Z6',
pan: 'ARCPS1311D',
firmType: 'Proprietorship',
propManagingPartnersDirectors: 'CHARMANNA SHYAM NELLAMAKADA',
totalPropPartnersDirectors: 'CHARMANNA SHYAM NELLAMAKADA',
docsFolderLink: 'https://drive.google.com/drive/folders/1sGtg3s1h9aBXX9fhxJufYuBWar8gVvnb',
workshopGmaCodes: null,
existingNew: null,
dlrcode: '3386'
}
// Add more dealer records here from your Excel/CSV data
];
async function seedDealersTable(): Promise<void> {
try {
logger.info('[Seed Dealers Table] Starting dealers table seeding...');
for (const dealerData of dealersData) {
// Use dlrcode or domainId as unique identifier if available
const uniqueIdentifier = dealerData.dlrcode || dealerData.domainId || dealerData.salesCode;
if (!uniqueIdentifier) {
logger.warn('[Seed Dealers Table] Skipping dealer record without unique identifier');
continue;
}
// Check if dealer already exists (using dlrcode, domainId, or salesCode)
const whereConditions: any[] = [];
if (dealerData.dlrcode) whereConditions.push({ dlrcode: dealerData.dlrcode });
if (dealerData.domainId) whereConditions.push({ domainId: dealerData.domainId });
if (dealerData.salesCode) whereConditions.push({ salesCode: dealerData.salesCode });
const existingDealer = whereConditions.length > 0
? await Dealer.findOne({
where: {
[Op.or]: whereConditions
}
})
: null;
if (existingDealer) {
logger.info(`[Seed Dealers Table] Dealer ${uniqueIdentifier} already exists, updating...`);
// Update existing dealer
await existingDealer.update({
...dealerData,
isActive: true
});
logger.info(`[Seed Dealers Table] ✅ Updated dealer: ${uniqueIdentifier}`);
} else {
// Create new dealer
await Dealer.create({
...dealerData,
isActive: true
});
logger.info(`[Seed Dealers Table] ✅ Created dealer: ${uniqueIdentifier}`);
}
}
logger.info('[Seed Dealers Table] ✅ Dealers table seeding completed successfully');
} catch (error) {
logger.error('[Seed Dealers Table] ❌ Error seeding dealers table:', error);
throw error;
}
}
// Run if called directly
if (require.main === module) {
sequelize
.authenticate()
.then(() => {
logger.info('[Seed Dealers Table] Database connection established');
return seedDealersTable();
})
.then(() => {
logger.info('[Seed Dealers Table] Seeding completed');
process.exit(0);
})
.catch((error) => {
logger.error('[Seed Dealers Table] Seeding failed:', error);
process.exit(1);
});
}
export { seedDealersTable, dealersData };