Skip to content

Commit 2887ef8

Browse files
committed
When creating a project from a project template, define phases' startDate, endDate and duration.
1 parent a2cced2 commit 2887ef8

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/routes/projects/create.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import validate from 'express-validation';
44
import _ from 'lodash';
55
import Joi from 'joi';
66
import config from 'config';
7+
import moment from 'moment';
78

89
import models from '../../models';
910
import { PROJECT_MEMBER_ROLE, PROJECT_STATUS, PROJECT_PHASE_STATUS, USER_ROLE, EVENT, REGEX } from '../../constants';
@@ -90,12 +91,16 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
9091
productTemplates.forEach((pt) => {
9192
productTemplateMap[pt.id] = pt;
9293
});
93-
return Promise.all(_.map(phases, (phase, phaseIdx) =>
94+
return Promise.all(_.map(phases, (phase, phaseIdx) => {
95+
const duration = _.get(phase, 'duration', 1);
96+
const startDate = moment.utc();
9497
// Create phase
95-
models.ProjectPhase.create({
98+
return models.ProjectPhase.create({
9699
projectId: newProject.id,
97100
name: _.get(phase, 'name', `Stage ${phaseIdx}`),
98-
duration: _.get(phase, 'duration', 0),
101+
duration,
102+
startDate: startDate.format(),
103+
endDate: moment.utc(startDate).add(duration - 1, 'days').format(),
99104
status: _.get(phase, 'status', PROJECT_PHASE_STATUS.DRAFT),
100105
budget: _.get(phase, 'budget', 0),
101106
updatedBy: req.authUser.userId,
@@ -121,8 +126,8 @@ function createProjectAndPhases(req, project, projectTemplate, productTemplates)
121126
result.newPhases.push(newPhaseJson);
122127
return Promise.resolve();
123128
});
124-
}),
125-
));
129+
});
130+
}));
126131
}).then(() => Promise.resolve(result));
127132
}
128133

src/routes/projects/create.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-unused-expressions */
22
import _ from 'lodash';
33
import chai from 'chai';
4+
import moment from 'moment';
45
import sinon from 'sinon';
56
import request from 'supertest';
67

@@ -85,6 +86,7 @@ describe('Project create', () => {
8586
phases: {
8687
phase1: {
8788
name: 'phase 1',
89+
duration: 5,
8890
products: [
8991
{
9092
id: 21,
@@ -116,6 +118,7 @@ describe('Project create', () => {
116118
1: {
117119
name: 'Design Stage',
118120
status: 'open',
121+
duration: 10,
119122
details: {
120123
description: 'detailed description',
121124
},
@@ -130,6 +133,7 @@ describe('Project create', () => {
130133
2: {
131134
name: 'Development Stage',
132135
status: 'open',
136+
duration: 20,
133137
products: [
134138
{
135139
id: 23,
@@ -440,6 +444,9 @@ describe('Project create', () => {
440444
const phases = _.sortBy(resJson.phases, p => p.name);
441445
phases[0].name.should.be.eql('Design Stage');
442446
phases[0].status.should.be.eql('open');
447+
phases[0].startDate.should.be.a('string');
448+
phases[0].duration.should.be.eql(10);
449+
new Date(phases[0].endDate).should.be.eql(moment.utc(phases[0].startDate).add(9, 'days').toDate());
443450
expect(phases[0].details).to.be.empty;
444451
phases[0].products.should.have.lengthOf(1);
445452
phases[0].products[0].name.should.be.eql('product 1');

0 commit comments

Comments
 (0)