Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};
Expand Down
2 changes: 2 additions & 0 deletions server/handlers/walletHandler/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this restricted to asc and desc

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OlhaD It may be better to rename these to sort_by and order, to match with the current version of the API, which already has these options implemented.
Also, this PR should be merged into the production branch v1.10, right?

The current version doesn't support sorting by token amount either, so we should create an issue for that as well.

});

const walletIdParamSchema = Joi.object({
Expand Down
4 changes: 2 additions & 2 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
6 changes: 5 additions & 1 deletion server/repositories/WalletRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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;
}
}
Expand Down
10 changes: 7 additions & 3 deletions server/services/WalletService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -67,7 +71,7 @@ class WalletService {
}),
);
}
return this._wallet.getAllWallets(id, limitOptions);
return this._wallet.getAllWallets(id, limitOptions, sortOptions);
}

async hasControlOver(parentId, childId) {
Expand Down