|
| 1 | +import _ from 'lodash'; |
| 2 | +import Promise from 'bluebird'; |
| 3 | +import models from '../../models'; |
| 4 | +import { USER_ROLE } from '../../constants'; |
| 5 | +import util from '../../util'; |
| 6 | + |
| 7 | +/** |
| 8 | + * API to handle retrieving projects |
| 9 | + * |
| 10 | + * Permissions: |
| 11 | + * Only users that have access to the project can retrieve it. |
| 12 | + * |
| 13 | + */ |
| 14 | +const PROJECT_ATTRIBUTES = _.without(_.keys(models.Project.rawAttributes), |
| 15 | + 'utm', |
| 16 | + 'deletedAt', |
| 17 | +); |
| 18 | +const PROJECT_MEMBER_ATTRIBUTES = _.without( |
| 19 | + _.keys(models.ProjectMember.rawAttributes), |
| 20 | + 'deletedAt', |
| 21 | +); |
| 22 | +const PROJECT_ATTACHMENT_ATTRIBUTES = _.without( |
| 23 | + _.keys(models.ProjectAttachment.rawAttributes), |
| 24 | + 'deletedAt', |
| 25 | + |
| 26 | +); |
| 27 | +const retrieveProjects = (req, criteria, sort, ffields) => { |
| 28 | + // order by |
| 29 | + const order = sort ? [sort.split(' ')] : [['createdAt', 'asc']]; |
| 30 | + let fields = ffields ? ffields.split(',') : []; |
| 31 | + // parse the fields string to determine what fields are to be returned |
| 32 | + fields = util.parseFields(fields, { |
| 33 | + projects: PROJECT_ATTRIBUTES, |
| 34 | + project_members: PROJECT_MEMBER_ATTRIBUTES, |
| 35 | + }); |
| 36 | + // make sure project.id is part of fields |
| 37 | + if (_.indexOf(fields.projects, 'id') < 0) fields.projects.push('id'); |
| 38 | + const retrieveAttachments = !req.query.fields || req.query.fields.indexOf('attachments') > -1; |
| 39 | + const retrieveMembers = !req.query.fields || !!fields.project_members.length; |
| 40 | + |
| 41 | + return models.Project.searchText({ |
| 42 | + filters: criteria.filters, |
| 43 | + order, |
| 44 | + limit: criteria.limit, |
| 45 | + offset: criteria.offset, |
| 46 | + attributes: _.get(fields, 'projects', null), |
| 47 | + }, req.log) |
| 48 | + .then(({ rows, count }) => { |
| 49 | + const projectIds = _.map(rows, 'id'); |
| 50 | + const promises = []; |
| 51 | + // retrieve members |
| 52 | + if (projectIds.length && retrieveMembers) { |
| 53 | + promises.push( |
| 54 | + models.ProjectMember.findAll({ |
| 55 | + attributes: _.get(fields, 'ProjectMembers'), |
| 56 | + where: { projectId: { in: projectIds } }, |
| 57 | + raw: true, |
| 58 | + }), |
| 59 | + ); |
| 60 | + } |
| 61 | + if (projectIds.length && retrieveAttachments) { |
| 62 | + promises.push( |
| 63 | + models.ProjectAttachment.findAll({ |
| 64 | + attributes: PROJECT_ATTACHMENT_ATTRIBUTES, |
| 65 | + where: { projectId: { in: projectIds } }, |
| 66 | + raw: true, |
| 67 | + }), |
| 68 | + ); |
| 69 | + } |
| 70 | + // return results after promise(s) have resolved |
| 71 | + return Promise.all(promises) |
| 72 | + .then((values) => { |
| 73 | + const allMembers = retrieveMembers ? values.shift() : []; |
| 74 | + const allAttachments = retrieveAttachments ? values.shift() : []; |
| 75 | + _.forEach(rows, (fp) => { |
| 76 | + const p = fp; |
| 77 | + // if values length is 1 it could be either attachments or members |
| 78 | + if (retrieveMembers) { |
| 79 | + p.members = _.filter(allMembers, m => m.projectId === p.id); |
| 80 | + } |
| 81 | + if (retrieveAttachments) { |
| 82 | + p.attachments = _.filter(allAttachments, a => a.projectId === p.id); |
| 83 | + } |
| 84 | + }); |
| 85 | + return { rows, count }; |
| 86 | + }); |
| 87 | + }); |
| 88 | +}; |
| 89 | + |
| 90 | +module.exports = [ |
| 91 | + /** |
| 92 | + * GET projects/ |
| 93 | + * Return a list of projects that match the criteria |
| 94 | + */ |
| 95 | + (req, res, next) => { |
| 96 | + // handle filters |
| 97 | + let filters = util.parseQueryFilter(req.query.filter); |
| 98 | + let sort = req.query.sort ? decodeURIComponent(req.query.sort) : 'createdAt'; |
| 99 | + if (sort && sort.indexOf(' ') === -1) { |
| 100 | + sort += ' asc'; |
| 101 | + } |
| 102 | + const sortableProps = [ |
| 103 | + 'createdAt', 'createdAt asc', 'createdAt desc', |
| 104 | + 'updatedAt', 'updatedAt asc', 'updatedAt desc', |
| 105 | + 'id', 'id asc', 'id desc', |
| 106 | + 'status', 'status asc', 'status desc', |
| 107 | + 'name', 'name asc', 'name desc', |
| 108 | + 'type', 'type asc', 'type desc', |
| 109 | + ]; |
| 110 | + if (!util.isValidFilter(filters, ['id', 'status', 'type', 'memberOnly', 'keyword']) || |
| 111 | + (sort && _.indexOf(sortableProps, sort) < 0)) { |
| 112 | + return util.handleError('Invalid filters or sort', null, req, next); |
| 113 | + } |
| 114 | + // check if user only wants to retrieve projects where he/she is a member |
| 115 | + const memberOnly = _.get(filters, 'memberOnly', false); |
| 116 | + filters = _.omit(filters, 'memberOnly'); |
| 117 | + |
| 118 | + const criteria = { |
| 119 | + filters, |
| 120 | + limit: Math.min(req.query.limit || 20, 20), |
| 121 | + offset: req.query.offset || 0, |
| 122 | + }; |
| 123 | + req.log.debug(criteria); |
| 124 | + |
| 125 | + if (!memberOnly |
| 126 | + && (util.hasRole(req, USER_ROLE.TOPCODER_ADMIN) |
| 127 | + || util.hasRole(req, USER_ROLE.MANAGER))) { |
| 128 | + // admins & topcoder managers can see all projects |
| 129 | + return retrieveProjects(req, criteria, sort, req.query.fields) |
| 130 | + .then(result => res.json(util.wrapResponse(req.id, result.rows, result.count))) |
| 131 | + .catch(err => next(err)); |
| 132 | + } |
| 133 | + // If user requested projects where he/she is a member or |
| 134 | + // if they are not a copilot then return projects that they are members in. |
| 135 | + // Copilots can view projects that they are members in or they have |
| 136 | + // |
| 137 | + const getProjectIds = !memberOnly && util.hasRole(req, USER_ROLE.COPILOT) ? |
| 138 | + models.Project.getProjectIdsForCopilot(req.authUser.userId) : |
| 139 | + models.ProjectMember.getProjectIdsForUser(req.authUser.userId); |
| 140 | + return getProjectIds |
| 141 | + .then((accessibleProjectIds) => { |
| 142 | + // filter based on accessible |
| 143 | + if (_.get(criteria.filters, 'id', null)) { |
| 144 | + criteria.filters.id.$in = _.intersection( |
| 145 | + accessibleProjectIds, |
| 146 | + criteria.filters.id.$in, |
| 147 | + ); |
| 148 | + } else { |
| 149 | + criteria.filters.id = { $in: accessibleProjectIds }; |
| 150 | + } |
| 151 | + return retrieveProjects(req, criteria, sort, req.query.fields); |
| 152 | + }) |
| 153 | + .then(result => res.json(util.wrapResponse(req.id, result.rows, result.count))) |
| 154 | + .catch(err => next(err)); |
| 155 | + }, |
| 156 | +]; |
0 commit comments