Skip to content

Commit 74b511f

Browse files
authored
Merge pull request #456 from Mloweedgar/master
feat(trust_relationships): enable pagination in get trust_relationships api end point
2 parents 8f2f242 + 5638127 commit 74b511f

File tree

7 files changed

+133
-98
lines changed

7 files changed

+133
-98
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"test-repository": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit ./server/repositories/**/*.spec.js",
1717
"server-test": "npm run test-seed and npm run server",
1818
"server": "NODE_ENV=development nodemon server/server.js",
19+
"server-debug": "NODE_ENV=development nodemon --inspect=9229 server/server.js",
1920
"test-seed": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --timeout 10000 './__tests__/seed.spec.js'",
2021
"test-seed-ci": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.ci mocha -r dotenv/config --timeout 10000 './__tests__/seed.spec.js'",
2122
"test-integration": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit --timeout 20000 './__tests__/*.spec.js' './__tests__/integration/*.spec.js'",

server/handlers/trustHandler.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,17 @@ describe('trustRouter', () => {
193193
});
194194

195195
it('successfully', async () => {
196+
const limit = 10;
197+
const offset = 0;
198+
const count = 1;
196199
const getAllTrustRelationshipsStub = sinon
197200
.stub(TrustService.prototype, 'getAllTrustRelationships')
198-
.resolves([{ id: trustId }]);
201+
.resolves({
202+
result: [{ id: trustId }],
203+
count
204+
});
199205
const res = await request(app).get(
200-
`/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}`,
206+
`/trust_relationships?type=${TrustRelationshipEnums.ENTITY_TRUST_TYPE.send}&request_type=${TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send}&state=${TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted}&limit=${limit}&offset=${offset}`,
201207
);
202208
expect(res).property('statusCode').eq(200);
203209
expect(res.body.trust_relationships).lengthOf(1);
@@ -206,6 +212,8 @@ describe('trustRouter', () => {
206212
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
207213
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
208214
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
215+
limit,
216+
offset,
209217
walletId: authenticatedWalletId,
210218
});
211219
});

server/handlers/trustHandler/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,27 @@ const trustGet = async (req, res) => {
1414
state,
1515
type,
1616
request_type,
17-
// limit, offset
17+
limit, offset
1818
} = validatedQuery;
1919

2020
const { wallet_id } = req;
2121
const trustService = new TrustService();
22-
const trustRelationships = await trustService.getAllTrustRelationships({
22+
const {
23+
result: trust_relationships,
24+
count: total,
25+
} = await trustService.getAllTrustRelationships({
2326
walletId: wallet_id,
2427
state,
2528
type,
2629
request_type,
27-
// offset,
28-
// limit,
30+
offset,
31+
limit,
2932
});
3033

3134
res.status(200).json({
32-
trust_relationships: trustRelationships,
35+
trust_relationships,
36+
query: { limit, offset },
37+
total,
3338
});
3439
};
3540

server/models/Trust.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ class Trust {
4949
return this._trustRepository.getByFilter(filter, { offset, limit });
5050
}
5151

52+
/*
53+
* Get all trust relationships by filters, setting filter to undefined to allow all data
54+
*/
55+
async getAllTrustRelationships({ walletId, state, type, request_type, offset, limit }) {
56+
57+
const filter = {
58+
and: [
59+
{'originator_wallet.id': walletId},
60+
],
61+
};
62+
if (state) {
63+
filter.and.push({ state });
64+
}
65+
if (type) {
66+
filter.and.push({ type });
67+
}
68+
if (request_type) {
69+
filter.and.push({ request_type });
70+
}
71+
return this._trustRepository.getAllByFilter(filter, { offset, limit });
72+
}
73+
5274
/*
5375
* Get all relationships which has been accepted
5476
*/

server/repositories/TrustRepository.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,62 @@ class TrustRepository extends BaseRepository {
7878

7979
return promise;
8080
}
81+
82+
async getAllByFilter(filter, limitOptions) {
83+
let promise = this._session
84+
.getDB()
85+
.select(
86+
'wallet_trust.id',
87+
'wallet_trust.actor_wallet_id',
88+
'wallet_trust.target_wallet_id',
89+
'wallet_trust.type',
90+
'wallet_trust.originator_wallet_id',
91+
'wallet_trust.request_type',
92+
'wallet_trust.state',
93+
'wallet_trust.created_at',
94+
'wallet_trust.updated_at',
95+
'wallet_trust.active',
96+
'originator_wallet.name as originating_wallet',
97+
'actor_wallet.name as actor_wallet',
98+
'target_wallet.name as target_wallet',
99+
)
100+
.from('wallet_trust')
101+
.leftJoin(
102+
'wallet as originator_wallet',
103+
'wallet_trust.originator_wallet_id',
104+
'=',
105+
'originator_wallet.id',
106+
)
107+
.leftJoin(
108+
'wallet as actor_wallet',
109+
'wallet_trust.actor_wallet_id',
110+
'=',
111+
'actor_wallet.id',
112+
)
113+
.leftJoin(
114+
'wallet as target_wallet',
115+
'wallet_trust.target_wallet_id',
116+
'=',
117+
'target_wallet.id',
118+
)
119+
.where((builder) => this.whereBuilder(filter, builder))
120+
.distinctOn('wallet_trust.id'); // distinct on id to avoid duplicates
121+
122+
// get the total count (before applying limit and offset options)
123+
const count = await this._session.getDB().from(promise.as('p')).count('*');
124+
125+
if (limitOptions && limitOptions.limit) {
126+
promise = promise.limit(limitOptions.limit);
127+
}
128+
129+
if (limitOptions && limitOptions.offset) {
130+
promise = promise.offset(limitOptions.offset);
131+
}
132+
133+
const result = await promise;
134+
135+
return { result, count: +count[0].count };
136+
}
81137
}
82138

