Skip to content

Commit

Permalink
feat: 371-update-wallets-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
OlhaD committed Jul 6, 2023
1 parent 91d0a60 commit 51ce44a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 13 deletions.
20 changes: 14 additions & 6 deletions server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ 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 { name, limit, offset } = req.query;
const wallets = await walletService.getAllWallets(
req.wallet_id,
{
limit,
offset,
},
name,
);
const walletCount = await walletService.getAllWalletsCount(req.wallet_id, name);

res.status(200).json({ wallets });
res.status(200).json({
total: walletCount,
wallets,
});
};

const walletSingleGet = async (req, res) => {
Expand Down
1 change: 1 addition & 0 deletions server/handlers/walletHandler/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const TrustRelationshipEnums = require('../../utils/trust-enums');
const walletGetQuerySchema = Joi.object({
limit: Joi.number().required(),
offset: Joi.number().integer(),
name: Joi.string(),
});

const walletIdParamSchema = Joi.object({
Expand Down
11 changes: 9 additions & 2 deletions server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@ class Wallet {
}

// get wallet itself along with all subwallets
async getAllWallets(id, limitOptions) {
return this._walletRepository.getAllWallets(id, limitOptions);
async getAllWallets(id, limitOptions, name = '') {
return this._walletRepository.getAllWallets(id, limitOptions, name);
}

// get count of wallet and all subwallets
async getAllWalletsCount(id, name = '') {
const result = await this._walletRepository.getAllWalletsCount(id, name);

return result.count;
}
}

Expand Down
81 changes: 78 additions & 3 deletions server/repositories/WalletRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ class WalletRepository extends BaseRepository {
}

// Get a wallet itself including its sub wallets
async getAllWallets(id, limitOptions) {
async getAllWallets(id, limitOptions, name = '') {
let promise = this._session
.getDB()
.select('id', 'name', 'logo_url', 'created_at')
.table('wallet')
.where('id', id)
// 'name' can contain '%' or '_' which are wildcards in SQL
.where(
'name',
'like',
`%${name.replaceAll('%', '\\%').replaceAll('_', '\\_')}%`,
)
.union(
this._session
.getDB()
Expand All @@ -70,7 +76,12 @@ class WalletRepository extends BaseRepository {
TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.manage,
'wallet_trust.state':
TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
}),
})
.where(
'name',
'like',
`%${name.replaceAll('%', '\\%').replaceAll('_', '\\_')}%`,
),
this._session
.getDB()
.select(
Expand All @@ -87,7 +98,12 @@ class WalletRepository extends BaseRepository {
TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.yield,
'wallet_trust.state':
TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
}),
})
.where(
'name',
'like',
`%${name.replaceAll('%', '\\%').replaceAll('_', '\\_')}%`,
),
);

if (limitOptions && limitOptions.limit) {
Expand All @@ -100,6 +116,65 @@ class WalletRepository extends BaseRepository {

return promise;
}

async getAllWalletsCount(id, name = '') {
const knex = this._session.getDB();

const promise = knex('wallet')
.select(knex.raw('SUM(total_count) as count'))
.from(function () {
this.union(function () {
this.select(knex.raw('COUNT(*) as total_count'))
.from('wallet')
.where(`${'wallet'}.id`, id)
// 'name' can contain '%' or '_' which are wildcards in SQL
.where(
'name',
'like',
`%${name.replaceAll('%', '\\%').replaceAll('_', '\\_')}%`,
);
})
.union(function () {
this.select(knex.raw('COUNT(*) as total_count'))
.from('wallet_trust')
.join('wallet', 'wallet_trust.target_wallet_id', '=', 'wallet.id')
.where({
'wallet_trust.actor_wallet_id': id,
'wallet_trust.request_type':
TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.manage,
'wallet_trust.state':
TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
})
.where(
'name',
'like',
`%${name.replaceAll('%', '\\%').replaceAll('_', '\\_')}%`,
);
})
.union(function () {
this.select(knex.raw('COUNT(*) as total_count'))
.from('wallet_trust')
.join('wallet', 'wallet_trust.actor_wallet_id', '=', 'wallet.id')
.where({
'wallet_trust.target_wallet_id': id,
'wallet_trust.request_type':
TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.yield,
'wallet_trust.state':
TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
})
.where(
'name',
'like',
`%${name.replaceAll('%', '\\%').replaceAll('_', '\\_')}%`,
);
})
.as('unioned_wallets');
})
.as('counts')
.first();

return promise;
}
}

module.exports = WalletRepository;
8 changes: 6 additions & 2 deletions server/services/WalletService.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class WalletService {
}
}

async getAllWallets(id, limitOptions, getTokenCount = true) {
async getAllWallets(id, limitOptions, name = '', 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, name);
return Promise.all(
wallets.map(async (wallet) => {
const json = { ...wallet };
Expand All @@ -70,6 +70,10 @@ class WalletService {
return this._wallet.getAllWallets(id, limitOptions);
}

async getAllWalletsCount(id, name = '') {
return this._wallet.getAllWalletsCount(id, name);
}

async hasControlOver(parentId, childId) {
return this._wallet.hasControlOver(parentId, childId);
}
Expand Down

0 comments on commit 51ce44a

Please sign in to comment.