Skip to content

[HOTFIX][PROD] bulk update member phases for milestone management #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 29, 2021
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
42 changes: 37 additions & 5 deletions docs/Project API.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "52f34e21-5b0b-4eb0-99fa-cbd1ac7f215a",
"_postman_id": "6418ac6e-a797-4e30-b4d3-a1dd0cdead22",
"name": "Project API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
Expand Down Expand Up @@ -4834,7 +4834,7 @@
"exec": [
"pm.test(\"Status code is 201\", function () {",
" pm.response.to.have.status(201);",
" pm.environment.set(\"phaseId\", pm.response.json().id);",
" pm.environment.set(\"phaseId-2\", pm.response.json().id);",
"});"
],
"type": "text/javascript"
Expand Down Expand Up @@ -4880,7 +4880,7 @@
"exec": [
"pm.test(\"Status code is 201\", function () {",
" pm.response.to.have.status(201);",
" pm.environment.set(\"phaseId\", pm.response.json().id);",
" pm.environment.set(\"phaseId-3\", pm.response.json().id);",
"});"
],
"type": "text/javascript"
Expand All @@ -4901,7 +4901,7 @@
],
"body": {
"mode": "raw",
"raw": "{\n\t\"name\": \"test project phase\",\n\t\"status\": \"active\",\n\t\"startDate\": \"2018-05-15T00:00:00\",\n\t\"endDate\": \"2018-05-16T00:00:00\",\n\t\"budget\": 20,\n\t\"details\": {\n\t\t\"aDetails\": \"a details\"\n\t},\n\t\"order\": 1,\n\t\"productTemplateId\": {{productTemplateId}}\n}"
"raw": "{\n\t\"name\": \"test project phase\",\n\t\"status\": \"active\",\n\t\"startDate\": \"2018-05-15T00:00:00\",\n\t\"endDate\": \"2018-05-16T00:00:00\",\n\t\"budget\": 20,\n\t\"details\": {\n\t\t\"aDetails\": \"a details\"\n\t},\n\t\"order\": 1,\n\t\"productTemplateId\": 2\n}"
},
"url": {
"raw": "{{api-url}}/projects/{{projectId}}/phases",
Expand All @@ -4926,7 +4926,7 @@
"exec": [
"pm.test(\"Status code is 201\", function () {",
" pm.response.to.have.status(201);",
" pm.environment.set(\"phaseId\", pm.response.json().id);",
" pm.environment.set(\"phaseId-4\", pm.response.json().id);",
"});"
],
"type": "text/javascript"
Expand Down Expand Up @@ -5355,6 +5355,38 @@
}
},
"response": []
},
{
"name": "Bulk Delete Phase",
"request": {
"method": "DELETE",
"header": [
{
"key": "Authorization",
"value": "Bearer {{jwt-token}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"phaseIds\": [\r\n {{phaseId-2}},\r\n {{phaseId-3}},\r\n {{phaseId-4}}\r\n ]\r\n}"
},
"url": {
"raw": "{{api-url}}/projects/{{projectId}}/phases",
"host": [
"{{api-url}}"
],
"path": [
"projects",
"{{projectId}}",
"phases"
]
}
},
"response": []
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ router.route('/v5/projects/metadata/productTemplates/:templateId(\\d+)')

router.route('/v5/projects/:projectId(\\d+)/phases')
.get(require('./phases/list'))
.post(require('./phases/create'));
.post(require('./phases/create'))
.delete(require('./phases/bulkDelete'));

router.route('/v5/projects/:projectId(\\d+)/phases/:phaseId(\\d+)')
.get(require('./phases/get'))
Expand Down
2 changes: 1 addition & 1 deletion src/routes/phaseMembers/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = [
req.log.debug('updated phase members', JSON.stringify(newPhaseMembers, null, 2));
const updatedPhase = _.cloneDeep(phase);
// emit event
if (_.intersectionBy(phaseMembers, updatedPhaseMembers, 'id').length !== updatedPhaseMembers.length) {
if (!_.isEqual(_.sortBy(phaseMembers, 'id'), _.sortBy(updatedPhaseMembers, 'id'))) {
util.sendResourceToKafkaBus(
req,
EVENT.ROUTING_KEY.PROJECT_PHASE_UPDATED,
Expand Down
79 changes: 79 additions & 0 deletions src/routes/phases/bulkDelete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import validate from 'express-validation';
import _ from 'lodash';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import Joi from 'joi';
import models from '../../models';
import util from '../../util';
import { EVENT, RESOURCES } from '../../constants';

const permissions = tcMiddleware.permissions;

const bulkDeletePhaseValidation = {
body: Joi.object().keys({
phaseIds: Joi.array().items(Joi.number().integer()).required(),
}).required(),
};

module.exports = [
// validate request payload
validate(bulkDeletePhaseValidation),
// check permission
permissions('project.deleteProjectPhase'),

(req, res, next) => {
const data = req.body;
const projectId = _.parseInt(req.params.projectId);

models.sequelize.transaction(transaction =>
// soft delete the record
models.ProjectPhase.findAll({
where: {
id: data.phaseIds,
projectId,
deletedAt: { $eq: null },
},
raw: true,
transaction,
}).then((phases) => {
const notFoundPhases = _.differenceWith(data.phaseIds, phases, (a, b) => a === b.id);
if (!_.isEmpty(notFoundPhases)) {
// handle 404
const err = new Error('no active project phase found for project id ' +
`${projectId} and phase ids ${notFoundPhases}`);
err.status = 404;
return Promise.reject(err);
}
return models.ProjectPhase.update({ deletedBy: req.authUser.userId }, {
where: {
id: data.phaseIds,
projectId,
},
transaction,
}).then(() =>
models.ProjectPhase.destroy({
where: {
id: data.phaseIds,
projectId,
},
transaction,
}),
);
}))
.then((deletedCount) => {
const result = {
id: data.phaseIds,
projectId,
};
req.log.debug('deleted project phases', JSON.stringify(result, null, 2));
if (deletedCount > 0) {
util.sendResourceToKafkaBus(
req,
EVENT.ROUTING_KEY.PROJECT_PHASE_REMOVED,
RESOURCES.PHASE,
result,
);
}
res.status(204).json({});
}).catch(err => next(err));
},
];
Loading