52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { requireServices, type IServiceContainer } from '@stock-bot/services';
|
|
import {
|
|
PipelineSyncRepository,
|
|
rejectDestructiveSync,
|
|
} from '../../../repositories/PipelineSyncRepository';
|
|
import type { JobPayload, SyncResult } from '../../../types/job-payloads';
|
|
|
|
export async function syncAllExchanges(
|
|
payload: JobPayload,
|
|
container: IServiceContainer
|
|
): Promise<SyncResult> {
|
|
rejectDestructiveSync(payload);
|
|
const { mongodb, postgres } = requireServices(container, 'mongodb', 'postgres');
|
|
// Fetch first: no SQL connection or write lock is held across provider-storage reads.
|
|
const [eod, ib] = await Promise.all([
|
|
mongodb.find('eodExchanges', { active: true }),
|
|
mongodb.find('ibExchanges', {}),
|
|
]);
|
|
return PipelineSyncRepository.transaction(postgres, async repository => {
|
|
const result: SyncResult = { processed: 0, created: 0, updated: 0, skipped: 0, errors: 0 };
|
|
const rows = [
|
|
...eod.map(row => ({
|
|
provider: 'eod',
|
|
code: row.Code,
|
|
name: row.Name,
|
|
country: row.CountryISO2,
|
|
currency: row.Currency,
|
|
confidence: 0.95,
|
|
})),
|
|
...ib.map(row => ({
|
|
provider: 'ib',
|
|
code: row.exchange_id,
|
|
name: row.name,
|
|
country: row.country_code,
|
|
currency: row.currency,
|
|
confidence: 0.85,
|
|
})),
|
|
];
|
|
for (const row of rows) {
|
|
result.processed++;
|
|
if (!row.code) {
|
|
result.skipped++;
|
|
continue;
|
|
}
|
|
const outcome = await repository.syncExchange(row);
|
|
result[outcome]++;
|
|
}
|
|
// Any SQL failure aborts the entire sync, including status; never swallow errors in a transaction.
|
|
await repository.updateStatus('all', 'exchanges', result.processed);
|
|
return result;
|
|
});
|
|
}
|