fixed exchange mappings and added visible column

This commit is contained in:
Boki 2025-06-18 09:29:20 -04:00
parent 4f4f615a62
commit 0bec1eca83
5 changed files with 66 additions and 7 deletions

View file

@ -259,8 +259,8 @@ export class SyncManager {
private async createExchange(qmExchange: any): Promise<void> { private async createExchange(qmExchange: any): Promise<void> {
const query = ` const query = `
INSERT INTO exchanges (code, name, country, currency) INSERT INTO exchanges (code, name, country, currency, visible)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (code) DO NOTHING ON CONFLICT (code) DO NOTHING
`; `;
@ -269,6 +269,7 @@ export class SyncManager {
qmExchange.exchangeShortName || qmExchange.name, qmExchange.exchangeShortName || qmExchange.name,
qmExchange.countryCode || 'US', qmExchange.countryCode || 'US',
'USD', // Default currency, can be improved 'USD', // Default currency, can be improved
true, // New exchanges are visible by default
]); ]);
} }

View file

@ -14,7 +14,7 @@ exchangeRoutes.get('/', async c => {
try { try {
const postgresClient = getPostgreSQLClient(); const postgresClient = getPostgreSQLClient();
// First get all exchanges with counts // First get all exchanges with counts (only visible ones)
const exchangesQuery = ` const exchangesQuery = `
SELECT SELECT
e.id, e.id,
@ -23,6 +23,7 @@ exchangeRoutes.get('/', async c => {
e.country, e.country,
e.currency, e.currency,
e.active, e.active,
e.visible,
e.created_at, e.created_at,
e.updated_at, e.updated_at,
COUNT(pem.id) as provider_mapping_count, COUNT(pem.id) as provider_mapping_count,
@ -31,7 +32,8 @@ exchangeRoutes.get('/', async c => {
STRING_AGG(DISTINCT pem.provider, ', ') as providers STRING_AGG(DISTINCT pem.provider, ', ') as providers
FROM exchanges e FROM exchanges e
LEFT JOIN provider_exchange_mappings pem ON e.id = pem.master_exchange_id LEFT JOIN provider_exchange_mappings pem ON e.id = pem.master_exchange_id
GROUP BY e.id, e.code, e.name, e.country, e.currency, e.active, e.created_at, e.updated_at WHERE e.visible = true
GROUP BY e.id, e.code, e.name, e.country, e.currency, e.active, e.visible, e.created_at, e.updated_at
ORDER BY e.code ORDER BY e.code
`; `;
@ -97,8 +99,8 @@ exchangeRoutes.get('/:id', async c => {
const exchangeId = c.req.param('id'); const exchangeId = c.req.param('id');
const postgresClient = getPostgreSQLClient(); const postgresClient = getPostgreSQLClient();
// Get exchange details // Get exchange details (only if visible)
const exchangeQuery = 'SELECT * FROM exchanges WHERE id = $1'; const exchangeQuery = 'SELECT * FROM exchanges WHERE id = $1 AND visible = true';
const exchangeResult = await postgresClient.query(exchangeQuery, [exchangeId]); const exchangeResult = await postgresClient.query(exchangeQuery, [exchangeId]);
if (exchangeResult.rows.length === 0) { if (exchangeResult.rows.length === 0) {
@ -169,6 +171,11 @@ exchangeRoutes.patch('/:id', async c => {
values.push(body.currency); values.push(body.currency);
} }
if (body.visible !== undefined) {
updateFields.push(`visible = $${paramIndex++}`);
values.push(body.visible);
}
if (updateFields.length === 0) { if (updateFields.length === 0) {
return c.json({ success: false, error: 'No valid fields to update' }, 400); return c.json({ success: false, error: 'No valid fields to update' }, 400);
} }
@ -189,6 +196,20 @@ exchangeRoutes.patch('/:id', async c => {
return c.json({ success: false, error: 'Exchange not found' }, 404); return c.json({ success: false, error: 'Exchange not found' }, 404);
} }
// If hiding an exchange (visible=false), delete its provider mappings to make them available for remapping
if (body.visible === false) {
const deleteMappingsQuery = `
DELETE FROM provider_exchange_mappings
WHERE master_exchange_id = $1
`;
const mappingsResult = await postgresClient.query(deleteMappingsQuery, [exchangeId]);
logger.info('Deleted provider mappings for hidden exchange', {
exchangeId,
deletedMappings: mappingsResult.rowCount
});
}
logger.info('Exchange updated', { exchangeId, updates: body }); logger.info('Exchange updated', { exchangeId, updates: body });
return c.json({ return c.json({

View file

@ -49,7 +49,7 @@ export function ExchangesTable() {
const handleDeleteExchange = useCallback(async (exchangeId: string, exchangeName: string) => { const handleDeleteExchange = useCallback(async (exchangeId: string, exchangeName: string) => {
if (confirm(`Are you sure you want to delete "${exchangeName}"? This will hide the exchange and make all its provider mappings available for remapping.`)) { if (confirm(`Are you sure you want to delete "${exchangeName}"? This will hide the exchange and make all its provider mappings available for remapping.`)) {
const success = await updateExchange(exchangeId, { active: false }); const success = await updateExchange(exchangeId, { visible: false });
if (success) { if (success) {
// Optionally refresh the list or show a success message // Optionally refresh the list or show a success message
refetch(); refetch();

View file

@ -24,6 +24,7 @@ export interface Exchange {
country: string; country: string;
currency: string; currency: string;
active: boolean; active: boolean;
visible: boolean;
created_at: string; created_at: string;
updated_at: string; updated_at: string;
provider_mapping_count: string; provider_mapping_count: string;
@ -47,6 +48,7 @@ export interface ExchangesApiResponse {
export interface UpdateExchangeRequest { export interface UpdateExchangeRequest {
name?: string; name?: string;
active?: boolean; active?: boolean;
visible?: boolean;
country?: string; country?: string;
currency?: string; currency?: string;
} }

View file

@ -0,0 +1,35 @@
-- Add visible column to exchanges table
-- Connect to trading_bot database
\c trading_bot;
-- Add visible column to exchanges table with default true
ALTER TABLE exchanges
ADD COLUMN IF NOT EXISTS visible BOOLEAN DEFAULT true;
-- Update existing records to be visible by default
UPDATE exchanges SET visible = true WHERE visible IS NULL;
-- Create index for visibility filtering
CREATE INDEX IF NOT EXISTS idx_exchanges_visible ON exchanges(visible);
-- Update the exchange_provider_summary view to include visibility
CREATE OR REPLACE VIEW exchange_provider_summary AS
SELECT
e.code as master_code,
e.name as master_name,
e.country,
e.currency,
e.active as master_active,
e.visible as master_visible,
COUNT(pem.id) as provider_mappings,
COUNT(CASE WHEN pem.active = true THEN 1 END) as active_mappings,
COUNT(CASE WHEN pem.verified = true THEN 1 END) as verified_mappings,
STRING_AGG(DISTINCT pem.provider, ', ') as providers
FROM exchanges e
LEFT JOIN provider_exchange_mappings pem ON e.id = pem.master_exchange_id
WHERE e.visible = true -- Only show visible exchanges in summary
GROUP BY e.id, e.code, e.name, e.country, e.currency, e.active, e.visible
ORDER BY e.code;
-- Show what we created
SELECT 'Added visible column to exchanges table' as status;