fix: fix get-user-orders-all-pairs issues

This commit is contained in:
Andrew Besedin 2026-02-17 02:26:31 +03:00
parent 4bf4978f57
commit a4e0406426
4 changed files with 33 additions and 29 deletions

View file

@ -33,7 +33,6 @@ export interface OrderWithPairAndCurrencies extends Order {
pair: PairWithCurrencies; pair: PairWithCurrencies;
} }
export interface GroupByIdPair { export interface PairWithIdAndCurrencies extends PairWithCurrencies {
pair_id: number; id: number;
pair: PairWithCurrencies;
} }

View file

@ -2,11 +2,11 @@ export type GetUserOrdersAllPairsResPair = {
id: number; id: number;
firstCurrency: { firstCurrency: {
id: number; id: number;
ticker: string | null; ticker: string;
}; };
secondCurrency: { secondCurrency: {
id: number; id: number;
ticker: string | null; ticker: string;
}; };
}; };

View file

@ -3,9 +3,9 @@ import Decimal from 'decimal.js';
import TransactionWithOrders from '@/interfaces/common/Transaction.js'; import TransactionWithOrders from '@/interfaces/common/Transaction.js';
import Currency from '@/schemes/Currency.js'; import Currency from '@/schemes/Currency.js';
import { import {
GroupByIdPair,
OrderWithPairAndCurrencies, OrderWithPairAndCurrencies,
PairWithCurrencies, PairWithCurrencies,
PairWithIdAndCurrencies,
} from '@/interfaces/database/modifiedRequests.js'; } from '@/interfaces/database/modifiedRequests.js';
import dexModel from './Dex.js'; import dexModel from './Dex.js';
import userModel from './User.js'; import userModel from './User.js';
@ -820,11 +820,11 @@ class OrdersModel {
id: number; id: number;
firstCurrency: { firstCurrency: {
id: number; id: number;
ticker: string | null; ticker: string;
}; };
secondCurrency: { secondCurrency: {
id: number; id: number;
ticker: string | null; ticker: string;
}; };
}[]; }[];
}> => { }> => {
@ -834,33 +834,37 @@ class OrdersModel {
throw new Error(OrdersModel.GET_USER_ORDERS_ALL_PAIRS_USER_NOT_FOUND); throw new Error(OrdersModel.GET_USER_ORDERS_ALL_PAIRS_USER_NOT_FOUND);
} }
const pairsGroupedSelection = (await Order.findAll({ // Select distinct pair IDs for the user's orders, then fetch pairs
where: { const distinctPairIdRows = (await Order.findAll({
user_id: userRow.id, attributes: [[sequelize.fn('DISTINCT', sequelize.col('pair_id')), 'pair_id']],
}, where: { user_id: userRow.id },
group: 'pair_id', raw: true,
include: [ })) as { pair_id: number }[];
{
model: Pair,
as: 'pair',
include: ['first_currency', 'second_currency'],
},
],
})) as unknown as GroupByIdPair[];
const pairs = pairsGroupedSelection.map((e) => { const pairIds = distinctPairIdRows.map((row) => row.pair_id);
const firstCurrencyTicker = e.pair.first_currency.asset_info?.ticker;
const secondCurrencyTicker = e.pair.second_currency.asset_info?.ticker; const pairsSelection = (await Pair.findAll({
where: { id: pairIds },
include: [
{ model: Currency, as: 'first_currency' },
{ model: Currency, as: 'second_currency' },
],
attributes: ['id'],
})) as PairWithIdAndCurrencies[];
const pairs = pairsSelection.map((e) => {
const firstCurrencyTicker = e.first_currency.name;
const secondCurrencyTicker = e.second_currency.name;
return { return {
id: e.pair.id, id: e.id,
firstCurrency: { firstCurrency: {
id: e.pair.first_currency.id, id: e.first_currency.id,
ticker: firstCurrencyTicker ?? null, ticker: firstCurrencyTicker,
}, },
secondCurrency: { secondCurrency: {
id: e.pair.second_currency.id, id: e.second_currency.id,
ticker: secondCurrencyTicker ?? null, ticker: secondCurrencyTicker,
}, },
}; };
}); });

View file

@ -15,6 +15,7 @@ ordersRouter.use(
'/orders/get', '/orders/get',
'/orders/cancel', '/orders/cancel',
'/orders/apply-order', '/orders/apply-order',
'/orders/get-user-orders-pairs',
], ],
middleware.verifyToken, middleware.verifyToken,
); );