From 63efe639da1e63ad9fc3069541443704f8323aab Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Sat, 15 Feb 2020 16:45:30 +0530 Subject: [PATCH 1/9] [NEW] Route to get updated roles after a date --- app/api/server/v1/roles.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index 22a0d543ce34..1fc9d396ea4d 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -7,7 +7,20 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server'; API.v1.addRoute('roles.list', { authRequired: true }, { get() { - const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); + const roles = Roles.find({}, { fields: { } }).fetch(); + + return API.v1.success({ roles }); + }, +}); + +// api to to get updated roles after a date(in ISODate format) +API.v1.addRoute('roles.listByUpdatedDate', { authRequired: true }, { + get() { + check(this.bodyParams, { + _updatedAt: String, + }); + + const roles = Roles.find({ _updatedAt: { $gte: new Date(this.bodyParams._updatedAt) } }).fetch(); return API.v1.success({ roles }); }, From 69a1ab2dbf1b0d4c26b11369436565068669a77a Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Sat, 15 Feb 2020 16:48:10 +0530 Subject: [PATCH 2/9] some minor fixes --- app/api/server/v1/roles.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index 1fc9d396ea4d..120b9df450e9 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -7,7 +7,7 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server'; API.v1.addRoute('roles.list', { authRequired: true }, { get() { - const roles = Roles.find({}, { fields: { } }).fetch(); + const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); return API.v1.success({ roles }); }, From 40f6590ba3ab1a1fc4e9e93d7934596b0d263cd0 Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Sat, 15 Feb 2020 16:49:43 +0530 Subject: [PATCH 3/9] changed variable --- app/api/server/v1/roles.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index 120b9df450e9..72c61fbd7055 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -17,10 +17,10 @@ API.v1.addRoute('roles.list', { authRequired: true }, { API.v1.addRoute('roles.listByUpdatedDate', { authRequired: true }, { get() { check(this.bodyParams, { - _updatedAt: String, + updatedAfter: String, }); - const roles = Roles.find({ _updatedAt: { $gte: new Date(this.bodyParams._updatedAt) } }).fetch(); + const roles = Roles.find({ _updatedAt: { $gte: new Date(this.bodyParams.updatedAfter) } }).fetch(); return API.v1.success({ roles }); }, From 7e173b406ba63b3425da7552c1ce7bf7f93a25de Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Thu, 27 Feb 2020 22:54:18 +0530 Subject: [PATCH 4/9] added checks to date variable and added function in model --- app/api/server/v1/roles.js | 19 +++++++++++++------ app/models/server/models/Roles.js | 8 ++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index 72c61fbd7055..463f94808434 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -16,13 +16,20 @@ API.v1.addRoute('roles.list', { authRequired: true }, { // api to to get updated roles after a date(in ISODate format) API.v1.addRoute('roles.listByUpdatedDate', { authRequired: true }, { get() { - check(this.bodyParams, { - updatedAfter: String, - }); - - const roles = Roles.find({ _updatedAt: { $gte: new Date(this.bodyParams.updatedAfter) } }).fetch(); + const { updatedAfter } = this.queryParams; + + 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.'); + } else { + updatedAfterDate = new Date(updatedAfter); + } + } - return API.v1.success({ roles }); + return API.v1.success({ + roles: Roles.findByUpdatedDate(updatedAfterDate, { fields: API.v1.defaultFieldsToExclude }), + }); }, }); diff --git a/app/models/server/models/Roles.js b/app/models/server/models/Roles.js index a2e9fa3418f6..61b9db75e97c 100644 --- a/app/models/server/models/Roles.js +++ b/app/models/server/models/Roles.js @@ -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).fetch(); + } + canAddUserToRole(uid, roleName, scope) { const role = this.findOne({ _id: roleName }, { fields: { scope: 1 } }); if (!role) { From 2c64f7b584cec9883be2ab8a79836aa135646e64 Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Tue, 10 Mar 2020 19:25:14 +0530 Subject: [PATCH 5/9] updated fetch --- app/api/server/v1/roles.js | 2 +- app/models/server/models/Roles.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index 463f94808434..aba5442fb1f0 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -28,7 +28,7 @@ API.v1.addRoute('roles.listByUpdatedDate', { authRequired: true }, { } return API.v1.success({ - roles: Roles.findByUpdatedDate(updatedAfterDate, { fields: API.v1.defaultFieldsToExclude }), + roles: Roles.findByUpdatedDate(updatedAfterDate, { fields: API.v1.defaultFieldsToExclude }).fetch(), }); }, }); diff --git a/app/models/server/models/Roles.js b/app/models/server/models/Roles.js index 61b9db75e97c..2715ab05d7c1 100644 --- a/app/models/server/models/Roles.js +++ b/app/models/server/models/Roles.js @@ -90,7 +90,7 @@ export class Roles extends Base { _updatedAt: { $gte: new Date(updatedAfterDate) }, }; - return this.find(query, options).fetch(); + return this.find(query, options); } canAddUserToRole(uid, roleName, scope) { From a6b4a4287d10efcdf898093b207450d7a8848941 Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Tue, 10 Mar 2020 20:03:55 +0530 Subject: [PATCH 6/9] updated function of query and added tests --- app/api/server/v1/roles.js | 25 +++++++++++-------------- tests/end-to-end/api/13-roles.js | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index aba5442fb1f0..b72de646fa12 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -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(), }); }, }); diff --git a/tests/end-to-end/api/13-roles.js b/tests/end-to-end/api/13-roles.js index 26b867ee154a..9ef3f6ce4823 100644 --- a/tests/end-to-end/api/13-roles.js +++ b/tests/end-to-end/api/13-roles.js @@ -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]', () => { From 47876eba36a44db01b8a7f7397369865c7a00186 Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Tue, 10 Mar 2020 21:09:29 +0530 Subject: [PATCH 7/9] minor fix --- app/api/server/v1/roles.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index b72de646fa12..b65aa6dffb86 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -7,14 +7,14 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server'; API.v1.addRoute('roles.list', { authRequired: true }, { get() { - const { updatedSince } = this.queryParams; - - if (!updatedSince) { + if (!this.queryParams) { const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); return API.v1.success({ roles }); } + const { updatedSince } = this.queryParams; + let updatedSinceDate; if (updatedSince) { if (isNaN(Date.parse(updatedSince))) { From 93c0432979a54534f2d1ce4d815d15c8d61c3bf3 Mon Sep 17 00:00:00 2001 From: Ashwani Yadav Date: Tue, 10 Mar 2020 22:47:12 +0530 Subject: [PATCH 8/9] removed unnecisary condition --- app/api/server/v1/roles.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index b65aa6dffb86..d55bd97c8ee2 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -7,21 +7,20 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server'; API.v1.addRoute('roles.list', { authRequired: true }, { get() { - if (!this.queryParams) { + const { updatedSince } = this.queryParams; + + if (!updatedSince) { const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); return API.v1.success({ roles }); } - const { updatedSince } = this.queryParams; - 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 { - updatedSinceDate = new Date(updatedSince); - } + + if (isNaN(Date.parse(updatedSince))) { + throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.'); + } else { + updatedSinceDate = new Date(updatedSince); } return API.v1.success({ From bca82e4ef1c7e563d98343712a8546a2a3d9aa74 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Tue, 10 Mar 2020 17:40:55 -0300 Subject: [PATCH 9/9] Final tweaks --- app/api/server/v1/roles.js | 21 +++++++++++---------- tests/end-to-end/api/13-roles.js | 11 +++++++---- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/api/server/v1/roles.js b/app/api/server/v1/roles.js index d55bd97c8ee2..7772f80e29d7 100644 --- a/app/api/server/v1/roles.js +++ b/app/api/server/v1/roles.js @@ -7,24 +7,25 @@ import { getUsersInRole, hasPermission } from '../../../authorization/server'; API.v1.addRoute('roles.list', { authRequired: true }, { get() { - const { updatedSince } = this.queryParams; - - if (!updatedSince) { - const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); + const roles = Roles.find({}, { fields: { _updatedAt: 0 } }).fetch(); - return API.v1.success({ roles }); - } + return API.v1.success({ roles }); + }, +}); - let updatedSinceDate; +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.'); - } else { - updatedSinceDate = new Date(updatedSince); } return API.v1.success({ - roles: Roles.findByUpdatedDate(updatedSinceDate, { fields: API.v1.defaultFieldsToExclude }).fetch(), + roles: { + update: Roles.findByUpdatedDate(new Date(updatedSince), { fields: API.v1.defaultFieldsToExclude }).fetch(), + remove: Roles.trashFindDeletedAfter(new Date(updatedSince)).fetch(), + }, }); }, }); diff --git a/tests/end-to-end/api/13-roles.js b/tests/end-to-end/api/13-roles.js index 9ef3f6ce4823..b3a3b1611375 100644 --- a/tests/end-to-end/api/13-roles.js +++ b/tests/end-to-end/api/13-roles.js @@ -33,22 +33,25 @@ describe('[Roles]', function() { }) .end(done); }); + }); + 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.list?updatedSince=2018-11-27T13:52:01Z')) + 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('update').and.to.be.an('array'); - expect(res.body).to.have.property('remove').and.to.be.an('array'); + 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.list?updatedSince=fsafdf')) + request.get(api('roles.sync?updatedSince=fsafdf')) .set(credentials) .expect('Content-Type', 'application/json') .expect(400)