Skip to content

PM-1499 edit copilot request #831

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 2 commits into from
Jul 23, 2025
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
1 change: 1 addition & 0 deletions src/routes/copilotRequest/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const addCopilotRequestValidations = {
data: Joi.object()
.keys({
projectId: Joi.number().required(),
opportunityTitle: Joi.string().required(),
copilotUsername: Joi.string(),
complexity: Joi.string().valid('low', 'medium', 'high').required(),
requiresCommunication: Joi.string().valid('yes', 'no').required(),
Expand Down
79 changes: 79 additions & 0 deletions src/routes/copilotRequest/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import validate from 'express-validation';
import _ from 'lodash';
import Joi from 'joi';

import models from '../../models';
import util from '../../util';
import { COPILOT_OPPORTUNITY_TYPE } from '../../constants';
import { PERMISSION } from '../../permissions/constants';

const updateCopilotRequestValidations = {
body: Joi.object().keys({
data: Joi.object()
.keys({
projectId: Joi.number().required(),
copilotUsername: Joi.string(),
complexity: Joi.string().valid('low', 'medium', 'high'),
requiresCommunication: Joi.string().valid('yes', 'no'),
paymentType: Joi.string().valid('standard', 'other'),
otherPaymentType: Joi.string(),
opportunityTitle: Joi.string(),
projectType: Joi.string().valid(_.values(COPILOT_OPPORTUNITY_TYPE)),
overview: Joi.string().min(10),
skills: Joi.array().items(
Joi.object({
id: Joi.string().required(),
name: Joi.string().required(),
}),
),
startDate: Joi.date().iso(),
numWeeks: Joi.number().integer().positive(),
tzRestrictions: Joi.string(),
numHoursPerWeek: Joi.number().integer().positive(),
})
.required(),
}),
};

module.exports = [
validate(updateCopilotRequestValidations),
async (req, res, next) => {
const copilotRequestId = _.parseInt(req.params.copilotRequestId);
const patchData = req.body.data;

if (!util.hasPermissionByReq(PERMISSION.MANAGE_COPILOT_REQUEST, req)) {
const err = new Error('Unable to update copilot request');
_.assign(err, {
details: JSON.stringify({ message: 'You do not have permission to update copilot request' }),
status: 403,
});
util.handleError('Permission error', err, req, next);
return;
}

try {
const copilotRequest = await models.CopilotRequest.findOne({
where: { id: copilotRequestId },
});

if (!copilotRequest) {
const err = new Error(`Copilot request not found for id ${copilotRequestId}`);
err.status = 404;
throw err;
}

// Only update fields provided in patchData
await copilotRequest.update(_.extend({
data: patchData,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _.extend method is used here, which might not work as intended. Consider using Object.assign or _.assign to merge patchData into the copilotRequest object. _.extend does not deeply merge objects, which might lead to unexpected behavior.

updatedBy: req.authUser.userId,
}));

res.status(200).json(copilotRequest);
} catch (err) {
if (err.message) {
_.assign(err, { details: err.message });
}
util.handleError('Error updating copilot request', err, req, next);
}
},
];
3 changes: 2 additions & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ router.route('/v5/projects/:projectId(\\d+)/settings')
router.route('/v5/projects/copilots/requests')
.get(require('./copilotRequest/list'));
router.route('/v5/projects/copilots/requests/:copilotRequestId(\\d+)')
.get(require('./copilotRequest/get'));
.get(require('./copilotRequest/get'))
.patch(require('./copilotRequest/update'));
router.route('/v5/projects/:projectId(\\d+)/copilots/requests')
.get(require('./copilotRequest/list'))
.post(require('./copilotRequest/create'));
Expand Down