From e41a23d4679063d0e1ecdb3c03197be22b7ed7c1 Mon Sep 17 00:00:00 2001 From: b1acksun Date: Tue, 24 Sep 2019 19:44:16 +0300 Subject: [PATCH 1/2] fix: add filter arguments #8 --- src/controllers/BlockChainMongo.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/controllers/BlockChainMongo.js b/src/controllers/BlockChainMongo.js index f263e18..4a65ec9 100644 --- a/src/controllers/BlockChainMongo.js +++ b/src/controllers/BlockChainMongo.js @@ -123,12 +123,28 @@ class BlockChainMongo extends BasicController { }; } - async getDelegations({ offset, limit }) { + async getDelegations({ userId, offset, limit, direction = 'out' }) { const db = this._client.db('_CYBERWAY_gls_vesting'); const collection = db.collection('delegation'); + const directionFilter = []; + + if (direction === 'out') { + directionFilter.push({ delegator: userId }); + } + + if (direction === 'in') { + directionFilter.push({ delegatee: userId }); + } + + if (direction === 'all') { + directionFilter.push({ delegator: userId }, { delegatee: userId }); + } + const items = await collection - .find({}) + .find({ + $or: directionFilter, + }) .project({ _id: false, id: false, _SERVICE_: false }) .skip(offset) .limit(limit) From 5de150713f2ad81b18308c54255024bea0f19d48 Mon Sep 17 00:00:00 2001 From: b1acksun Date: Tue, 24 Sep 2019 20:04:19 +0300 Subject: [PATCH 2/2] fix: add props validation --- src/controllers/BlockChainMongo.js | 4 ++-- src/services/Connector.js | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/controllers/BlockChainMongo.js b/src/controllers/BlockChainMongo.js index 4a65ec9..74cc8fd 100644 --- a/src/controllers/BlockChainMongo.js +++ b/src/controllers/BlockChainMongo.js @@ -123,7 +123,7 @@ class BlockChainMongo extends BasicController { }; } - async getDelegations({ userId, offset, limit, direction = 'out' }) { + async getDelegations({ userId, offset, limit, direction }) { const db = this._client.db('_CYBERWAY_gls_vesting'); const collection = db.collection('delegation'); @@ -132,7 +132,7 @@ class BlockChainMongo extends BasicController { if (direction === 'out') { directionFilter.push({ delegator: userId }); } - + if (direction === 'in') { directionFilter.push({ delegatee: userId }); } diff --git a/src/services/Connector.js b/src/services/Connector.js index 8c36234..40a3f22 100644 --- a/src/services/Connector.js +++ b/src/services/Connector.js @@ -14,9 +14,18 @@ class Connector extends BasicConnector { await super.start({ serverRoutes: { getDelegations: { - inherits: ['pagination'], + inherits: ['pagination', 'userSpecific'], handler: this._bcMongo.getDelegations, scope: this._bcMongo, + validation: { + properties: { + direction: { + type: 'string', + default: 'all', + enum: ['in', 'out', 'all'], + }, + }, + }, }, getValidators: { inherits: ['pagination'], @@ -59,6 +68,16 @@ class Connector extends BasicConnector { }, }, }, + userSpecific: { + validation: { + required: ['userId'], + properties: { + userId: { + type: 'string', + }, + }, + }, + }, }, }, });