Skip to content

Commit 6084631

Browse files
authored
Feature/project status copilot messages (#106)
Add project access error messages for copilot
1 parent 4f5c91d commit 6084631

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/permissions/project.view.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import _ from 'lodash';
33
import util from '../util';
44
import models from '../models';
5-
import { USER_ROLE } from '../constants';
5+
import { USER_ROLE, PROJECT_STATUS, PROJECT_MEMBER_ROLE } from '../constants';
66

77
/**
88
* Super admin, Topcoder Managers are allowed to view any projects
@@ -37,8 +37,37 @@ module.exports = freq => new Promise((resolve, reject) => {
3737
})
3838
.then((hasAccess) => {
3939
if (!hasAccess) {
40+
let errorMessage = 'You do not have permissions to perform this action';
41+
// customize error message for copilots
42+
if (util.hasRole(freq, USER_ROLE.COPILOT)) {
43+
if (_.findIndex(freq.context.currentProjectMembers, m => m.role === PROJECT_MEMBER_ROLE.COPILOT) >= 0) {
44+
errorMessage = 'Copilot: Project is already claimed by another copilot';
45+
return Promise.resolve(errorMessage);
46+
}
47+
return models.Project
48+
.find({
49+
where: { id: projectId },
50+
attributes: ['status'],
51+
raw: true,
52+
})
53+
.then((project) => {
54+
if (!project || [PROJECT_STATUS.DRAFT, PROJECT_STATUS.IN_REVIEW].indexOf(project.status) >= 0) {
55+
errorMessage = 'Copilot: Project is not yet available to copilots';
56+
} else {
57+
// project status is 'active' or higher so it's not available to copilots
58+
errorMessage = 'Copilot: Project has already started';
59+
}
60+
return Promise.resolve(errorMessage);
61+
});
62+
}
63+
// user is not an admin nor is a registered project member
64+
return Promise.resolve(errorMessage);
65+
}
66+
return Promise.resolve(null);
67+
}).then((errorMessage) => {
68+
if (errorMessage) {
4069
// user is not an admin nor is a registered project member
41-
return reject(new Error('You do not have permissions to perform this action'));
70+
return reject(new Error(errorMessage));
4271
}
4372
return resolve(true);
4473
});

0 commit comments

Comments
 (0)