Skip to content

feat: allow existing project member to be invited as copilot #835

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 29 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
25b8b34
fix: removed logic to allow inviting user even if they are already a …
hentrymartin Jul 23, 2025
7831f54
fix: allow copilots to be added even if the existing member
hentrymartin Jul 23, 2025
57a8d00
fix: allow copilots to be added even if the existing member
hentrymartin Jul 23, 2025
1b21d3b
fix: allow copilots to be added even if the existing member
hentrymartin Jul 23, 2025
fa13330
fix: allow copilots to be added even if the existing member
hentrymartin Jul 23, 2025
0536910
fix: allow copilots to be added even if the existing member
hentrymartin Jul 23, 2025
b67a6ba
fix: allow copilots to be added even if the existing member
hentrymartin Jul 23, 2025
f26de03
fix: just switch role if user is already a member
hentrymartin Jul 24, 2025
50d2dac
fix: just switch role if user is already a member
hentrymartin Jul 24, 2025
347caae
fix: debug logs
hentrymartin Jul 24, 2025
d4e4033
fix: debug logs
hentrymartin Jul 24, 2025
ecb836c
fix: debug logs
hentrymartin Jul 24, 2025
279d2f1
fix: update kafka
hentrymartin Jul 24, 2025
3998114
fix: update kafka
hentrymartin Jul 24, 2025
b7c5f95
revert
hentrymartin Jul 24, 2025
bebe265
fix: build
hentrymartin Jul 24, 2025
9385156
fix: added error string and already assigned role
hentrymartin Jul 25, 2025
fdb09e3
fix: added error string and already assigned role
hentrymartin Jul 25, 2025
2dc0ea2
fix: added error string and already assigned role
hentrymartin Jul 25, 2025
13c8c39
fix: added error string and already assigned role
hentrymartin Jul 25, 2025
f73f444
feat: modifications on copilot addition to project
hentrymartin Jul 27, 2025
7fce661
feat: modifications on copilot addition to project
hentrymartin Jul 27, 2025
1f2cba4
feat: modifications on copilot addition to project
hentrymartin Jul 27, 2025
1266652
feat: modifications on copilot addition to project
hentrymartin Jul 27, 2025
c701b1e
fix: complete the copilot requests if the incoming role is observer o…
hentrymartin Jul 27, 2025
1d19d15
fix: complete the copilot requests if the incoming role is observer o…
hentrymartin Jul 27, 2025
af842b0
fix: complete the copilot requests if the incoming role is observer o…
hentrymartin Jul 27, 2025
0d02782
fix: complete the copilot requests if the incoming role is observer o…
hentrymartin Jul 28, 2025
ce575c7
fix: action string
hentrymartin Jul 28, 2025
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ workflows:
context : org-global
filters:
branches:
only: ['develop', 'migration-setup', 'pm-1497']
only: ['develop', 'migration-setup', 'pm-1506']
- deployProd:
context : org-global
filters:
Expand Down
31 changes: 29 additions & 2 deletions src/routes/projectMemberInvites/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _ from 'lodash';
import Joi from 'joi';
import config from 'config';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import { Op } from 'sequelize';
import models from '../../models';
import util from '../../util';
import {
Expand Down Expand Up @@ -299,7 +300,7 @@ module.exports = [

return [];
})
.then((inviteUsers) => {
.then(async (inviteUsers) => {
const members = req.context.currentProjectMembers;
const projectId = _.parseInt(req.params.projectId);
// check user handle exists in returned result
Expand All @@ -322,13 +323,39 @@ module.exports = [
const errorMessageForAlreadyMemberUser = 'User with such handle is already a member of the team.';

if (inviteUserIds) {
// remove members already in the team
const existingMembers = _.filter(members, (m) => {
return inviteUserIds.includes(m.userId);
});

req.log.debug(`Existing members: ${JSON.stringify(existingMembers)}`);

const projectMembers = await models.ProjectMember.findAll({
where: {
userId: {
[Op.in]: existingMembers.map(item => item.userId),
},
projectId,
}
});

req.log.debug(`Existing Project Members: ${JSON.stringify(projectMembers)}`);

const existingProjectMembersMap = projectMembers.reduce((acc, current) => {
return Object.assign({}, acc, {
[current.userId]: current,
});
}, {});

req.log.debug(`Existing Project Members Map: ${JSON.stringify(existingProjectMembersMap)}`);

_.remove(inviteUserIds, u => _.some(members, (m) => {
const isPresent = m.userId === u;
if (isPresent) {
failed.push(_.assign({}, {
handle: getUserHandleById(m.userId, inviteUsers),
message: errorMessageForAlreadyMemberUser,
error: "ALREADY_MEMBER",

Choose a reason for hiding this comment

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

Consider using a constant or an enum for the error code "ALREADY_MEMBER" to avoid magic strings and improve maintainability.

role: existingProjectMembersMap[m.userId].role,
}));
}
return isPresent;
Expand Down
92 changes: 87 additions & 5 deletions src/routes/projectMembers/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Joi from 'joi';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import models from '../../models';
import util from '../../util';
import { EVENT, RESOURCES, PROJECT_MEMBER_ROLE } from '../../constants';
import { EVENT, RESOURCES, PROJECT_MEMBER_ROLE, COPILOT_REQUEST_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_APPLICATION_STATUS } from '../../constants';
import { PERMISSION, PROJECT_TO_TOPCODER_ROLES_MATRIX } from '../../permissions/constants';
import { Op } from 'sequelize';

/**
* API to update a project member.
Expand All @@ -27,12 +28,85 @@ const updateProjectMemberValdiations = {
PROJECT_MEMBER_ROLE.SOLUTION_ARCHITECT,
PROJECT_MEMBER_ROLE.PROJECT_MANAGER,
).required(),
action: Joi.string().optional(),
}),
query: {
fields: Joi.string().optional(),
},
};

const completeAllCopilotRequests = async (req, projectId, _transaction) => {
const allCopilotRequests = await models.CopilotRequest.findAll({
where: {
projectId,
},
transaction: _transaction,
});

req.log.debug(`all copilot requests ${JSON.stringify(allCopilotRequests)}`);

await models.CopilotRequest.update({
status: COPILOT_REQUEST_STATUS.FULFILLED,
}, {
where: {
id: {
[Op.in]: allCopilotRequests.map(item => item.id),
}
},
transaction: _transaction,
});

req.log.debug(`updated all copilot requests`);

const copilotOpportunites = await models.CopilotOpportunity.findAll({
where: {
copilotRequestId: {
[Op.in]: allCopilotRequests.map(item => item.id),
},
},
transaction: _transaction,
});

req.log.debug(`all copilot opportunities ${JSON.stringify(copilotOpportunites)}`);

await models.CopilotOpportunity.update({
status: COPILOT_OPPORTUNITY_STATUS.COMPLETED,
}, {
where: {
id: {
[Op.in]: copilotOpportunites.map(item => item.id),
}
},
transaction: _transaction,
});

req.log.debug(`updated all copilot opportunities`);

const allCopilotApplications = await models.CopilotApplication.findAll({
where: {
opportunityId: {
[Op.in]: copilotOpportunites.map(item => item.id),
},
},
transaction: _transaction,
});

req.log.debug(`all copilot applications ${JSON.stringify(allCopilotApplications)}`);

await models.CopilotApplication.update({
status: COPILOT_APPLICATION_STATUS.CANCELED,
}, {
where: {
id: {
[Op.in]: allCopilotApplications.map(item => item.id),
},
},
transaction: _transaction,
});

req.log.debug(`updated all copilot applications`);
};

module.exports = [
// handles request validations
validate(updateProjectMemberValdiations),
Expand All @@ -45,15 +119,16 @@ module.exports = [
let updatedProps = req.body;
const projectId = _.parseInt(req.params.projectId);
const memberRecordId = _.parseInt(req.params.id);
const action = updatedProps.action;
updatedProps = _.pick(updatedProps, ['isPrimary', 'role']);
const fields = req.query.fields ? req.query.fields.split(',') : null;

let previousValue;
// let newValue;
models.sequelize.transaction(() => models.ProjectMember.findOne({
models.sequelize.transaction((_transaction) => models.ProjectMember.findOne({
where: { id: memberRecordId, projectId },
})
.then((_member) => {
.then(async (_member) => {
if (!_member) {
// handle 404
const err = new Error(`project member not found for project id ${projectId} ` +
Expand All @@ -76,10 +151,13 @@ module.exports = [
return Promise.reject(err);
}

req.log.debug(`updated props ${JSON.stringify(updatedProps)}`);
req.log.debug(`previous values ${JSON.stringify(previousValue)}`);
// no updates if no change
if (updatedProps.role === previousValue.role &&
if ((updatedProps.role === previousValue.role || action === 'complete-copilot-requests') &&
(_.isUndefined(updatedProps.isPrimary) ||
updatedProps.isPrimary === previousValue.isPrimary)) {
await completeAllCopilotRequests(req, projectId, _transaction);
return Promise.resolve();
}

Expand Down Expand Up @@ -121,9 +199,13 @@ module.exports = [
});
})
.then(() => projectMember.reload(projectMember.id))
.then(() => {
.then(async () => {
projectMember = projectMember.get({ plain: true });
projectMember = _.omit(projectMember, ['deletedAt']);

if (['observer', 'customer'].includes(updatedProps.role)) {
await completeAllCopilotRequests(req, projectId, _transaction);
}
})
.then(() => (
util.getObjectsWithMemberDetails([projectMember], fields, req)
Expand Down