Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Route to get updated roles after a date #16610

Merged
merged 11 commits into from
Mar 20, 2020
17 changes: 17 additions & 0 deletions app/api/server/v1/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ API.v1.addRoute('roles.list', { authRequired: true }, {
},
});

API.v1.addRoute('roles.sync', { authRequired: true }, {
get() {
const { updatedSince } = this.queryParams;

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

return API.v1.success({
roles: {
update: Roles.findByUpdatedDate(new Date(updatedSince), { fields: API.v1.defaultFieldsToExclude }).fetch(),
remove: Roles.trashFindDeletedAfter(new Date(updatedSince)).fetch(),
},
});
},
});

API.v1.addRoute('roles.create', { authRequired: true }, {
post() {
check(this.bodyParams, {
Expand Down
8 changes: 8 additions & 0 deletions app/models/server/models/Roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export class Roles extends Base {
return this.findOne(query, options);
}

findByUpdatedDate(updatedAfterDate, options) {
const query = {
_updatedAt: { $gte: new Date(updatedAfterDate) },
};

return this.find(query, options);
}

canAddUserToRole(uid, roleName, scope) {
const role = this.findOne({ _id: roleName }, { fields: { scope: 1 } });
if (!role) {
Expand Down
27 changes: 27 additions & 0 deletions tests/end-to-end/api/13-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ describe('[Roles]', function() {
});
});

describe('GET [/roles.sync]', () => {
it('should return an array of roles which are updated after updatedSice date when search by "updatedSince" query parameter', (done) => {
request.get(api('roles.sync?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('roles');
expect(res.body.roles).to.have.property('update').and.to.be.an('array');
expect(res.body.roles).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.sync?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]', () => {
it('should create a new role with Users scope', (done) => {
request.post(api('roles.create'))
Expand Down