add: add get-user-orders-all-pairs endpoint
This commit is contained in:
parent
2d2746acb9
commit
5aa1da59e1
6 changed files with 153 additions and 4 deletions
|
|
@ -7,6 +7,11 @@ import GetUserOrdersRes, {
|
|||
GetUserOrdersResCurrency,
|
||||
GetUserOrdersResOrderData,
|
||||
} from '@/interfaces/responses/orders/GetUserOrdersRes';
|
||||
import GetUserOrdersAllPairsBody from '@/interfaces/bodies/orders/GetUserOrdersAllPairsBody';
|
||||
import GetUserOrdersAllPairsRes, {
|
||||
GetUserOrdersAllPairsErrorCode,
|
||||
GetUserOrdersAllPairsResPair,
|
||||
} from '@/interfaces/responses/orders/GetUserOrdersAllPairsRes';
|
||||
import candlesModel from '../models/Candles';
|
||||
import ordersModel from '../models/Orders';
|
||||
import CreateOrderBody from '../interfaces/bodies/orders/CreateOrderBody';
|
||||
|
|
@ -291,6 +296,42 @@ class OrdersController {
|
|||
}
|
||||
};
|
||||
|
||||
getUserOrdersAllPairs = async (req: Request, res: Response<GetUserOrdersAllPairsRes>) => {
|
||||
try {
|
||||
const body = req.body as GetUserOrdersAllPairsBody;
|
||||
const { userData } = body;
|
||||
|
||||
const getUserOrdersAllPairsResult = await ordersModel.getUserOrdersAllPairs(
|
||||
userData.address,
|
||||
);
|
||||
|
||||
const pairs = getUserOrdersAllPairsResult.data;
|
||||
|
||||
const responsePairs: GetUserOrdersAllPairsResPair[] = pairs.map((pair) => ({
|
||||
id: pair.id,
|
||||
firstCurrency: {
|
||||
id: pair.firstCurrency.id,
|
||||
ticker: pair.firstCurrency.ticker,
|
||||
},
|
||||
secondCurrency: {
|
||||
id: pair.secondCurrency.id,
|
||||
ticker: pair.secondCurrency.ticker,
|
||||
},
|
||||
}));
|
||||
|
||||
res.status(200).send({
|
||||
success: true,
|
||||
data: responsePairs,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).send({
|
||||
success: false,
|
||||
data: GetUserOrdersAllPairsErrorCode.UNHANDLED_ERROR,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
async cancelOrder(req: Request, res: Response) {
|
||||
try {
|
||||
if (!(req.body as CancelOrderBody).orderId)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
import UserData from '@/interfaces/common/UserData';
|
||||
|
||||
interface GetUserOrdersAllPairsBody {
|
||||
userData: UserData;
|
||||
}
|
||||
|
||||
export const getUserOrdersAllPairsValidator = [];
|
||||
|
||||
export default GetUserOrdersAllPairsBody;
|
||||
|
|
@ -32,3 +32,8 @@ export interface PairWithCurrencies extends Pair {
|
|||
export interface OrderWithPairAndCurrencies extends Order {
|
||||
pair: PairWithCurrencies;
|
||||
}
|
||||
|
||||
export interface GroupByIdPair {
|
||||
pair_id: number;
|
||||
pair: PairWithCurrencies;
|
||||
}
|
||||
|
|
|
|||
30
src/interfaces/responses/orders/GetUserOrdersAllPairsRes.ts
Normal file
30
src/interfaces/responses/orders/GetUserOrdersAllPairsRes.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
export type GetUserOrdersAllPairsResPair = {
|
||||
id: number;
|
||||
firstCurrency: {
|
||||
id: number;
|
||||
ticker: string | null;
|
||||
};
|
||||
secondCurrency: {
|
||||
id: number;
|
||||
ticker: string | null;
|
||||
};
|
||||
};
|
||||
|
||||
export type GetUserOrdersAllPairsSuccessRes = {
|
||||
success: true;
|
||||
data: GetUserOrdersAllPairsResPair[];
|
||||
};
|
||||
|
||||
export enum GetUserOrdersAllPairsErrorCode {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
UNHANDLED_ERROR = 'Unhandled error',
|
||||
}
|
||||
|
||||
export type GetUserOrdersAllPairsErrorRes = {
|
||||
success: false;
|
||||
data: GetUserOrdersAllPairsErrorCode;
|
||||
};
|
||||
|
||||
type GetUserOrdersAllPairsRes = GetUserOrdersAllPairsSuccessRes | GetUserOrdersAllPairsErrorRes;
|
||||
|
||||
export default GetUserOrdersAllPairsRes;
|
||||
|
|
@ -3,12 +3,10 @@ import Decimal from 'decimal.js';
|
|||
import TransactionWithOrders from '@/interfaces/common/Transaction.js';
|
||||
import Currency from '@/schemes/Currency.js';
|
||||
import {
|
||||
OrderWithAllTransactions,
|
||||
OrderWithPair,
|
||||
GroupByIdPair,
|
||||
OrderWithPairAndCurrencies,
|
||||
PairWithCurrencies,
|
||||
} from '@/interfaces/database/modifiedRequests.js';
|
||||
import configModel from './Config.js';
|
||||
import dexModel from './Dex.js';
|
||||
import userModel from './User.js';
|
||||
import exchangeModel from './ExchangeTransactions.js';
|
||||
|
|
@ -23,7 +21,6 @@ import io from '../server.js';
|
|||
import ApplyTip from '../interfaces/responses/orders/ApplyTip.js';
|
||||
import CreateOrderBody from '../interfaces/bodies/orders/CreateOrderBody.js';
|
||||
import GetUserOrdersPageBody from '../interfaces/bodies/orders/GetUserOrdersPageBody.js';
|
||||
import GetUserOrdersBody from '../interfaces/bodies/orders/GetUserOrdersBody.js';
|
||||
import CancelOrderBody from '../interfaces/bodies/orders/CancelOrderBody.js';
|
||||
import ApplyOrderBody from '../interfaces/bodies/orders/ApplyOrderBody.js';
|
||||
import Order, { OrderStatus, OrderType } from '../schemes/Order';
|
||||
|
|
@ -804,6 +801,66 @@ class OrdersModel {
|
|||
return { success: false, data: 'Internal error' };
|
||||
}
|
||||
}
|
||||
|
||||
static GET_USER_ORDERS_ALL_PAIRS_USER_NOT_FOUND = 'No user found';
|
||||
getUserOrdersAllPairs = async (
|
||||
address: string,
|
||||
): Promise<{
|
||||
success: true;
|
||||
data: {
|
||||
id: number;
|
||||
firstCurrency: {
|
||||
id: number;
|
||||
ticker: string | null;
|
||||
};
|
||||
secondCurrency: {
|
||||
id: number;
|
||||
ticker: string | null;
|
||||
};
|
||||
}[];
|
||||
}> => {
|
||||
const userRow = await userModel.getUserRow(address);
|
||||
|
||||
if (!userRow) {
|
||||
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[];
|
||||
|
||||
const pairs = pairsGroupedSelection.map((e) => {
|
||||
const firstCurrencyTicker = e.pair.first_currency.asset_info?.ticker;
|
||||
const secondCurrencyTicker = e.pair.second_currency.asset_info?.ticker;
|
||||
|
||||
return {
|
||||
id: e.pair.id,
|
||||
firstCurrency: {
|
||||
id: e.pair.first_currency.id,
|
||||
ticker: firstCurrencyTicker ?? null,
|
||||
},
|
||||
secondCurrency: {
|
||||
id: e.pair.second_currency.id,
|
||||
ticker: secondCurrencyTicker ?? null,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: pairs,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
const ordersModel = new OrdersModel();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import express from 'express';
|
||||
|
||||
import { createOrderValidator } from '@/interfaces/bodies/orders/CreateOrderBody.js';
|
||||
import { getUserOrdersValidator } from '@/interfaces/bodies/orders/GetUserOrdersBody.js';
|
||||
import { getUserOrdersAllPairsValidator } from '@/interfaces/bodies/orders/GetUserOrdersAllPairsBody.js';
|
||||
import middleware from '../middleware/middleware.js';
|
||||
import ordersController from '../controllers/orders.controller.js';
|
||||
|
||||
|
|
@ -35,5 +37,10 @@ ordersRouter.post('/orders/get-chart-orders', ordersController.getChartOrders);
|
|||
ordersRouter.post('/orders/get-pair-stats', ordersController.getPairStats);
|
||||
ordersRouter.post('/orders/apply-order', ordersController.applyOrder);
|
||||
ordersRouter.post('/orders/get-trades', ordersController.getTrades);
|
||||
ordersRouter.get(
|
||||
'/orders/get-user-orders-pairs',
|
||||
middleware.expressValidator(getUserOrdersAllPairsValidator),
|
||||
ordersController.getUserOrdersAllPairs.bind(ordersController),
|
||||
);
|
||||
|
||||
export default ordersRouter;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue