-
Notifications
You must be signed in to change notification settings - Fork 55
feat: project member & invites endpoint with additional fields #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maxceem
merged 3 commits into
topcoder-platform:dev
from
dpkshrma:feature/project-invites-traits
Dec 3, 2019
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import _ from 'lodash'; | ||
| import Joi from 'joi'; | ||
| import validate from 'express-validation'; | ||
| import { middleware as tcMiddleware } from 'tc-core-library-js'; | ||
| import models from '../../models'; | ||
| import util from '../../util'; | ||
|
|
||
| /** | ||
| * API to list all project member invites. | ||
| * | ||
| */ | ||
| const permissions = tcMiddleware.permissions; | ||
|
|
||
| const schema = { | ||
| query: { | ||
| fields: Joi.string().optional(), | ||
| }, | ||
| }; | ||
|
|
||
| module.exports = [ | ||
| validate(schema), | ||
| permissions('projectMemberInvite.list'), | ||
| async (req, res, next) => { | ||
| try { | ||
| let fields = null; | ||
| if (req.query.fields) { | ||
| fields = req.query.fields.split(','); | ||
| } | ||
| const memberFields = _.keys(models.ProjectMemberInvite.attributes); | ||
| const projectId = _.parseInt(req.params.projectId); | ||
| const invites = await models.ProjectMemberInvite.findAll({ | ||
| where: { projectId }, | ||
| raw: true, | ||
| }); | ||
| const opts = { | ||
| logger: req.log, | ||
| requestId: req.id, | ||
| memberFields, | ||
| }; | ||
| const invitesWithDetails = await util.getObjectsWithMemberDetails(invites, fields, opts); | ||
| return res.json(util.wrapResponse(req.id, invitesWithDetails)); | ||
| } catch (err) { | ||
| return next(err); | ||
| } | ||
| }, | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
|
|
||
|
|
||
| import _ from 'lodash'; | ||
| import Joi from 'joi'; | ||
| import validate from 'express-validation'; | ||
| import { middleware as tcMiddleware } from 'tc-core-library-js'; | ||
| import models from '../../models'; | ||
| import util from '../../util'; | ||
|
|
||
| /** | ||
| * API to get a project member in a project. | ||
| */ | ||
| const permissions = tcMiddleware.permissions; | ||
|
|
||
| const schema = { | ||
| query: { | ||
| fields: Joi.string().optional(), | ||
| }, | ||
| }; | ||
|
|
||
| module.exports = [ | ||
| validate(schema), | ||
| permissions('project.getMember'), | ||
| async (req, res, next) => { | ||
| try { | ||
| let fields = null; | ||
| if (req.query.fields) { | ||
| fields = req.query.fields.split(','); | ||
| } | ||
| const memberFields = _.keys(models.ProjectMember.attributes); | ||
| const memberId = _.parseInt(req.params.id); | ||
| const members = [_.find(req.context.currentProjectMembers, user => user.id === memberId)]; | ||
| const [member] = await util.getObjectsWithMemberDetails( | ||
| members, fields, { | ||
| logger: req.log, | ||
| requestId: req.id, | ||
| memberFields, | ||
| }, | ||
| ); | ||
| return res.json(util.wrapResponse(req.id, member)); | ||
| } catch (err) { | ||
| return next(err); | ||
| } | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,124 +1,42 @@ | ||
| /** | ||
| * Endpoint to list project members. | ||
| */ | ||
| import _ from 'lodash'; | ||
| import Joi from 'joi'; | ||
| import validate from 'express-validation'; | ||
| import { middleware as tcMiddleware } from 'tc-core-library-js'; | ||
| import models from '../../models'; | ||
| import util from '../../util'; | ||
|
|
||
| /** | ||
| * API to list all project members. | ||
| * | ||
| */ | ||
| const permissions = tcMiddleware.permissions; | ||
|
|
||
| const schema = { | ||
| query: { | ||
| fields: Joi.string().optional(), | ||
| }, | ||
| }; | ||
|
|
||
| module.exports = [ | ||
| validate(schema), | ||
| permissions('project.listMembers'), | ||
| async (req, res) => { | ||
| let members = req.context.currentProjectMembers; | ||
|
|
||
|
|
||
| if (members.length && _.get(req, 'query.fields')) { | ||
| const fields = req.query.fields.split(','); | ||
|
|
||
| const ModelFields = [ | ||
| 'id', | ||
| 'userId', | ||
| 'role', | ||
| 'isPrimary', | ||
| 'deletedAt', | ||
| 'createdAt', | ||
| 'updatedAt', | ||
| 'deletedBy', | ||
| 'createdBy', | ||
| 'updatedBy', | ||
| ]; | ||
|
|
||
| const modelFields = _.intersection(ModelFields, fields); | ||
| const hasUserIdField = _.indexOf(fields, 'userId') !== -1; | ||
| if (hasUserIdField === false) { | ||
| modelFields.push('userId'); | ||
| } | ||
| // merge model fields | ||
| members = _.map(members, m => _.pick(m, modelFields)); | ||
|
|
||
| const MemberDetailFields = ['handle', 'firstName', 'lastName']; | ||
| const memberDetailFields = _.intersection(MemberDetailFields, fields); | ||
|
|
||
| // has handleField | ||
| const hasHandleField = _.indexOf(memberDetailFields, 'handle') !== -1; | ||
|
|
||
| if (hasHandleField === false) { | ||
| memberDetailFields.push('handle'); | ||
| } | ||
|
|
||
| if (memberDetailFields.length) { | ||
| const userIds = _.map(members, m => `userId:${m.userId}`) || []; | ||
|
|
||
| const logger = req.log; | ||
| try { | ||
| const memberDetails = await util.getMemberDetailsByUserIds(userIds, logger, req.id); | ||
|
|
||
| // merge detail fields | ||
| _.forEach(members, (m) => { | ||
| const detail = _.find(memberDetails, mt => mt.userId === m.userId); | ||
| if (detail) { | ||
| _.assign(m, _.pick(detail, memberDetailFields)); | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| const TraitFields = ['photoURL', 'workingHourStart', 'workingHourEnd', 'timeZone']; | ||
| const traitFields = _.intersection(TraitFields, fields); | ||
| if (traitFields.length) { | ||
| const promises = []; | ||
| _.forEach(members, (m) => { | ||
| if (m.handle) { | ||
| promises.push(util.getMemberTratisByHandle(m.handle, req.log, req.id)); | ||
| } | ||
| }); | ||
|
|
||
| const traits = await Promise.all(promises); | ||
|
|
||
| // remove photoURL, because connect_info also has photoURL | ||
| const ConnectInfoFields = ['workingHourStart', 'workingHourEnd', 'timeZone']; | ||
| const connectInfoFields = _.intersection(ConnectInfoFields, fields); | ||
|
|
||
| // merge traits | ||
| _.forEach(members, (m) => { | ||
| const traitsArr = _.find(traits, t => t[0].userId === m.userId); | ||
| if (traitsArr) { | ||
| if (traitFields[0] === 'photoURL') { | ||
| _.assign(m, { | ||
| photoURL: _.get(_.find(traitsArr, { traitId: 'basic_info' }), 'traits.data[0].photoURL'), | ||
| }); | ||
| if (traitFields.length > 1) { | ||
| const traitInfo = _.get(_.find(traitsArr, { traitId: 'connect_info' }), 'traits.data[0]', {}); | ||
| _.assign(m, _.pick(traitInfo, connectInfoFields)); | ||
| } | ||
| } else { | ||
| const traitInfo = _.get(_.find(traitsArr, { traitId: 'connect_info' }), 'traits.data[0]', {}); | ||
| _.assign(m, _.pick(traitInfo, connectInfoFields)); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } catch (e) { | ||
| logger.error('Error getting member details', e); | ||
| if (hasUserIdField === false) { | ||
| members = _.map(members, m => _.omit(m, ['userId'])); | ||
| } | ||
| if (hasHandleField === false) { | ||
| members = _.map(members, m => _.omit(m, ['handle'])); | ||
| } | ||
| return res.json(util.wrapResponse(req.id, members)); | ||
| } | ||
| } | ||
|
|
||
| if (hasUserIdField === false) { | ||
| members = _.map(members, m => _.omit(m, ['userId'])); | ||
| } | ||
| if (hasHandleField === false) { | ||
| members = _.map(members, m => _.omit(m, ['handle'])); | ||
| } | ||
| async (req, res, next) => { | ||
| let fields = null; | ||
| if (req.query.fields) { | ||
| fields = req.query.fields.split(','); | ||
| } | ||
| try { | ||
| const memberFields = _.keys(models.ProjectMember.attributes); | ||
| const members = await util.getObjectsWithMemberDetails( | ||
| req.context.currentProjectMembers, fields, { | ||
| logger: req.log, | ||
| requestId: req.id, | ||
| memberFields, | ||
| }, | ||
| ); | ||
| return res.json(util.wrapResponse(req.id, members)); | ||
| } catch (err) { | ||
| return next(err); | ||
| } | ||
|
|
||
| return res.json(util.wrapResponse(req.id, req.context.currentProjectMembers)); | ||
| }, | ||
| ]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.