Skip to content

Commit aed6b4d

Browse files
author
Vikas Agarwal
committed
Fixed admin index endpoint to fetch the phases along with project
Fixed project upgrade endpoint to set duration of the project and not send product added event separately
1 parent 96d6982 commit aed6b4d

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed

package-lock.json

Lines changed: 55 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"jsonwebtoken": "^8.3.0",
5454
"lodash": "^4.16.4",
5555
"method-override": "^2.3.9",
56+
"moment": "^2.22.2",
5657
"pg": "^4.5.5",
5758
"pg-native": "^1.10.1",
5859
"sequelize": "^3.23.0",

src/models/project.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ module.exports = function defineProject(sequelize, DataTypes) {
155155
.then(projects => ({ rows: projects, count }));
156156
});
157157
},
158-
findProjectRange(models, startId, endId, fields) {
158+
findProjectRange(models, startId, endId, fields, raw = true) {
159159
return this.findAll({
160160
where: { id: { $between: [startId, endId] } },
161161
attributes: _.get(fields, 'projects', null),
162-
raw: true,
162+
raw,
163163
include: [{
164164
model: models.ProjectPhase,
165165
as: 'phases',

src/routes/admin/project-index-create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = [
5050
});
5151

5252
const eClient = util.getElasticSearchClient();
53-
return models.Project.findProjectRange(models, projectIdStart, projectIdEnd, fields)
53+
return models.Project.findProjectRange(models, projectIdStart, projectIdEnd, fields, false)
5454
.then((_projects) => {
5555
const projects = _projects.map((_project) => {
5656
const project = _project;

src/routes/projectUpgrade/create.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* API to upgrade projects
55
*/
66
import _ from 'lodash';
7+
import moment from 'moment';
78
import validate from 'express-validation';
89
import Joi from 'joi';
910
import { middleware as tcMiddleware } from 'tc-core-library-js';
@@ -59,11 +60,11 @@ function applyTemplate(template, source, destination) {
5960
}
6061
// other sub sections which requires generic handling
6162
if (subSection.fieldName) { // if sub section contains field name, directly copy its value
62-
console.log(subSection.fieldName, _.get(source, subSection.fieldName));
63+
// console.log(subSection.fieldName, _.get(source, subSection.fieldName));
6364
_.set(destination, subSection.fieldName, _.get(source, subSection.fieldName));
6465
} else if (subSection.type === 'questions') { // if questions typed subsection
6566
subSection.questions.forEach((question) => { // iterate throught each question to copy its value
66-
console.log(question.fieldName, _.get(source, question.fieldName));
67+
// console.log(question.fieldName, _.get(source, question.fieldName));
6768
_.set(destination, question.fieldName, _.get(source, question.fieldName));
6869
});
6970
}
@@ -90,6 +91,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
9091
const previousValue = _.clone(project.get({ plain: true }));
9192
await models.sequelize.transaction(async (transaction) => {
9293
const products = project.details.products;
94+
9395
const projectTemplate = await models.ProjectTemplate.find({
9496
where: { key: products[0] },
9597
attributes: ['id', 'phases'],
@@ -104,6 +106,11 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
104106
const endDate = projectCompleted
105107
? (await findCompletedProjectEndDate(project.id, transaction)) || project.updatedAt
106108
: null;
109+
// calculates the duration
110+
const projectDuration = endDate
111+
? moment(endDate).diff(project.createdAt, 'days')
112+
: moment().diff(moment(project.createdAt), 'days');
113+
107114
let phaseStatus = project.status;
108115
// maps the in_review status to the draft status for the phase
109116
phaseStatus = phaseStatus === PROJECT_STATUS.IN_REVIEW ? PROJECT_STATUS.DRAFT : phaseStatus;
@@ -116,6 +123,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
116123
name: phaseName || phaseObject.name || '',
117124
status: phaseStatus,
118125
startDate: project.createdAt,
126+
duration: projectDuration,
119127
endDate,
120128
budget: project.details && project.details.appDefinition && project.details.appDefinition.budget,
121129
progress: projectCompleted ? 100 : 0,
@@ -172,25 +180,16 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
172180
await project.update({ version: 'v3', templateId: projectTemplate.id }, { transaction });
173181
});
174182
newPhasesAndProducts.forEach(({ phase, products }) => {
183+
const phaseJSON = phase.toJSON();
184+
phaseJSON.products = products;
175185
// Send events to buses (ProjectPhase)
176186
req.log.debug('Sending event to RabbitMQ bus for project phase %d', phase.id);
177187
req.app.services.pubsub.publish(EVENT.ROUTING_KEY.PROJECT_PHASE_ADDED,
178-
phase,
188+
phaseJSON,
179189
{ correlationId: req.id },
180190
);
181191
req.log.debug('Sending event to Kafka bus for project phase %d', phase.id);
182-
req.app.emit(EVENT.ROUTING_KEY.PROJECT_PHASE_ADDED, { req, created: phase });
183-
184-
products.forEach((newPhaseProduct) => {
185-
// Send events to buses (PhaseProduct)
186-
req.log.debug('Sending event to RabbitMQ bus for phase product %d', newPhaseProduct.id);
187-
req.app.services.pubsub.publish(EVENT.ROUTING_KEY.PROJECT_PHASE_PRODUCT_ADDED,
188-
newPhaseProduct,
189-
{ correlationId: req.id },
190-
);
191-
req.log.debug('Sending event to Kafka bus for phase product %d', newPhaseProduct.id);
192-
req.app.emit(EVENT.ROUTING_KEY.PROJECT_PHASE_PRODUCT_ADDED, { req, created: newPhaseProduct });
193-
});
192+
req.app.emit(EVENT.ROUTING_KEY.PROJECT_PHASE_ADDED, { req, created: phaseJSON });
194193
});
195194

196195
// Send events to buses (Project)

src/routes/projectUpgrade/create.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ describe('Project upgrade', () => {
296296
}
297297

298298
expect(server.services.pubsub.publish.calledWith('project.phase.added')).to.be.true;
299-
expect(server.services.pubsub.publish.calledWith('project.phase.product.added')).to.be.true;
299+
// we should not raise product added event as when we are adding a phase, it automatically adds the product
300+
// product added event should be raised only when a new product is added to an existing phase
301+
expect(server.services.pubsub.publish.calledWith('project.phase.product.added')).to.be.false;
300302
expect(server.services.pubsub.publish.calledWith('project.updated')).to.be.true;
301303
};
302304

0 commit comments

Comments
 (0)