Skip to content

Commit 0541c11

Browse files
authored
Merge pull request #459 from samwel141/master
Created sortable trust relationships
2 parents 03c56c6 + 830353a commit 0541c11

File tree

9 files changed

+124
-80
lines changed

9 files changed

+124
-80
lines changed

server/handlers/trustHandler.spec.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const TrustService = require('../services/TrustService');
1414
const JWTService = require('../services/JWTService');
1515
const TrustRelationshipEnums = require('../utils/trust-enums');
1616

17+
1718
describe('trustRouter', () => {
1819
let app;
1920
const authenticatedWalletId = uuid.v4();
@@ -192,32 +193,43 @@ describe('trustRouter', () => {
192193
expect(res.body.message).match(/request_type.*one.*of/);
193194
});
194195

195-
it('successfully', async () => {
196-
const limit = 10;
197-
const offset = 0;
198-
const count = 1;
199-
const getAllTrustRelationshipsStub = sinon
200-
.stub(TrustService.prototype, 'getAllTrustRelationships')
201-
.resolves({
202-
result: [{ id: trustId }],
203-
count
204-
});
205-
const res = await request(app).get(
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}`,
207-
);
208-
expect(res).property('statusCode').eq(200);
209-
expect(res.body.trust_relationships).lengthOf(1);
210-
expect(res.body.trust_relationships[0]).eql({ id: trustId });
211-
expect(getAllTrustRelationshipsStub).calledWith({
212-
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
213-
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
214-
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
215-
limit,
216-
offset,
217-
walletId: authenticatedWalletId,
196+
it('successfully', async () => {
197+
const limit = 10;
198+
const offset = 0;
199+
const count = 1;
200+
const orderBy = 'created_at';
201+
const order = 'desc';
202+
203+
const getAllTrustRelationshipsStub = sinon
204+
.stub(TrustService.prototype, 'getAllTrustRelationships')
205+
.resolves({
206+
result: [{ id: trustId }],
207+
count
218208
});
209+
210+
const res = await request(app).get(
211+
`/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}&order=${order}&sort_by=${orderBy}`,
212+
);
213+
214+
expect(res).property('statusCode').eq(200);
215+
expect(res.body.trust_relationships).to.have.lengthOf(1);
216+
expect(res.body.trust_relationships[0]).to.eql({ id: trustId });
217+
218+
expect(getAllTrustRelationshipsStub).to.have.been.calledWith({
219+
state: TrustRelationshipEnums.ENTITY_TRUST_STATE_TYPE.trusted,
220+
type: TrustRelationshipEnums.ENTITY_TRUST_TYPE.send,
221+
request_type: TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE.send,
222+
limit,
223+
offset,
224+
order,
225+
sort_by: orderBy,
226+
walletId: authenticatedWalletId,
219227
});
220228
});
229+
});
230+
231+
232+
221233

222234
describe('get /trust_relationships/:id', () => {
223235
it('missed parameters -- relationshipId must be a guid', async () => {

server/handlers/trustHandler/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const trustGet = async (req, res) => {
1414
state,
1515
type,
1616
request_type,
17-
limit, offset
17+
limit,
18+
offset,
19+
sort_by,
20+
order
1821
} = validatedQuery;
1922

2023
const { wallet_id } = req;
@@ -29,11 +32,13 @@ const trustGet = async (req, res) => {
2932
request_type,
3033
offset,
3134
limit,
35+
sort_by,
36+
order
3237
});
3338

3439
res.status(200).json({
3540
trust_relationships,
36-
query: { limit, offset },
41+
query: { limit, offset, sort_by, order },
3742
total,
3843
});
3944
};

server/handlers/trustHandler/schemas.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const trustGetQuerySchema = Joi.object({
1313
),
1414
offset: Joi.number().integer().min(0).default(0),
1515
limit: Joi.number().integer().min(1).max(2000).default(500),
16+
sort_by: Joi.string().valid( 'state', 'created_at', 'updated_at').default('created_at'),
17+
order: Joi.string().valid('asc', 'desc').default('desc'),
1618
});
1719

1820
const trustPostSchema = Joi.object({

server/handlers/walletHandler.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ describe('walletRouter', () => {
3838
describe('get /wallets', () => {
3939
it('no limit parameter(1000 as default)', async () => {
4040
const res = await request(app).get('/wallets');
41-
expect(res).property('statusCode').eq(200);
42-
});
41+
expect(res).property('statusCode').eq(200);
42+
});
4343

4444
it('successfully', async () => {
4545
const walletId = uuid.v4();

server/models/Trust.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-unused-vars */
12
const Joi = require('joi');
23
const log = require('loglevel');
34
const TrustRepository = require('../repositories/TrustRepository');
@@ -25,6 +26,8 @@ class Trust {
2526
request_type,
2627
offset,
2728
limit,
29+
sort_by,
30+
order
2831
}) {
2932
const filter = {
3033
and: [
@@ -52,7 +55,7 @@ class Trust {
5255
/*
5356
* Get all trust relationships by filters, setting filter to undefined to allow all data
5457
*/
55-
async getAllTrustRelationships({ walletId, state, type, request_type, offset, limit }) {
58+
async getAllTrustRelationships({ walletId, state, type, request_type, offset, limit, sort_by, order }) {
5659

5760
const filter = {
5861
and: [
@@ -68,7 +71,7 @@ class Trust {
6871
if (request_type) {
6972
filter.and.push({ request_type });
7073
}
71-
return this._trustRepository.getAllByFilter(filter, { offset, limit });
74+
return this._trustRepository.getAllByFilter(filter, { offset, limit, sort_by, order });
7275
}
7376

7477
/*

server/repositories/TrustRepository.js

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,64 +76,76 @@ class TrustRepository extends BaseRepository {
7676
promise = promise.offset(limitOptions.offset);
7777
}
7878

79+
let order = 'desc';
80+
let column = 'wallet_trust.created_at';
81+
82+
if (limitOptions) {
83+
if (limitOptions.order) {
84+
order = limitOptions.order;
85+
}
86+
if (limitOptions.sort_by) {
87+
column = limitOptions.sort_by;
88+
}
89+
}
90+
promise = promise.orderBy(column, order);
91+
7992
return promise;
8093
}
8194

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
95+
async getAllByFilter(filter, limitOptions) {
96+
const subquery = this._session.getDB()
97+
.select(
98+
'wallet_trust.id',
99+
'wallet_trust.actor_wallet_id',
100+
'wallet_trust.target_wallet_id',
101+
'wallet_trust.type',
102+
'wallet_trust.originator_wallet_id',
103+
'wallet_trust.request_type',
104+
'wallet_trust.state',
105+
'wallet_trust.created_at',
106+
'wallet_trust.updated_at',
107+
'wallet_trust.active',
108+
'originator_wallet.name as originating_wallet',
109+
'actor_wallet.name as actor_wallet',
110+
'target_wallet.name as target_wallet',
111+
)
112+
.from('wallet_trust')
113+
.leftJoin('wallet as originator_wallet', 'wallet_trust.originator_wallet_id', '=', 'originator_wallet.id')
114+
.leftJoin('wallet as actor_wallet', 'wallet_trust.actor_wallet_id', '=', 'actor_wallet.id')
115+
.leftJoin('wallet as target_wallet', 'wallet_trust.target_wallet_id', '=', 'target_wallet.id')
116+
.where((builder) => this.whereBuilder(filter, builder))
117+
.distinctOn('wallet_trust.id')
118+
.orderBy('wallet_trust.id', 'asc');
121119

122-
// get the total count (before applying limit and offset options)
123-
const count = await this._session.getDB().from(promise.as('p')).count('*');
120+
let derivedTable = this._session.getDB().select('*').from(subquery.as('subquery'));
124121

125-
if (limitOptions && limitOptions.limit) {
126-
promise = promise.limit(limitOptions.limit);
127-
}
122+
let order = 'desc';
123+
let column = 'created_at';
128124

129-
if (limitOptions && limitOptions.offset) {
130-
promise = promise.offset(limitOptions.offset);
125+
if (limitOptions) {
126+
if (limitOptions.order) {
127+
order = limitOptions.order;
131128
}
129+
if (limitOptions.sort_by) {
130+
column = limitOptions.sort_by;
131+
}
132+
}
132133

133-
const result = await promise;
134+
derivedTable = derivedTable.orderBy(column, order);
134135

135-
return { result, count: +count[0].count };
136+
if (limitOptions && limitOptions.limit) {
137+
derivedTable = derivedTable.limit(limitOptions.limit);
136138
}
139+
if (limitOptions && limitOptions.offset) {
140+
derivedTable = derivedTable.offset(limitOptions.offset);
141+
}
142+
143+
const result = await derivedTable;
144+
145+
const {count} = result[0];
146+
147+
return { result, count: +count };
148+
}
137149
}
138150

139151
module.exports = TrustRepository;

server/repositories/WalletRepository.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class WalletRepository extends BaseRepository {
6464
created_at_end_date,
6565
getCount,
6666
) {
67-
let query = this._session
67+
let query = this._session
6868
.getDB()
6969
.select('id', 'name', 'about', 'logo_url', 'created_at')
7070
.table('wallet')
@@ -144,7 +144,6 @@ class WalletRepository extends BaseRepository {
144144
}
145145

146146
const wallets = await query;
147-
148147
if (getCount) {
149148
const count = await countQuery;
150149
return { wallets, count: +count[0].count };

server/services/TrustService.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TrustService {
1313

1414
async getTrustRelationships(
1515
loggedInWalletId,
16-
{ walletId, state, type, request_type, offset, limit },
16+
{ walletId, state, type, request_type, offset, limit,sort_by, order },
1717
) {
1818
// check if wallet exists first
1919
// throws error if no wallet matching walletId exists
@@ -27,6 +27,8 @@ class TrustService {
2727
request_type,
2828
offset,
2929
limit,
30+
sort_by,
31+
order
3032
});
3133
}
3234

@@ -37,6 +39,9 @@ class TrustService {
3739
request_type,
3840
offset,
3941
limit,
42+
sort_by,
43+
order,
44+
4045
}) {
4146
return this._trust.getAllTrustRelationships({
4247
walletId,
@@ -45,6 +50,8 @@ class TrustService {
4550
request_type,
4651
offset,
4752
limit,
53+
sort_by,
54+
order
4855
});
4956
}
5057

server/services/TrustService.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ describe('TrustService', () => {
3838
request_type: 'request_type',
3939
limit: 1,
4040
offset: 0,
41+
sort_by: 'sort_by',
42+
order: 'order',
4143
},
4244
);
4345

@@ -55,6 +57,8 @@ describe('TrustService', () => {
5557
request_type: 'request_type',
5658
limit: 1,
5759
offset: 0,
60+
sort_by: 'sort_by',
61+
order: 'order'
5862
}),
5963
).eql(true);
6064
});

0 commit comments

Comments
 (0)