fix: handle missing CSV files gracefully in rate search with Promise.allSettled

This commit is contained in:
David 2025-11-17 20:02:49 +01:00
parent f5eabf4861
commit e030871b4e
2 changed files with 63 additions and 2 deletions

View File

@ -143,7 +143,20 @@ export class CsvRateSearchService implements SearchCsvRatesPort {
// Pass company name from config to override CSV column value // Pass company name from config to override CSV column value
return this.csvRateLoader.loadRatesFromCsv(config.csvFilePath, email, config.companyName); return this.csvRateLoader.loadRatesFromCsv(config.csvFilePath, email, config.companyName);
}); });
const rateArrays = await Promise.all(ratePromises);
// Use allSettled to handle missing files gracefully
const results = await Promise.allSettled(ratePromises);
const rateArrays = results
.filter((result): result is PromiseFulfilledResult<CsvRate[]> => result.status === 'fulfilled')
.map(result => result.value);
// Log any failed file loads
const failures = results.filter(result => result.status === 'rejected');
if (failures.length > 0) {
console.warn(`Failed to load ${failures.length} CSV files:`,
failures.map((f, idx) => `${configs[idx]?.csvFilePath}: ${(f as PromiseRejectedResult).reason}`));
}
return rateArrays.flat(); return rateArrays.flat();
} }
@ -152,7 +165,13 @@ export class CsvRateSearchService implements SearchCsvRatesPort {
const ratePromises = files.map(file => const ratePromises = files.map(file =>
this.csvRateLoader.loadRatesFromCsv(file, 'bookings@example.com') this.csvRateLoader.loadRatesFromCsv(file, 'bookings@example.com')
); );
const rateArrays = await Promise.all(ratePromises);
// Use allSettled here too for consistency
const results = await Promise.allSettled(ratePromises);
const rateArrays = results
.filter((result): result is PromiseFulfilledResult<CsvRate[]> => result.status === 'fulfilled')
.map(result => result.value);
return rateArrays.flat(); return rateArrays.flat();
} }

View File

@ -0,0 +1,42 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../app.module';
import { CsvRateConfigRepository } from '@infrastructure/persistence/typeorm/repositories/csv-rate-config.repository';
/**
* Script to delete orphaned CSV rate configuration
* Usage: npm run ts-node src/scripts/delete-orphaned-csv-config.ts
*/
async function deleteOrphanedConfig() {
const app = await NestFactory.createApplicationContext(AppModule);
const repository = app.get(CsvRateConfigRepository);
try {
console.log('🔍 Searching for orphaned test.csv configuration...');
const configs = await repository.findAll();
const orphanedConfig = configs.find(c => c.csvFilePath === 'test.csv');
if (!orphanedConfig) {
console.log('✅ No orphaned test.csv configuration found');
await app.close();
return;
}
console.log(`📄 Found orphaned config: ${orphanedConfig.companyName} - ${orphanedConfig.csvFilePath}`);
console.log(` ID: ${orphanedConfig.id}`);
console.log(` Uploaded: ${orphanedConfig.uploadedAt}`);
// Delete the orphaned configuration
await repository.delete(orphanedConfig.companyName);
console.log('✅ Successfully deleted orphaned test.csv configuration');
} catch (error: any) {
console.error('❌ Error deleting orphaned config:', error.message);
process.exit(1);
}
await app.close();
}
deleteOrphanedConfig();