Skip to content

Commit f8f52e0

Browse files
committed
fix: project object optional
1 parent 6fb8f34 commit f8f52e0

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/routes/copilotOpportunity/get.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { USER_ROLE } from '../../constants';
12
import models from '../../models';
23
import util from '../../util';
34

@@ -8,9 +9,11 @@ module.exports = [
89
return util.handleError('Invalid opportunity ID', null, req, next, 400);
910
}
1011

12+
const isAdminOrManager = util.hasRoles(req, [USER_ROLE.CONNECT_ADMIN, USER_ROLE.TOPCODER_ADMIN, USER_ROLE.PROJECT_MANAGER]);
13+
1114
return models.CopilotOpportunity.findOne({
1215
where: { id },
13-
include: [
16+
include: isAdminOrManager ? [
1417
{
1518
model: models.CopilotRequest,
1619
as: 'copilotRequest',
@@ -27,24 +30,36 @@ module.exports = [
2730
},
2831
]
2932
},
33+
]: [
34+
{
35+
model: models.CopilotRequest,
36+
as: 'copilotRequest',
37+
},
3038
],
3139
})
3240
.then((copilotOpportunity) => {
3341
const plainOpportunity = copilotOpportunity.get({ plain: true });
34-
const memberIds = plainOpportunity.project.members && plainOpportunity.project.members.map((member) => member.userId);
42+
const memberIds = (plainOpportunity.project && plainOpportunity.project.members && plainOpportunity.project.members.map((member) => member.userId)) || [];
3543
let canApplyAsCopilot = false;
3644
if (req.authUser) {
3745
canApplyAsCopilot = !memberIds.includes(req.authUser.userId)
3846
}
39-
// This shouldn't be exposed to the clientside
40-
delete plainOpportunity.project.members;
47+
48+
if (plainOpportunity.project) {
49+
// This shouldn't be exposed to the clientside
50+
delete plainOpportunity.project.members;
51+
}
4152
const formattedOpportunity = Object.assign({
4253
members: memberIds,
4354
canApplyAsCopilot,
4455
}, plainOpportunity,
4556
plainOpportunity.copilotRequest ? plainOpportunity.copilotRequest.data : {},
4657
{ copilotRequest: undefined },
4758
);
59+
60+
if (!isAdminOrManager) {
61+
delete formattedOpportunity.projectId;
62+
}
4863
res.json(formattedOpportunity);
4964
})
5065
.catch((err) => {

src/routes/copilotOpportunity/list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ module.exports = [
7373
plainOpportunity.copilotRequest ? plainOpportunity.copilotRequest.data : {},
7474
{ copilotRequest: undefined },
7575
);
76-
76+
7777
// For users who are not admin or manager, we dont want to expose
7878
// the project id
7979
if (!isAdminOrManager) {
80-
delete plainOpportunity.projectId;
80+
delete formatted.projectId;
8181
}
8282
return formatted;
8383
});

0 commit comments

Comments
 (0)