fix: handle missing CSV files gracefully in rate search with Promise.allSettled
This commit is contained in:
parent
f5eabf4861
commit
e030871b4e
@ -143,7 +143,20 @@ export class CsvRateSearchService implements SearchCsvRatesPort {
|
||||
// Pass company name from config to override CSV column value
|
||||
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();
|
||||
}
|
||||
|
||||
@ -152,7 +165,13 @@ export class CsvRateSearchService implements SearchCsvRatesPort {
|
||||
const ratePromises = files.map(file =>
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
42
apps/backend/src/scripts/delete-orphaned-csv-config.ts
Normal file
42
apps/backend/src/scripts/delete-orphaned-csv-config.ts
Normal 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();
|
||||
Loading…
Reference in New Issue
Block a user