finished exchanges api connections

This commit is contained in:
Boki 2025-06-16 09:24:17 -04:00
parent d7780e9684
commit e8fbe76f2e
6 changed files with 306 additions and 23 deletions

View file

@ -183,3 +183,140 @@ exchangeRoutes.post('/api/exchanges/sync', async c => {
return c.json({ error: 'Internal server error' }, 500);
}
});
// Update exchange (shortName and active status)
exchangeRoutes.patch('/api/exchanges/:id', async c => {
try {
const id = c.req.param('id');
const updates = await c.req.json();
// Validate the updates - only allow shortName and active
const sanitizedUpdates: Record<string, unknown> = {};
if ('shortName' in updates && typeof updates.shortName === 'string') {
sanitizedUpdates.shortName = updates.shortName;
}
if ('active' in updates && typeof updates.active === 'boolean') {
sanitizedUpdates.active = updates.active;
}
if (Object.keys(sanitizedUpdates).length === 0) {
return c.json({ error: 'No valid fields to update' }, 400);
}
await connectMongoDB();
const db = getDatabase();
const collection = db.collection<MasterExchange>('masterExchanges');
// Update using MongoDB _id (ObjectId)
const result = await collection.updateOne(
{ _id: new (await import('mongodb')).ObjectId(id) },
{
$set: {
...sanitizedUpdates,
updated_at: new Date(),
},
}
);
if (result.matchedCount === 0) {
return c.json({ error: 'Exchange not found' }, 404);
}
return c.json({
status: 'success',
message: 'Exchange updated successfully',
data: { id, updates: sanitizedUpdates },
});
} catch (error) {
logger.error('Error updating exchange', { error });
return c.json({ error: 'Internal server error' }, 500);
}
});
// Add source to exchange
exchangeRoutes.post('/api/exchanges/:id/sources', async c => {
try {
const id = c.req.param('id');
const { source, source_code, mapping } = await c.req.json();
if (!source || !source_code || !mapping || !mapping.id) {
return c.json({ error: 'source, source_code, and mapping with id are required' }, 400);
}
// Create the storage key using source and source_code
const storageKey = `${source}_${source_code}`;
// Add lastUpdated to the mapping
const sourceData = {
...mapping,
lastUpdated: new Date(),
};
await connectMongoDB();
const db = getDatabase();
const collection = db.collection<MasterExchange>('masterExchanges');
// Update using MongoDB _id (ObjectId)
const result = await collection.updateOne(
{ _id: new (await import('mongodb')).ObjectId(id) },
{
$set: {
[`sourceMappings.${storageKey}`]: sourceData,
updated_at: new Date(),
},
}
);
if (result.matchedCount === 0) {
return c.json({ error: 'Exchange not found' }, 404);
}
return c.json({
status: 'success',
message: 'Source mapping added successfully',
data: { id, storageKey, mapping: sourceData },
});
} catch (error) {
logger.error('Error adding source mapping', { error });
return c.json({ error: 'Internal server error' }, 500);
}
});
// Remove source from exchange
exchangeRoutes.delete('/api/exchanges/:id/sources/:sourceName', async c => {
try {
const id = c.req.param('id');
const sourceName = c.req.param('sourceName');
await connectMongoDB();
const db = getDatabase();
const collection = db.collection<MasterExchange>('masterExchanges');
// Remove the source mapping using MongoDB _id (ObjectId)
const result = await collection.updateOne(
{ _id: new (await import('mongodb')).ObjectId(id) },
{
$unset: {
[`sourceMappings.${sourceName}`]: '',
},
$set: {
updated_at: new Date(),
},
}
);
if (result.matchedCount === 0) {
return c.json({ error: 'Exchange not found' }, 404);
}
return c.json({
status: 'success',
message: 'Source mapping removed successfully',
data: { id, sourceName },
});
} catch (error) {
logger.error('Error removing source mapping', { error });
return c.json({ error: 'Internal server error' }, 500);
}
});