1-
2-
31import _ from 'lodash' ;
2+ import Joi from 'joi' ;
3+ import validate from 'express-validation' ;
44import { middleware as tcMiddleware } from 'tc-core-library-js' ;
55import models from '../../models' ;
66import util from '../../util' ;
@@ -9,27 +9,50 @@ import util from '../../util';
99 * API to update invite member to project.
1010 *
1111 */
12+ const schema = {
13+ query : {
14+ fields : Joi . string ( ) . optional ( ) ,
15+ } ,
16+ } ;
1217const permissions = tcMiddleware . permissions ;
1318
1419module . exports = [
15- // handles request validations
20+ validate ( schema ) ,
1621 permissions ( 'projectMemberInvite.get' ) ,
17- ( req , res , next ) => {
18- const projectId = _ . parseInt ( req . params . projectId ) ;
19- const currentUserId = req . authUser . userId ;
20- let invite ;
21- return models . ProjectMemberInvite . getPendingInviteByEmailOrUserId ( projectId , req . authUser . email , currentUserId )
22- . then ( ( _invite ) => {
23- invite = _invite ;
24- if ( ! invite ) {
25- // check there is an existing invite for the user with status PENDING
26- // handle 404
27- const err = new Error ( 'invite not found for project id ' +
28- `${ projectId } , userId ${ currentUserId } , email ${ req . authUser . email } ` ) ;
29- err . status = 404 ;
30- return next ( err ) ;
31- }
32- return res . json ( util . wrapResponse ( req . id , invite ) ) ;
33- } ) ;
22+ async ( req , res , next ) => {
23+ try {
24+ const projectId = _ . parseInt ( req . params . projectId ) ;
25+ const currentUserId = req . authUser . userId ;
26+ const invite = await models . ProjectMemberInvite . getPendingInviteByEmailOrUserId (
27+ projectId , req . authUser . email , currentUserId ,
28+ ) ;
29+ if ( ! invite ) {
30+ // check there is an existing invite for the user with status PENDING
31+ // handle 404
32+ const err = new Error (
33+ 'invite not found for project id ' +
34+ `${ projectId } , userId ${ currentUserId } , email ${ req . authUser . email } ` ,
35+ ) ;
36+ err . status = 404 ;
37+ throw err ;
38+ }
39+
40+ let fields = null ;
41+ if ( req . query . fields ) {
42+ fields = req . query . fields . split ( ',' ) ;
43+ }
44+ let inviteWithDetails ;
45+ try {
46+ [ inviteWithDetails ] = await util . getObjectsWithMemberDetails ( [ invite ] , fields , req ) ;
47+ } catch ( err ) {
48+ inviteWithDetails = invite ;
49+ req . log . error ( 'Cannot get user details for invite.' ) ;
50+ req . log . debug ( 'Error during getting user details for invite.' , err ) ;
51+ }
52+
53+ return res . json ( util . wrapResponse ( req . id , inviteWithDetails ) ) ;
54+ } catch ( err ) {
55+ return next ( err ) ;
56+ }
3457 } ,
3558] ;
0 commit comments