Skip to content

Commit

Permalink
feat: added sort_by query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
xianglupeng committed Sep 4, 2023
1 parent 0e2f442 commit 426cad7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions server/handlers/transferHandler/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const transferGetQuerySchema = Joi.object({
after: Joi.date().iso(),
limit: Joi.number().min(1).max(1000),
offset: Joi.number().min(0).integer().default(0),
sort_by: Joi.string().pattern(new RegExp(`^(-)?(${Object.values(TransferEnums.SORT).join('|')})$`)).optional(),
});

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion server/models/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Transfer {
transferId,
before,
after,
sort_by,
}) {
const filter = {
and: [],
Expand Down Expand Up @@ -97,7 +98,7 @@ class Transfer {
if (after) {
filter.and.push({ after: { 'transfer.created_at': after } });
}
return this.getByFilter(filter, { offset, limit });
return this.getByFilter(filter, { offset, limit, sort_by });
}

/*
Expand Down
3 changes: 2 additions & 1 deletion server/models/Transfer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ describe('Transfer Model', () => {
offset: 0,
state,
walletId,
sort_by: null,
});

expect(result).eql({transfers:[{id: transferId}]});
Expand All @@ -159,7 +160,7 @@ describe('Transfer Model', () => {
{ 'transfer.id': transferId },
],
},
{ limit: 10, offset: 0 },
{ limit: 10, offset: 0, sort_by: null },
);
});

Expand Down
10 changes: 10 additions & 0 deletions server/repositories/TransferRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ class TransferRepository extends BaseRepository {
)
.where((builder) => this.whereBuilder(filter, builder));

if (limitOptions && limitOptions.sort_by) {
const column = limitOptions.sort_by.startsWith('-') ?
limitOptions.sort_by.slice(1) : limitOptions.sort_by;
const order = limitOptions.sort_by.startsWith('-') ? 'desc' : 'asc';
promise = promise.orderBy(column, order);
} else {
// sort by created time by default
promise = promise.orderBy('transfer.created_at', 'desc');
}

// get the total count (before applying limit and offset options)
const count = await this._session.getDB().from(promise.as('p')).count('*');

Expand Down
5 changes: 3 additions & 2 deletions server/services/TransferService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TransferService {
}

async getByFilter(query, walletLoginId) {
const { state, wallet, limit, offset, before, after } = query;
const { state, wallet, limit, offset, before, after, sort_by } = query;

let walletId;

Expand All @@ -29,7 +29,8 @@ class TransferService {
limit,
walletLoginId,
before,
after
after,
sort_by
});


Expand Down
2 changes: 2 additions & 0 deletions server/services/TransferService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('TransferService', () => {
walletId: undefined,
before: undefined,
after: undefined,
sort_by: undefined,
}),
).eql(true);
expect(walletGetByIdOrNameStub.notCalled).eql(true);
Expand Down Expand Up @@ -194,6 +195,7 @@ describe('TransferService', () => {
walletId: 'id',
before,
after,
sort_by: undefined,
}),
).eql(true);
expect(walletGetByIdOrNameStub.calledOnceWithExactly('wallet')).eql(true);
Expand Down
10 changes: 10 additions & 0 deletions server/utils/transfer-enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ TransferEnums.STATE = {
failed: 'failed',
};

TransferEnums.SORT = {
id: 'id',
source_wallet_id: 'source_wallet_id',
destination_wallet_id: 'destination_wallet_id',
originator_wallet_id: 'originator_wallet_id',
created_at: 'created_at',
close_at: 'closed_at',
state: 'state',
};

module.exports = TransferEnums;

0 comments on commit 426cad7

Please sign in to comment.