|
2 | 2 | import _ from 'lodash';
|
3 | 3 | import util from '../util';
|
4 | 4 | import models from '../models';
|
5 |
| -import { USER_ROLE } from '../constants'; |
| 5 | +import { USER_ROLE, PROJECT_STATUS, PROJECT_MEMBER_ROLE } from '../constants'; |
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * Super admin, Topcoder Managers are allowed to view any projects
|
@@ -37,8 +37,37 @@ module.exports = freq => new Promise((resolve, reject) => {
|
37 | 37 | })
|
38 | 38 | .then((hasAccess) => {
|
39 | 39 | 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) { |
40 | 69 | // 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)); |
42 | 71 | }
|
43 | 72 | return resolve(true);
|
44 | 73 | });
|
|
0 commit comments