Skip to content

Commit c5a01ed

Browse files
committed
feat: removed phase order cascading updates
1 parent 817da90 commit c5a01ed

File tree

4 files changed

+3
-207
lines changed

4 files changed

+3
-207
lines changed

src/routes/phases/create.js

+1-48
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import validate from 'express-validation';
22
import _ from 'lodash';
33
import Joi from 'joi';
4-
import Sequelize from 'sequelize';
54

65
import models from '../../models';
76
import util from '../../util';
@@ -45,7 +44,6 @@ module.exports = [
4544
});
4645

4746
let newProjectPhase = null;
48-
let otherUpdated = null;
4947
models.sequelize.transaction(() => {
5048
req.log.debug('Create Phase - Starting transaction');
5149
return models.Project.findOne({
@@ -73,39 +71,8 @@ module.exports = [
7371
newProjectPhase = _.omit(newProjectPhase, ['deletedAt', 'deletedBy', 'utm']);
7472
});
7573
})
74+
// create product if `productTemplateId` is defined
7675
.then(() => {
77-
req.log.debug('re-ordering the other phases');
78-
79-
if (_.isNil(newProjectPhase.order)) {
80-
return Promise.resolve();
81-
}
82-
83-
// Increase the order of the other phases in the same project,
84-
// which have `order` >= this phase order
85-
return models.ProjectPhase.update({ order: Sequelize.literal('"order" + 1') }, {
86-
where: {
87-
projectId,
88-
id: { $ne: newProjectPhase.id },
89-
order: { $gte: newProjectPhase.order },
90-
},
91-
});
92-
})
93-
.then((updatedCount) => {
94-
if (updatedCount) {
95-
return models.ProjectPhase.findAll({
96-
where: {
97-
projectId,
98-
id: { $ne: newProjectPhase.id },
99-
order: { $gte: newProjectPhase.order },
100-
},
101-
order: [['updatedAt', 'DESC']],
102-
limit: updatedCount[0],
103-
});
104-
}
105-
return Promise.resolve();
106-
})
107-
.then((_otherUpdated) => {
108-
otherUpdated = _otherUpdated || [];
10976
if (_.isNil(data.productTemplateId)) {
11077
return Promise.resolve();
11178
}
@@ -144,20 +111,6 @@ module.exports = [
144111
RESOURCES.PHASE,
145112
newProjectPhase);
146113

147-
// send updated event for all other phases which have been cascading updated
148-
_.map(otherUpdated, phase =>
149-
util.sendResourceToKafkaBus(
150-
req,
151-
EVENT.ROUTING_KEY.PROJECT_PHASE_UPDATED,
152-
RESOURCES.PHASE,
153-
_.assign(_.pick(phase.toJSON(), 'id', 'order', 'updatedBy', 'updatedAt')),
154-
// Pass the same object as original phase even though, the order has changed.
155-
// So far we don't use the order so it's ok. But in general, we should pass
156-
// the original phases. <- TODO
157-
_.assign(_.pick(phase.toJSON(), 'id', 'order', 'updatedBy', 'updatedAt'))),
158-
true, // don't send event to Notification Service as the main event here is updating one phase
159-
);
160-
161114
res.status(201).json(newProjectPhase);
162115
})
163116
.catch((err) => {

src/routes/phases/create.spec.js

+1-43
Original file line numberDiff line numberDiff line change
@@ -279,49 +279,6 @@ describe('Project Phases', () => {
279279
});
280280
});
281281

282-
it('should return 201 if payload has order specified', (done) => {
283-
request(server)
284-
.post(`/v5/projects/${projectId}/phases/`)
285-
.set({
286-
Authorization: `Bearer ${testUtil.jwts.copilot}`,
287-
})
288-
.send(_.assign({ order: 1 }, body))
289-
.expect('Content-Type', /json/)
290-
.expect(201)
291-
.end((err, res) => {
292-
if (err) {
293-
done(err);
294-
} else {
295-
const resJson = res.body;
296-
validatePhase(resJson, body);
297-
resJson.order.should.be.eql(1);
298-
299-
const firstPhaseId = resJson.id;
300-
301-
// Create second phase
302-
request(server)
303-
.post(`/v5/projects/${projectId}/phases/`)
304-
.set({
305-
Authorization: `Bearer ${testUtil.jwts.copilot}`,
306-
})
307-
.send(_.assign({ order: 1 }, body))
308-
.expect('Content-Type', /json/)
309-
.expect(201)
310-
.end((err2, res2) => {
311-
const resJson2 = res2.body;
312-
validatePhase(resJson2, body);
313-
resJson2.order.should.be.eql(1);
314-
315-
models.ProjectPhase.findOne({ where: { id: firstPhaseId } })
316-
.then((firstPhase) => {
317-
firstPhase.order.should.be.eql(2);
318-
done();
319-
});
320-
});
321-
}
322-
});
323-
});
324-
325282
it('should return 201 if payload has productTemplateId specified', (done) => {
326283
request(server)
327284
.post(`/v5/projects/${projectId}/phases/`)
@@ -336,6 +293,7 @@ describe('Project Phases', () => {
336293
done(err);
337294
} else {
338295
const resJson = res.body;
296+
console.log(resJson);
339297
validatePhase(resJson, body);
340298
resJson.products.should.have.length(1);
341299

src/routes/phases/update.js

+1-87
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import validate from 'express-validation';
33
import _ from 'lodash';
44
import Joi from 'joi';
5-
import Sequelize from 'sequelize';
65
import { middleware as tcMiddleware } from 'tc-core-library-js';
76
import models from '../../models';
87
import util from '../../util';
@@ -88,80 +87,9 @@ module.exports = [
8887
}))
8988
.then((updatedPhase) => {
9089
updated = updatedPhase;
91-
92-
// Ignore re-ordering if there's no order specified for this phase
93-
if (_.isNil(updated.order)) {
94-
return Promise.resolve();
95-
}
96-
97-
// Update order of the other phases only if the order was changed
98-
if (previousValue.order === updated.order) {
99-
return Promise.resolve();
100-
}
101-
102-
return models.ProjectPhase.count({
103-
where: {
104-
projectId,
105-
id: { $ne: updated.id },
106-
order: updated.order,
107-
},
108-
})
109-
.then((count) => {
110-
if (count === 0) {
111-
return Promise.resolve();
112-
}
113-
114-
// Increase the order from M to K: if there is an item with order K,
115-
// orders from M+1 to K should be made M to K-1
116-
if (!_.isNil(previousValue.order) && previousValue.order < updated.order) {
117-
return models.ProjectPhase.update({ order: Sequelize.literal('"order" - 1') }, {
118-
where: {
119-
projectId,
120-
id: { $ne: updated.id },
121-
order: { $between: [previousValue.order + 1, updated.order] },
122-
},
123-
});
124-
}
125-
126-
// Decrease the order from M to K: if there is an item with order K,
127-
// orders from K to M-1 should be made K+1 to M
128-
return models.ProjectPhase.update({ order: Sequelize.literal('"order" + 1') }, {
129-
where: {
130-
projectId,
131-
id: { $ne: updated.id },
132-
order: {
133-
$between: [
134-
updated.order,
135-
(previousValue.order ? previousValue.order : Number.MAX_SAFE_INTEGER) - 1,
136-
],
137-
},
138-
},
139-
});
140-
});
14190
}),
14291
)
143-
.then((updatedCount) => {
144-
if (updatedCount) {
145-
return models.ProjectPhase.findAll({
146-
where: {
147-
projectId,
148-
id: { $ne: updated.id },
149-
order: {
150-
$between: !_.isNil(previousValue.order) && previousValue.order < updated.order
151-
? [previousValue.order + 1, updated.order]
152-
: [
153-
updated.order,
154-
(previousValue.order ? previousValue.order : Number.MAX_SAFE_INTEGER) - 1,
155-
],
156-
},
157-
},
158-
order: [['updatedAt', 'DESC']],
159-
limit: updatedCount[0],
160-
});
161-
}
162-
return Promise.resolve([]);
163-
})
164-
.then((otherUpdated) => {
92+
.then(() => {
16593
req.log.debug('updated project phase', JSON.stringify(updated, null, 2));
16694

16795
const updatedValue = updated.get({ plain: true });
@@ -175,20 +103,6 @@ module.exports = [
175103
previousValue,
176104
ROUTES.PHASES.UPDATE);
177105

178-
// send updated event for all other phases which have been cascading updated
179-
_.map(otherUpdated, phase =>
180-
util.sendResourceToKafkaBus(
181-
req,
182-
EVENT.ROUTING_KEY.PROJECT_PHASE_UPDATED,
183-
RESOURCES.PHASE,
184-
_.assign(_.pick(phase.toJSON(), 'id', 'order', 'updatedBy', 'updatedAt')),
185-
// Pass the same object as original phase even though, the order has changed.
186-
// So far we don't use the order so it's ok. But in general, we should pass
187-
// the original phases. <- TODO
188-
_.assign(_.pick(phase.toJSON(), 'id', 'order', 'updatedBy', 'updatedAt'))),
189-
true, // don't send event to Notification Service as the main event here is updating one phase
190-
);
191-
192106
res.json(updated);
193107
})
194108
.catch(err => next(err));

src/routes/phases/update.spec.js

-29
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ describe('Project Phases', () => {
6060
let projectId;
6161
let projectName;
6262
let phaseId;
63-
let phaseId2;
6463
let phaseId3;
6564
const memberUser = {
6665
handle: testUtil.getDecodedToken(testUtil.jwts.member).handle,
@@ -122,7 +121,6 @@ describe('Project Phases', () => {
122121
models.ProjectPhase.bulkCreate(phases, { returning: true })
123122
.then((createdPhases) => {
124123
phaseId = createdPhases[0].id;
125-
phaseId2 = createdPhases[1].id;
126124
phaseId3 = createdPhases[2].id;
127125

128126
done();
@@ -252,33 +250,6 @@ describe('Project Phases', () => {
252250
});
253251
});
254252

255-
it('should return updated phase if the order is specified', (done) => {
256-
request(server)
257-
.patch(`/v5/projects/${projectId}/phases/${phaseId}`)
258-
.set({
259-
Authorization: `Bearer ${testUtil.jwts.copilot}`,
260-
})
261-
.send(_.assign({ order: 1 }, updateBody))
262-
.expect('Content-Type', /json/)
263-
.expect(200)
264-
.end((err, res) => {
265-
if (err) {
266-
done(err);
267-
} else {
268-
const resJson = res.body;
269-
validatePhase(resJson, updateBody);
270-
resJson.order.should.be.eql(1);
271-
272-
// Check the order of the other phase
273-
models.ProjectPhase.findOne({ where: { id: phaseId2 } })
274-
.then((phase2) => {
275-
phase2.order.should.be.eql(2);
276-
done();
277-
});
278-
}
279-
});
280-
});
281-
282253
it('should return 200 if requested by admin', (done) => {
283254
request(server)
284255
.patch(`/v5/projects/${projectId}/phases/${phaseId}`)

0 commit comments

Comments
 (0)