getting close to having exchanges done
This commit is contained in:
parent
6a34d1140f
commit
4f4f615a62
3 changed files with 68 additions and 61 deletions
|
|
@ -9,12 +9,13 @@ import { getMongoDBClient } from '@stock-bot/mongodb-client';
|
|||
const logger = getLogger('exchange-routes');
|
||||
export const exchangeRoutes = new Hono();
|
||||
|
||||
// Get all exchanges with provider mapping counts
|
||||
// Get all exchanges with provider mapping counts and mappings
|
||||
exchangeRoutes.get('/', async c => {
|
||||
try {
|
||||
const postgresClient = getPostgreSQLClient();
|
||||
|
||||
const query = `
|
||||
// First get all exchanges with counts
|
||||
const exchangesQuery = `
|
||||
SELECT
|
||||
e.id,
|
||||
e.code,
|
||||
|
|
@ -34,12 +35,49 @@ exchangeRoutes.get('/', async c => {
|
|||
ORDER BY e.code
|
||||
`;
|
||||
|
||||
const result = await postgresClient.query(query);
|
||||
const exchangesResult = await postgresClient.query(exchangesQuery);
|
||||
|
||||
// Then get all provider mappings
|
||||
const mappingsQuery = `
|
||||
SELECT
|
||||
pem.*,
|
||||
e.code as master_exchange_code,
|
||||
e.name as master_exchange_name
|
||||
FROM provider_exchange_mappings pem
|
||||
JOIN exchanges e ON pem.master_exchange_id = e.id
|
||||
ORDER BY pem.master_exchange_id, pem.provider, pem.provider_exchange_code
|
||||
`;
|
||||
const mappingsResult = await postgresClient.query(mappingsQuery);
|
||||
|
||||
// Group mappings by exchange ID
|
||||
const mappingsByExchange = mappingsResult.rows.reduce((acc, mapping) => {
|
||||
const exchangeId = mapping.master_exchange_id;
|
||||
if (!acc[exchangeId]) {
|
||||
acc[exchangeId] = [];
|
||||
}
|
||||
acc[exchangeId].push(mapping);
|
||||
return acc;
|
||||
}, {} as Record<string, any[]>);
|
||||
|
||||
// Attach mappings to exchanges
|
||||
const exchangesWithMappings = exchangesResult.rows.map(exchange => {
|
||||
const mappings = mappingsByExchange[exchange.id] || [];
|
||||
logger.info('Exchange mapping debug', {
|
||||
exchangeId: exchange.id,
|
||||
exchangeCode: exchange.code,
|
||||
mappingsCount: mappings.length,
|
||||
availableExchangeIds: Object.keys(mappingsByExchange)
|
||||
});
|
||||
return {
|
||||
...exchange,
|
||||
provider_mappings: mappings
|
||||
};
|
||||
});
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
data: result.rows,
|
||||
total: result.rows.length,
|
||||
data: exchangesWithMappings,
|
||||
total: exchangesWithMappings.length,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to get exchanges', { error });
|
||||
|
|
@ -343,6 +381,9 @@ exchangeRoutes.post('/provider-mappings', async c => {
|
|||
);
|
||||
}
|
||||
|
||||
// Validate and clean currency field (must be 3 characters or null)
|
||||
const cleanCurrency = currency && currency.length <= 3 ? currency : null;
|
||||
|
||||
const query = `
|
||||
INSERT INTO provider_exchange_mappings
|
||||
(provider, provider_exchange_code, provider_exchange_name, master_exchange_id,
|
||||
|
|
@ -357,7 +398,7 @@ exchangeRoutes.post('/provider-mappings', async c => {
|
|||
provider_exchange_name,
|
||||
master_exchange_id,
|
||||
country_code,
|
||||
currency,
|
||||
cleanCurrency,
|
||||
confidence,
|
||||
active,
|
||||
verified,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue