From a4e0406426675d1aacf964b0a167662d5168fe97 Mon Sep 17 00:00:00 2001 From: Andrew Besedin Date: Tue, 17 Feb 2026 02:26:31 +0300 Subject: [PATCH] fix: fix get-user-orders-all-pairs issues --- src/interfaces/database/modifiedRequests.ts | 5 +- .../orders/GetUserOrdersAllPairsRes.ts | 4 +- src/models/Orders.ts | 52 ++++++++++--------- src/routes/orders.router.ts | 1 + 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/interfaces/database/modifiedRequests.ts b/src/interfaces/database/modifiedRequests.ts index a55aa3d..3e60529 100644 --- a/src/interfaces/database/modifiedRequests.ts +++ b/src/interfaces/database/modifiedRequests.ts @@ -33,7 +33,6 @@ export interface OrderWithPairAndCurrencies extends Order { pair: PairWithCurrencies; } -export interface GroupByIdPair { - pair_id: number; - pair: PairWithCurrencies; +export interface PairWithIdAndCurrencies extends PairWithCurrencies { + id: number; } diff --git a/src/interfaces/responses/orders/GetUserOrdersAllPairsRes.ts b/src/interfaces/responses/orders/GetUserOrdersAllPairsRes.ts index 8dd7210..0523c71 100644 --- a/src/interfaces/responses/orders/GetUserOrdersAllPairsRes.ts +++ b/src/interfaces/responses/orders/GetUserOrdersAllPairsRes.ts @@ -2,11 +2,11 @@ export type GetUserOrdersAllPairsResPair = { id: number; firstCurrency: { id: number; - ticker: string | null; + ticker: string; }; secondCurrency: { id: number; - ticker: string | null; + ticker: string; }; }; diff --git a/src/models/Orders.ts b/src/models/Orders.ts index f510f38..262efa4 100644 --- a/src/models/Orders.ts +++ b/src/models/Orders.ts @@ -3,9 +3,9 @@ import Decimal from 'decimal.js'; import TransactionWithOrders from '@/interfaces/common/Transaction.js'; import Currency from '@/schemes/Currency.js'; import { - GroupByIdPair, OrderWithPairAndCurrencies, PairWithCurrencies, + PairWithIdAndCurrencies, } from '@/interfaces/database/modifiedRequests.js'; import dexModel from './Dex.js'; import userModel from './User.js'; @@ -820,11 +820,11 @@ class OrdersModel { id: number; firstCurrency: { id: number; - ticker: string | null; + ticker: string; }; secondCurrency: { 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); } - const pairsGroupedSelection = (await Order.findAll({ - where: { - user_id: userRow.id, - }, - group: 'pair_id', - include: [ - { - model: Pair, - as: 'pair', - include: ['first_currency', 'second_currency'], - }, - ], - })) as unknown as GroupByIdPair[]; + // Select distinct pair IDs for the user's orders, then fetch pairs + const distinctPairIdRows = (await Order.findAll({ + attributes: [[sequelize.fn('DISTINCT', sequelize.col('pair_id')), 'pair_id']], + where: { user_id: userRow.id }, + raw: true, + })) as { pair_id: number }[]; - const pairs = pairsGroupedSelection.map((e) => { - const firstCurrencyTicker = e.pair.first_currency.asset_info?.ticker; - const secondCurrencyTicker = e.pair.second_currency.asset_info?.ticker; + const pairIds = distinctPairIdRows.map((row) => row.pair_id); + + 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 { - id: e.pair.id, + id: e.id, firstCurrency: { - id: e.pair.first_currency.id, - ticker: firstCurrencyTicker ?? null, + id: e.first_currency.id, + ticker: firstCurrencyTicker, }, secondCurrency: { - id: e.pair.second_currency.id, - ticker: secondCurrencyTicker ?? null, + id: e.second_currency.id, + ticker: secondCurrencyTicker, }, }; }); diff --git a/src/routes/orders.router.ts b/src/routes/orders.router.ts index ad4260f..fdf1896 100644 --- a/src/routes/orders.router.ts +++ b/src/routes/orders.router.ts @@ -15,6 +15,7 @@ ordersRouter.use( '/orders/get', '/orders/cancel', '/orders/apply-order', + '/orders/get-user-orders-pairs', ], middleware.verifyToken, );