Skip to content

Commit 2b3cf21

Browse files
committed
Merge branch 'dev' into v5-upgrade
# Conflicts: # src/routes/index.js # src/routes/milestones/update.js # src/routes/projects/create.spec.js # src/routes/projects/update.js
2 parents 17ca11c + 9839c0e commit 2b3cf21

File tree

8 files changed

+42
-18
lines changed

8 files changed

+42
-18
lines changed

config/custom-environment-variables.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@
6464
"REG_STATS": "LOOKER_API_REG_STATS_QUERY_ID",
6565
"BUDGET": "LOOKER_API_BUDGET_QUERY_ID"
6666
}
67-
}
67+
},
68+
"DEFAULT_M2M_USERID": "DEFAULT_M2M_USERID"
6869
}

config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@
6969
"REG_STATS": 1234,
7070
"BUDGET": 123
7171
}
72-
}
72+
},
73+
"DEFAULT_M2M_USERID": -101
7374
}

src/events/projects/index.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ describe('projectUpdatedKafkaHandler', () => {
112112
index: ES_PROJECT_INDEX,
113113
type: ES_PROJECT_TYPE,
114114
id: project.id,
115-
body: {
116-
doc: project.get({ plain: true }),
117-
},
115+
body: project.get({ plain: true }),
116+
refresh: 'wait_for',
118117
});
119118
});
120119

src/routes/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ router.all(
3434
),
3535
);
3636

37+
router.all(
38+
RegExp(`\\/${apiVersion}\\/.*`), (req, res, next) => {
39+
// if it is an M2M call, hard code user id to a deafult value to avoid errors
40+
// Ideally, the m2m token should have unique userId, which may not be an actual user, as well
41+
const isMachineToken = _.get(req, 'authUser.isMachine', false);
42+
if (req.authUser && !req.authUser.userId && isMachineToken) {
43+
req.authUser.userId = config.DEFAULT_M2M_USERID;
44+
}
45+
return next();
46+
},
47+
);
48+
3749
router.route('/v5/projects/metadata/projectTemplates')
3850
.get(require('./projectTemplates/list'));
3951
router.route('/v5/projects/metadata/projectTemplates/:templateId(\\d+)')

src/routes/milestones/update.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,15 @@ module.exports = [
191191
}
192192
}
193193

