Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/events/projects/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ describe('projectUpdatedKafkaHandler', () => {
index: ES_PROJECT_INDEX,
type: ES_PROJECT_TYPE,
id: project.id,
body: {
doc: project.get({ plain: true }),
},
body: project.get({ plain: true }),
refresh: 'wait_for',
});
});

Expand Down
27 changes: 22 additions & 5 deletions src/routes/milestones/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Sequelize from 'sequelize';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import util from '../../util';
import validateTimeline from '../../middlewares/validateTimeline';
import { EVENT, MILESTONE_STATUS } from '../../constants';
import { EVENT, MILESTONE_STATUS, ADMIN_ROLES } from '../../constants';
import models from '../../models';

const permissions = tcMiddleware.permissions;
Expand Down Expand Up @@ -185,8 +185,23 @@ module.exports = [
}
}

if (entityToUpdate.completionDate && entityToUpdate.completionDate < milestone.startDate) {
const apiErr = new Error('The milestone completionDate should be greater or equal than the startDate.');
if (entityToUpdate.completionDate || entityToUpdate.actualStartDate) {
if (!util.hasPermission({ topcoderRoles: ADMIN_ROLES }, req.authUser)) {
const apiErr = new Error('You are not authorised to perform this action');
apiErr.status = 403;
return Promise.reject(apiErr);
}
}

if (
entityToUpdate.completionDate &&
(entityToUpdate.actualStartDate || milestone.actualStartDate) &&
moment.utc(entityToUpdate.completionDate).isBefore(
moment.utc(entityToUpdate.actualStartDate || milestone.actualStartDate),
'day',
)
) {
const apiErr = new Error('The milestone completionDate should be greater or equal to actualStartDate.');
apiErr.status = 422;
return Promise.reject(apiErr);
}
Expand All @@ -208,7 +223,8 @@ module.exports = [
// if status has changed to be completed, set the compeltionDate if not provided
if (entityToUpdate.status === MILESTONE_STATUS.COMPLETED) {
entityToUpdate.completionDate = entityToUpdate.completionDate ? entityToUpdate.completionDate : today;
entityToUpdate.duration = entityToUpdate.completionDate.diff(entityToUpdate.actualStartDate, 'days') + 1;
entityToUpdate.duration = moment.utc(entityToUpdate.completionDate)
.diff(entityToUpdate.actualStartDate, 'days') + 1;
}
// if status has changed to be active, set the startDate to today
if (entityToUpdate.status === MILESTONE_STATUS.ACTIVE) {
Expand All @@ -233,7 +249,8 @@ module.exports = [

// if completionDate has changed
if (!statusChanged && completionDateChanged) {
entityToUpdate.duration = entityToUpdate.completionDate.diff(entityToUpdate.actualStartDate, 'days') + 1;
entityToUpdate.duration = moment.utc(entityToUpdate.completionDate)
.diff(entityToUpdate.actualStartDate, 'days') + 1;
entityToUpdate.status = MILESTONE_STATUS.COMPLETED;
}

Expand Down
55 changes: 52 additions & 3 deletions src/routes/milestones/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ describe('UPDATE Milestone', () => {
param: {
name: 'Milestone 1-updated',
duration: 3,
completionDate: '2018-05-16T00:00:00.000Z',
description: 'description-updated',
status: 'draft',
type: 'type1-updated',
Expand Down Expand Up @@ -302,6 +301,30 @@ describe('UPDATE Milestone', () => {
.expect(403, done);
});

it('should return 403 for non-admin member updating the completionDate', (done) => {
const newBody = _.cloneDeep(body);
newBody.param.completionDate = '2019-01-16T00:00:00.000Z';
request(server)
.patch('/v4/timelines/1/milestones/1')
.set({
Authorization: `Bearer ${testUtil.jwts.manager}`,
})
.send(newBody)
.expect(403, done);
});

it('should return 403 for non-admin member updating the actualStartDate', (done) => {
const newBody = _.cloneDeep(body);
newBody.param.actualStartDate = '2018-05-15T00:00:00.000Z';
request(server)
.patch('/v4/timelines/1/milestones/1')
.set({
Authorization: `Bearer ${testUtil.jwts.manager}`,
})
.send(newBody)
.expect(403, done);
});

it('should return 404 for non-existed timeline', (done) => {
request(server)
.patch('/v4/timelines/1234/milestones/1')
Expand Down Expand Up @@ -490,20 +513,22 @@ describe('UPDATE Milestone', () => {
});

it('should return 200 for admin', (done) => {
const newBody = _.cloneDeep(body);
newBody.param.completionDate = '2018-05-15T00:00:00.000Z';
request(server)
.patch('/v4/timelines/1/milestones/1')
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.send(body)
.send(newBody)
.expect(200)
.end((err, res) => {
const resJson = res.body.result.content;
should.exist(resJson.id);
resJson.name.should.be.eql(body.param.name);
resJson.description.should.be.eql(body.param.description);
resJson.duration.should.be.eql(body.param.duration);
resJson.completionDate.should.be.eql(body.param.completionDate);
resJson.completionDate.should.be.eql(newBody.param.completionDate);
resJson.status.should.be.eql(body.param.status);
resJson.type.should.be.eql(body.param.type);
resJson.details.should.be.eql({
Expand Down Expand Up @@ -1061,6 +1086,30 @@ describe('UPDATE Milestone', () => {
.end(done);
});

it('should return 200 for admin updating the completionDate', (done) => {
const newBody = _.cloneDeep(body);
newBody.param.completionDate = '2018-05-16T00:00:00.000Z';
request(server)
.patch('/v4/timelines/1/milestones/1')
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.send(newBody)
.expect(200, done);
});

it('should return 200 for admin updating the actualStartDate', (done) => {
const newBody = _.cloneDeep(body);
newBody.param.actualStartDate = '2018-05-15T00:00:00.000Z';
request(server)
.patch('/v4/timelines/1/milestones/1')
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.send(newBody)
.expect(200, done);
});

it('should return 200 for connect manager', (done) => {
request(server)
.patch('/v4/timelines/1/milestones/1')
Expand Down