initial masterExchanges
This commit is contained in:
parent
d068898e32
commit
660a2a1ec2
7 changed files with 446 additions and 3 deletions
128
apps/data-service/src/routes/exchange.routes.ts
Normal file
128
apps/data-service/src/routes/exchange.routes.ts
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* Exchange Routes - Simple API endpoints for exchange management
|
||||
*/
|
||||
import { Hono } from 'hono';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { MasterExchange } from '@stock-bot/mongodb-client';
|
||||
import { connectMongoDB, getDatabase } from '@stock-bot/mongodb-client';
|
||||
import { queueManager } from '../index';
|
||||
|
||||
const logger = getLogger('exchange-routes');
|
||||
|
||||
export const exchangeRoutes = new Hono();
|
||||
|
||||
// Get master exchange details
|
||||
exchangeRoutes.get('/api/exchanges/:masterExchangeId', async c => {
|
||||
try {
|
||||
const masterExchangeId = c.req.param('masterExchangeId');
|
||||
|
||||
await connectMongoDB();
|
||||
const db = getDatabase();
|
||||
const collection = db.collection<MasterExchange>('masterExchanges');
|
||||
const exchange = await collection.findOne({ masterExchangeId });
|
||||
|
||||
if (!exchange) {
|
||||
return c.json({ error: 'Exchange not found' }, 404);
|
||||
}
|
||||
|
||||
return c.json({
|
||||
status: 'success',
|
||||
data: exchange,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error getting exchange details', { error });
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Get source mapping
|
||||
exchangeRoutes.get('/api/exchanges/mapping/:sourceName/:sourceId', async c => {
|
||||
try {
|
||||
const sourceName = c.req.param('sourceName');
|
||||
const sourceId = c.req.param('sourceId');
|
||||
|
||||
await connectMongoDB();
|
||||
const db = getDatabase();
|
||||
const collection = db.collection<MasterExchange>('masterExchanges');
|
||||
|
||||
const result = await collection.findOne(
|
||||
{ [`sourceMappings.${sourceName}.id`]: sourceId },
|
||||
{ projection: { masterExchangeId: 1 } }
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
return c.json({ error: 'Mapping not found' }, 404);
|
||||
}
|
||||
|
||||
return c.json({
|
||||
status: 'success',
|
||||
data: { masterExchangeId: result.masterExchangeId, sourceName, sourceId },
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error getting exchange mapping', { error });
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Get all exchanges from a specific source
|
||||
exchangeRoutes.get('/api/exchanges/source/:sourceName', async c => {
|
||||
try {
|
||||
const sourceName = c.req.param('sourceName');
|
||||
|
||||
await connectMongoDB();
|
||||
const db = getDatabase();
|
||||
const collection = db.collection<MasterExchange>('masterExchanges');
|
||||
|
||||
const results = await collection
|
||||
.find(
|
||||
{ [`sourceMappings.${sourceName}`]: { $exists: true } },
|
||||
{
|
||||
projection: {
|
||||
masterExchangeId: 1,
|
||||
[`sourceMappings.${sourceName}`]: 1,
|
||||
},
|
||||
}
|
||||
)
|
||||
.toArray();
|
||||
|
||||
const exchanges = results.map(result => ({
|
||||
masterExchangeId: result.masterExchangeId,
|
||||
sourceMapping: result.sourceMappings[sourceName],
|
||||
}));
|
||||
|
||||
return c.json({
|
||||
status: 'success',
|
||||
data: {
|
||||
sourceName,
|
||||
count: exchanges.length,
|
||||
exchanges,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error getting source exchanges', { error });
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Trigger exchange sync
|
||||
exchangeRoutes.post('/api/exchanges/sync', async c => {
|
||||
try {
|
||||
const job = await queueManager.add('exchange-sync', {
|
||||
type: 'exchange-sync',
|
||||
provider: 'exchange-sync',
|
||||
operation: 'sync-ib-exchanges',
|
||||
payload: {},
|
||||
priority: 2,
|
||||
});
|
||||
|
||||
return c.json({
|
||||
status: 'success',
|
||||
message: 'IB exchange sync job queued',
|
||||
jobId: job.id,
|
||||
operation: 'sync-ib-exchanges',
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error triggering exchange sync', { error });
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
});
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
/**
|
||||
* Routes index - exports all route modules
|
||||
*/
|
||||
export { exchangeRoutes } from './exchange.routes';
|
||||
export { healthRoutes } from './health.routes';
|
||||
export { queueRoutes } from './queue.routes';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue