save work
This commit is contained in:
@ -28,65 +28,78 @@ async function importCustomers(csvFilePath: string): Promise<void> {
|
||||
|
||||
console.log(`Found ${records.length} records in CSV file`);
|
||||
|
||||
// Track processed emails to skip duplicates
|
||||
const processedEmails = new Set<string>();
|
||||
// Track existing names to ensure uniqueness
|
||||
const existingNames = new Set<string>(
|
||||
(await customerRepository.find()).map(customer => customer.name)
|
||||
);
|
||||
// Get existing customers for update
|
||||
const existingCustomers = await customerRepository.find();
|
||||
const customersByEmail = new Map<string, Customer>();
|
||||
const customersByName = new Map<string, Customer>();
|
||||
|
||||
// Create lookup maps for faster access
|
||||
existingCustomers.forEach(customer => {
|
||||
if (customer.email) {
|
||||
customersByEmail.set(customer.email.toLowerCase(), customer);
|
||||
}
|
||||
customersByName.set(customer.name.toLowerCase(), customer);
|
||||
});
|
||||
|
||||
let importedCount = 0;
|
||||
let skippedDuplicateEmail = 0;
|
||||
let skippedDuplicateName = 0;
|
||||
let updatedCount = 0;
|
||||
|
||||
for (const record of records) {
|
||||
const email = record.Email === 'null' ? '' : record.Email;
|
||||
const name = record.Name;
|
||||
|
||||
// Skip if email is already processed (not empty and already seen)
|
||||
if (email && processedEmails.has(email)) {
|
||||
console.log(`Skipping record with duplicate email: ${email}`);
|
||||
skippedDuplicateEmail++;
|
||||
continue;
|
||||
let customer: Customer;
|
||||
let isUpdate = false;
|
||||
|
||||
// Check if customer exists by email or name
|
||||
if (email && customersByEmail.has(email.toLowerCase())) {
|
||||
// Update existing customer by email
|
||||
customer = customersByEmail.get(email.toLowerCase())!;
|
||||
isUpdate = true;
|
||||
console.log(`Updating customer with email: ${email}`);
|
||||
} else if (customersByName.has(name.toLowerCase())) {
|
||||
// Update existing customer by name
|
||||
customer = customersByName.get(name.toLowerCase())!;
|
||||
isUpdate = true;
|
||||
console.log(`Updating customer with name: ${name}`);
|
||||
} else {
|
||||
// Create new customer
|
||||
customer = new Customer();
|
||||
console.log(`Creating new customer: ${name}`);
|
||||
}
|
||||
|
||||
// Skip if name already exists in database
|
||||
if (existingNames.has(name)) {
|
||||
console.log(`Skipping record with duplicate name: ${name}`);
|
||||
skippedDuplicateName++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add to processed sets
|
||||
if (email) {
|
||||
processedEmails.add(email);
|
||||
}
|
||||
existingNames.add(name);
|
||||
|
||||
// Create new customer
|
||||
const customer = new Customer();
|
||||
// Update customer fields
|
||||
customer.name = name;
|
||||
customer.url = record['URL'] === 'null' ? '' : record['URL'];
|
||||
customer.url = record.URL === 'null' ? '' : record.URL;
|
||||
customer.email = email;
|
||||
customer.city = record.City === 'null' ? '' : record.City;
|
||||
|
||||
try {
|
||||
|
||||
// Save to database
|
||||
await customerRepository.save(customer);
|
||||
importedCount++;
|
||||
|
||||
console.log(`Imported customer: ${name}`);
|
||||
if (isUpdate) {
|
||||
updatedCount++;
|
||||
console.log(`Updated customer: ${name}`);
|
||||
} else {
|
||||
importedCount++;
|
||||
console.log(`Imported customer: ${name}`);
|
||||
|
||||
// Add to lookup maps for future reference
|
||||
if (email) {
|
||||
customersByEmail.set(email.toLowerCase(), customer);
|
||||
}
|
||||
customersByName.set(name.toLowerCase(), customer);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(`Skipped: ${name}`);
|
||||
console.log(`Error saving customer: ${name}`, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
console.log('Import summary:');
|
||||
console.log(`- Total records in CSV: ${records.length}`);
|
||||
console.log(`- Successfully imported: ${importedCount}`);
|
||||
console.log(`- Skipped (duplicate email): ${skippedDuplicateEmail}`);
|
||||
console.log(`- Skipped (duplicate name): ${skippedDuplicateName}`);
|
||||
console.log(`- Successfully imported (new): ${importedCount}`);
|
||||
console.log(`- Successfully updated: ${updatedCount}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error importing customers:', error);
|
||||
|
||||
Reference in New Issue
Block a user