194-
if (entityToUpdate.completionDate && entityToUpdate.completionDate < milestone.startDate) {
195-
const apiErr = new Error('The milestone completionDate should be greater or equal than the startDate.');
194+
if (
195+
entityToUpdate.completionDate &&
196+
(entityToUpdate.actualStartDate || milestone.actualStartDate) &&
197+
moment.utc(entityToUpdate.completionDate).isBefore(
198+
moment.utc(entityToUpdate.actualStartDate || milestone.actualStartDate),
199+
'day',
200+
)
201+
) {
202+
const apiErr = new Error('The milestone completionDate should be greater or equal to actualStartDate.');
196203
apiErr.status = 400;
197204
return Promise.reject(apiErr);
198205
}
@@ -214,7 +221,8 @@ module.exports = [
214221
// if status has changed to be completed, set the compeltionDate if not provided
215222
if (entityToUpdate.status === MILESTONE_STATUS.COMPLETED) {
216223
entityToUpdate.completionDate = entityToUpdate.completionDate ? entityToUpdate.completionDate : today;
217-
entityToUpdate.duration = entityToUpdate.completionDate.diff(entityToUpdate.actualStartDate, 'days') + 1;
224+
entityToUpdate.duration = moment.utc(entityToUpdate.completionDate)
225+
.diff(entityToUpdate.actualStartDate, 'days') + 1;
218226
}
219227
// if status has changed to be active, set the startDate to today
220228
if (entityToUpdate.status === MILESTONE_STATUS.ACTIVE) {
@@ -239,7 +247,8 @@ module.exports = [
239247

240248
// if completionDate has changed
241249
if (!statusChanged && completionDateChanged) {
242-
entityToUpdate.duration = entityToUpdate.completionDate.diff(entityToUpdate.actualStartDate, 'days') + 1;
250+
entityToUpdate.duration = moment.utc(entityToUpdate.completionDate)
251+
.diff(entityToUpdate.actualStartDate, 'days') + 1;
243252
entityToUpdate.status = MILESTONE_STATUS.COMPLETED;
244253
}
245254

src/routes/projects/create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ module.exports = [
406406
});
407407
// override values
408408
_.assign(project, {
409-
status: PROJECT_STATUS.DRAFT,
409+
status: PROJECT_STATUS.IN_REVIEW,
410410
createdBy: req.authUser.userId,
411411
updatedBy: req.authUser.userId,
412412
lastActivityAt: new Date(),
@@ -465,7 +465,7 @@ module.exports = [
465465
// add to project history asynchronously, don't wait for it to complete
466466
models.ProjectHistory.create({
467467
projectId: newProject.id,
468-
status: PROJECT_STATUS.DRAFT,
468+
status: PROJECT_STATUS.IN_REVIEW,
469469
cancelReason: null,
470470
updatedBy: req.authUser.userId,
471471
}).then(() => req.log.debug('project history created for project %d', newProject.id))

src/routes/projects/create.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ describe('Project create', () => {
404404
should.exist(resJson);
405405
should.exist(resJson.billingAccountId);
406406
should.exist(resJson.name);
407-
resJson.status.should.be.eql('draft');
407+
resJson.status.should.be.eql('in_review');
408408
resJson.type.should.be.eql(body.type);
409409
resJson.version.should.be.eql('v3');
410410
resJson.members.should.have.lengthOf(1);
@@ -459,7 +459,7 @@ describe('Project create', () => {
459459
should.exist(resJson);
460460
should.exist(resJson.billingAccountId);
461461
should.exist(resJson.name);
462-
resJson.status.should.be.eql('draft');
462+
resJson.status.should.be.eql('in_review');
463463
resJson.type.should.be.eql(body.type);
464464
resJson.version.should.be.eql('v2');
465465
resJson.members.should.have.lengthOf(1);
@@ -512,7 +512,7 @@ describe('Project create', () => {
512512
should.exist(resJson);
513513
should.exist(resJson.billingAccountId);
514514
should.exist(resJson.name);
515-
resJson.status.should.be.eql('draft');
515+
resJson.status.should.be.eql('in_review');
516516
resJson.type.should.be.eql(body.type);
517517
resJson.members.should.have.lengthOf(1);
518518
resJson.members[0].role.should.be.eql('customer');
@@ -585,7 +585,7 @@ describe('Project create', () => {
585585
should.exist(resJson);
586586
should.exist(resJson.billingAccountId);
587587
should.exist(resJson.name);
588-
resJson.status.should.be.eql('draft');
588+
resJson.status.should.be.eql('in_review');
589589
resJson.type.should.be.eql(body.type);
590590
resJson.members.should.have.lengthOf(1);
591591
resJson.members[0].role.should.be.eql('customer');
@@ -718,7 +718,7 @@ describe('Project create', () => {
718718
should.exist(resJson);
719719
should.exist(resJson.billingAccountId);
720720
should.exist(resJson.name);
721-
resJson.status.should.be.eql('draft');
721+
resJson.status.should.be.eql('in_review');
722722
resJson.type.should.be.eql(body.type);
723723
resJson.version.should.be.eql('v3');
724724
resJson.members.should.have.lengthOf(1);
@@ -806,7 +806,7 @@ describe('Project create', () => {
806806
should.exist(resJson);
807807
should.exist(resJson.billingAccountId);
808808
should.exist(resJson.name);
809-
resJson.status.should.be.eql('draft');
809+
resJson.status.should.be.eql('in_review');
810810
resJson.type.should.be.eql(body.type);
811811
resJson.members.should.have.lengthOf(1);
812812
resJson.members[0].role.should.be.eql('customer');

src/routes/projects/update.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ const validateUpdates = (existingProject, updatedProps, req) => {
146146
!util.hasRoles(req, [USER_ROLE.MANAGER, USER_ROLE.TOPCODER_ADMIN])) {
147147
errors.push('Don\'t have permission to update \'directProjectId\' property');
148148
}
149-
149+
if ((existingProject.status !== PROJECT_STATUS.DRAFT) && (updatedProps.status === PROJECT_STATUS.DRAFT)) {
150+
errors.push('cannot update a project status to draft');
151+
}
150152
return errors;
151153
};
152154

0 commit comments

Comments
 (0)