Skip to content

fix(PM-1168): invite user when assigning as copilot #809

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 17 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
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-1168']
only: ['develop', 'migration-setup', 'pm-1168_1']
- deployProd:
context : org-global
filters:
Expand Down
61 changes: 60 additions & 1 deletion src/routes/copilotOpportunity/assign.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import _ from 'lodash';
import validate from 'express-validation';
import Joi from 'joi';
import config from 'config';

import models from '../../models';
import util from '../../util';
import { PERMISSION } from '../../permissions/constants';
import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS } from '../../constants';
import { createEvent } from '../../services/busApi';
import { CONNECT_NOTIFICATION_EVENT, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, PROJECT_MEMBER_ROLE, RESOURCES } from '../../constants';

const assignCopilotOpportunityValidations = {
body: Joi.object().keys({
Expand Down Expand Up @@ -73,6 +75,63 @@ module.exports = [
throw err;
}

const project = await models.Project.findOne({
where: {
id: projectId,
},
});

const applicationUser = await util.getMemberDetailsByUserIds([userId], req.log, req.id)[0];

Choose a reason for hiding this comment

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

It seems like util.getMemberDetailsByUserIds([userId], req.log, req.id) returns a promise, but the code attempts to access the first element directly without awaiting the promise. Consider using await to ensure applicationUser is correctly assigned.


req.log.info("before create", applicationUser)

const invite = await models.ProjectMemberInvite.create({
status: INVITE_STATUS.PENDING,
role: PROJECT_MEMBER_ROLE.COPILOT,
userId,
email: applicationUser.email,
})

req.log.info("aftr create", invite)

Choose a reason for hiding this comment

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

Consider correcting the typo in the log message from "aftr create" to "after create" for better readability and consistency.

Choose a reason for hiding this comment

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

Typo in log message: 'aftr' should be corrected to 'after'.


util.sendResourceToKafkaBus(
req,
EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_CREATED,
RESOURCES.PROJECT_MEMBER_INVITE,
invite.toJSON());

const initiator = await util.getMemberDetailsByUserIds([req.authUser.userId], req.log, req.id);

Choose a reason for hiding this comment

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

Check if util.getMemberDetailsByUserIds can return an empty result or error, and handle such cases to avoid potential runtime errors.


const emailEventType = CONNECT_NOTIFICATION_EVENT.PROJECT_MEMBER_EMAIL_INVITE_CREATED;
await createEvent(emailEventType, {
data: {
workManagerUrl: config.get('workManagerUrl'),
accountsAppURL: config.get('accountsAppUrl'),
subject: config.get('inviteEmailSubject'),
projects: [{
name: project.name,
projectId,
sections: [
{
EMAIL_INVITES: true,
title: config.get('inviteEmailSectionTitle'),
projectName: project.name,
projectId,
initiator,
isSSO: util.isSSO(project),
},
],
}],
},
recipients: [applicationUser.email],

Choose a reason for hiding this comment

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

It seems like the recipient email has been changed from existingUser.email to applicationUser.email. Ensure that applicationUser is correctly defined and initialized before this line, and that it contains the expected email address for the invitation process.

version: 'v3',
from: {
name: config.get('EMAIL_INVITE_FROM_NAME'),
email: config.get('EMAIL_INVITE_FROM_EMAIL'),
},
categories: [`${process.env.NODE_ENV}:${emailEventType}`.toLowerCase()],
}, req.log);

await models.CopilotRequest.update(
{ status: COPILOT_REQUEST_STATUS.FULFILLED },
{ where: { id: opportunity.copilotRequestId }, transaction: t },
Expand Down