83139
module.exports = TrustRepository;

server/services/TrustService.js

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const Trust = require('../models/Trust');
22
const Session = require('../infra/database/Session');
3-
const Wallet = require('../models/Wallet');
43
const WalletService = require('./WalletService');
54
const EventService = require('./EventService');
65
const EventEnums = require('../utils/event-enum');
@@ -35,44 +34,15 @@ class TrustService {
3534
});
3635
}
3736

38-
// limit and offset not feasible using the current implementation
39-
// except if done manually or coming up with a single query
40-
async getAllTrustRelationships({ walletId, state, type, request_type }) {
41-
const walletModel = new Wallet(this._session);
42-
const { wallets } = await walletModel.getAllWallets(
37+
async getAllTrustRelationships({ walletId, state, type, request_type, offset, limit }) {
38+
return this._trust.getAllTrustRelationships({
4339
walletId,
44-
undefined,
45-
undefined,
46-
'created_at',
47-
'desc',
48-
);
49-
50-
const alltrustRelationships = [];
51-
52-
await Promise.all(
53-
wallets.map(async (w) => {
54-
const trustRelationships = await this.getTrustRelationships({
55-
walletId: w.id,
56-
state,
57-
type,
58-
request_type,
59-
});
60-
alltrustRelationships.push(...trustRelationships);
61-
}),
62-
);
63-
64-
// remove possible duplicates
65-
const ids = {};
66-
const finalTrustRelationships = [];
67-
68-
alltrustRelationships.forEach((tr) => {
69-
if (!ids[tr.id]) {
70-
finalTrustRelationships.push(tr);
71-
ids[tr.id] = 1;
72-
}
40+
state,
41+
type,
42+
request_type,
43+
offset,
44+
limit,
7345
});
74-
75-
return finalTrustRelationships;
7646
}
7747

7848
async createTrustRelationship({

server/services/TrustService.spec.js

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const sinon = require('sinon');
22
const chai = require('chai');
33
const TrustService = require('./TrustService');
44
const WalletService = require('./WalletService');
5-
const Wallet = require('../models/Wallet');
65
const Trust = require('../models/Trust');
76
const EventService = require('./EventService');
87

@@ -181,67 +180,41 @@ describe('TrustService', () => {
181180
});
182181

183182
it('getAllTrustRelationships', async () => {
184-
const getAllWalletsStub = sinon
185-
.stub(Wallet.prototype, 'getAllWallets')
186-
.resolves({ wallets: [{ id: 'id1' }, { id: 'id2' }] });
187-
const getTrustRelationshipsStub = sinon.stub(
188-
TrustService.prototype,
189-
'getTrustRelationships',
190-
);
191-
getTrustRelationshipsStub
192-
.onFirstCall()
193-
.resolves([
183+
184+
const data = {
185+
result: [
194186
{ id: 'trustId1' },
195187
{ id: 'trustId2' },
196188
{ id: 'trustId3' },
197189
{ id: 'trustId4' },
198-
]);
199-
getTrustRelationshipsStub
200-
.onSecondCall()
201-
.resolves([
202-
{ id: 'trustId1' },
203-
{ id: 'trustId2' },
204-
{ id: 'trustId5' },
205-
{ id: 'trustId6' },
206-
]);
190+
],
191+
count: 4,
192+
}
193+
194+
const getAllTrustRelationshipsStub = sinon.stub(
195+
TrustService.prototype,
196+
'getAllTrustRelationships',
197+
);
198+
getAllTrustRelationshipsStub
199+
.resolves(data);
207200

208201
const trustRelationships = await trustService.getAllTrustRelationships({
209-
walletId: 'walletId',
210-
state: 'state',
211-
type: 'type',
212-
request_type: 'request_type',
202+
state: 'requested',
203+
type: 'send',
204+
request_type: 'send',
205+
limit: 10,
206+
offset: 0,
213207
});
214-
expect(trustRelationships).eql([
215-
{ id: 'trustId1' },
216-
{ id: 'trustId2' },
217-
{ id: 'trustId3' },
218-
{ id: 'trustId4' },
219-
{ id: 'trustId5' },
220-
{ id: 'trustId6' },
221-
]);
222-
expect(
223-
getAllWalletsStub.calledOnceWithExactly(
224-
'walletId',
225-
undefined,
226-
undefined,
227-
'created_at',
228-
'desc',
229-
),
230-
).eql(true);
231-
expect(
232-
getTrustRelationshipsStub.getCall(0).calledWithExactly({
233-
walletId: 'id1',
234-
state: 'state',
235-
type: 'type',
236-
request_type: 'request_type',
237-
}),
238-
).eql(true);
208+
209+
expect(trustRelationships).eql(data);
210+
239211
expect(
240-
getTrustRelationshipsStub.getCall(1).calledWithExactly({
241-
walletId: 'id2',
242-
state: 'state',
243-
type: 'type',
244-
request_type: 'request_type',
212+
getAllTrustRelationshipsStub.calledWithExactly({
213+
state: 'requested',
214+
type: 'send',
215+
request_type: 'send',
216+
limit: 10,
217+
offset: 0,
245218
}),
246219
).eql(true);
247220
});

0 commit comments

Comments
 (0)