Skip to content

Commit

Permalink
updated function of query and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwaniYDV committed Mar 10, 2020
1 parent 2c64f7b commit a6b4a42
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
25 changes: 11 additions & 14 deletions app/api/server/v1/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,25 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server';

API.v1.addRoute('roles.list', { authRequired: true }, {
get() {
const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch();
const { updatedSince } = this.queryParams;

return API.v1.success({ roles });
},
});
if (!updatedSince) {
const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch();

// api to to get updated roles after a date(in ISODate format)
API.v1.addRoute('roles.listByUpdatedDate', { authRequired: true }, {
get() {
const { updatedAfter } = this.queryParams;
return API.v1.success({ roles });
}

let updatedAfterDate;
if (updatedAfter) {
if (isNaN(Date.parse(updatedAfter))) {
throw new Meteor.Error('error-updatedAfter-param-invalid', 'The "updatedAfter" query parameter must be a valid date.');
let updatedSinceDate;
if (updatedSince) {
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
} else {
updatedAfterDate = new Date(updatedAfter);
updatedSinceDate = new Date(updatedSince);
}
}

return API.v1.success({
roles: Roles.findByUpdatedDate(updatedAfterDate, { fields: API.v1.defaultFieldsToExclude }).fetch(),
roles: Roles.findByUpdatedDate(updatedSinceDate, { fields: API.v1.defaultFieldsToExclude }).fetch(),
});
},
});
Expand Down
24 changes: 24 additions & 0 deletions tests/end-to-end/api/13-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ describe('[Roles]', function() {
})
.end(done);
});

it('should return an array of roles which are updated after updatedSice date when search by "updatedSince" query parameter', (done) => {
request.get(api('roles.list?updatedSince=2018-11-27T13:52:01Z'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('update').and.to.be.an('array');
expect(res.body).to.have.property('remove').and.to.be.an('array');
})
.end(done);
});

it('should return an error when updatedSince query parameter is not a valid ISODate string', (done) => {
request.get(api('roles.list?updatedSince=fsafdf'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
});
});

describe('POST [/roles.create]', () => {
Expand Down

0 comments on commit a6b4a42

Please sign in to comment.