From 9b012e8d623ee6f5fa5e88c834633bf77cbfd986 Mon Sep 17 00:00:00 2001 From: OlhaD Date: Mon, 22 Jan 2024 07:11:12 -0500 Subject: [PATCH] feat: add sorting to get wallets API --- server/handlers/walletHandler/index.js | 17 ++++++++++++----- server/handlers/walletHandler/schemas.js | 2 ++ server/models/Wallet.js | 4 ++-- server/repositories/WalletRepository.js | 6 +++++- server/services/WalletService.js | 10 +++++++--- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/server/handlers/walletHandler/index.js b/server/handlers/walletHandler/index.js index ab1332c4..26c6c20e 100644 --- a/server/handlers/walletHandler/index.js +++ b/server/handlers/walletHandler/index.js @@ -12,11 +12,18 @@ const walletGet = async (req, res) => { await walletGetQuerySchema.validateAsync(req.query, { abortEarly: false }); const walletService = new WalletService(); - const { limit, offset } = req.query; - const wallets = await walletService.getAllWallets(req.wallet_id, { - limit, - offset, - }); + const { limit, offset, sortField, sortOrder } = req.query; + const wallets = await walletService.getAllWallets( + req.wallet_id, + { + limit, + offset, + }, + { + sortField: sortField || 'name', + sortOrder: sortOrder || 'asc', + }, + ); res.status(200).json({ wallets }); }; diff --git a/server/handlers/walletHandler/schemas.js b/server/handlers/walletHandler/schemas.js index e11911eb..765c1511 100644 --- a/server/handlers/walletHandler/schemas.js +++ b/server/handlers/walletHandler/schemas.js @@ -4,6 +4,8 @@ const TrustRelationshipEnums = require('../../utils/trust-enums'); const walletGetQuerySchema = Joi.object({ limit: Joi.number().required(), offset: Joi.number().integer(), + sortField: Joi.string(), + sortOrder: Joi.string(), }); const walletIdParamSchema = Joi.object({ diff --git a/server/models/Wallet.js b/server/models/Wallet.js index b8469221..519bf8e5 100644 --- a/server/models/Wallet.js +++ b/server/models/Wallet.js @@ -140,8 +140,8 @@ class Wallet { } // get wallet itself along with all subwallets - async getAllWallets(id, limitOptions) { - return this._walletRepository.getAllWallets(id, limitOptions); + async getAllWallets(id, limitOptions, sortOptions) { + return this._walletRepository.getAllWallets(id, limitOptions, sortOptions); } } diff --git a/server/repositories/WalletRepository.js b/server/repositories/WalletRepository.js index d59bece4..f08f5f9b 100644 --- a/server/repositories/WalletRepository.js +++ b/server/repositories/WalletRepository.js @@ -47,7 +47,7 @@ class WalletRepository extends BaseRepository { } // Get a wallet itself including its sub wallets - async getAllWallets(id, limitOptions) { + async getAllWallets(id, limitOptions, sortOptions) { let promise = this._session .getDB() .select('id', 'name', 'logo_url', 'created_at') @@ -98,6 +98,10 @@ class WalletRepository extends BaseRepository { promise = promise.offset(limitOptions.offset); } + if (sortOptions && sortOptions.sortField && sortOptions.sortOrder) { + promise = promise.orderBy(sortOptions.sortField, sortOptions.sortOrder); + } + return promise; } } diff --git a/server/services/WalletService.js b/server/services/WalletService.js index 77668a4f..3fffad5b 100644 --- a/server/services/WalletService.js +++ b/server/services/WalletService.js @@ -55,10 +55,14 @@ class WalletService { } } - async getAllWallets(id, limitOptions, getTokenCount = true) { + async getAllWallets(id, limitOptions, sortOptions, getTokenCount = true) { if (getTokenCount) { const token = new Token(this._session); - const wallets = await this._wallet.getAllWallets(id, limitOptions); + const wallets = await this._wallet.getAllWallets( + id, + limitOptions, + sortOptions, + ); return Promise.all( wallets.map(async (wallet) => { const json = { ...wallet }; @@ -67,7 +71,7 @@ class WalletService { }), ); } - return this._wallet.getAllWallets(id, limitOptions); + return this._wallet.getAllWallets(id, limitOptions, sortOptions); } async hasControlOver(parentId, childId) {