Skip to content

Commit f38a2b6

Browse files
committed
PM-1499 - when updating copilot request, check for same type request
1 parent ff30126 commit f38a2b6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/routes/copilotRequest/update.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import Joi from 'joi';
44

55
import models from '../../models';
66
import util from '../../util';
7-
import { COPILOT_OPPORTUNITY_TYPE } from '../../constants';
7+
import { COPILOT_OPPORTUNITY_TYPE, COPILOT_REQUEST_STATUS } from '../../constants';
88
import { PERMISSION } from '../../permissions/constants';
9+
import { Op } from 'sequelize';
910

1011
const updateCopilotRequestValidations = {
1112
body: Joi.object().keys({
@@ -62,9 +63,33 @@ module.exports = [
6263
throw err;
6364
}
6465

66+
// check if same type of copilot request already exists
67+
if (patchData.projectType !== undefined && patchData.projectType !== copilotRequest.data.projectType) {
68+
const sameTypeRequest = await models.CopilotRequest.findOne({
69+
where: {
70+
projectId: copilotRequest.projectId,
71+
status: {
72+
[Op.in]: [COPILOT_REQUEST_STATUS.NEW, COPILOT_REQUEST_STATUS.APPROVED, COPILOT_REQUEST_STATUS.SEEKING],
73+
},
74+
data: {
75+
projectType: patchData.projectType,
76+
},
77+
id: { [Op.not]: copilotRequestId },
78+
},
79+
});
80+
81+
if (sameTypeRequest) {
82+
const err = new Error('There\'s a request of same type already!');
83+
_.assign(err, {
84+
status: 400,
85+
});
86+
throw err;
87+
}
88+
}
89+
6590
// Only update fields provided in patchData
66-
await copilotRequest.update(_.extend({
67-
data: patchData,
91+
await copilotRequest.update(_.assign({
92+
data: _.assign(copilotRequest.data, patchData),
6893
updatedBy: req.authUser.userId,
6994
}));
7095

0 commit comments

Comments
 (0)