Skip to content

Commit 8e30d1b

Browse files
authored
Merge pull request #824 from topcoder-platform/pm-1273
fix(PM-1273): Send canApplyAsCopilot to check if the user can apply for the opportunity
2 parents 71f598c + 6557460 commit 8e30d1b

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ workflows:
149149
context : org-global
150150
filters:
151151
branches:
152-
only: ['develop', 'migration-setup', 'pm-1356']
152+
only: ['develop', 'migration-setup', 'pm-1273']
153153
- deployProd:
154154
context : org-global
155155
filters:

src/models/projectMember.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ module.exports = function defineProjectMember(sequelize, DataTypes) {
3434
],
3535
});
3636

37+
ProjectMember.associate = (models) => {
38+
ProjectMember.belongsTo(models.Project, { foreignKey: 'projectId' });
39+
};
40+
3741
ProjectMember.getProjectIdsForUser = userId => ProjectMember.findAll({
3842
where: {
3943
deletedAt: { $eq: null },

src/routes/copilotOpportunity/get.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,30 @@ module.exports = [
2020
model: models.Project,
2121
as: 'project',
2222
attributes: ['name'],
23+
include: [
24+
{
25+
model: models.ProjectMember,
26+
as: 'members',
27+
attributes: ['id', 'userId', 'role'],
28+
},
29+
]
2330
},
2431
],
2532
})
2633
.then((copilotOpportunity) => {
2734
const plainOpportunity = copilotOpportunity.get({ plain: true });
28-
const formattedOpportunity = Object.assign({}, plainOpportunity,
35+
req.log.info("authUser", req.authUser);
36+
const memberIds = plainOpportunity.project.members && plainOpportunity.project.members.map((member) => member.userId);
37+
let canApplyAsCopilot = false;
38+
if (req.authUser) {
39+
canApplyAsCopilot = !memberIds.includes(req.authUser.userId)
40+
}
41+
// This shouldn't be exposed to the clientside
42+
delete plainOpportunity.project.members;
43+
const formattedOpportunity = Object.assign({
44+
members: memberIds,
45+
canApplyAsCopilot,
46+
}, plainOpportunity,
2947
plainOpportunity.copilotRequest ? plainOpportunity.copilotRequest.data : {},
3048
{ copilotRequest: undefined },
3149
);

src/routes/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ const jwtAuth = require('tc-core-library-js').middleware.jwtAuthenticator;
3535
router.all(
3636
RegExp(`\\/${apiVersion}\\/(copilots|projects|timelines|orgConfig|customer-payments)(?!\\/health).*`),
3737
(req, res, next) => {
38-
if (publicRoutes.some(routeRegex => routeRegex.test(req.path))) {
38+
let token
39+
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
40+
token = req.headers.authorization.split(' ')[1]
41+
}
42+
if (publicRoutes.some(routeRegex => routeRegex.test(req.path)) && !token) {
3943
return next();
4044
}
45+
req.log.info("token available", token);
4146
// JWT authentication
4247
return jwtAuth(config)(req, res, next);
4348
},

0 commit comments

Comments
 (0)