import { Get , Post , Delete , Authorized , Param , Body , CurrentUser , UseBefore , JsonController , BadRequestError , ForbiddenError } from 'routing-controllers' ; import passportJwtMiddleware from '../security/passportJwtMiddleware' ; import { WhitelistUser } from '../models/WhitelistUser' ; import { errorCodes } from '../config/errorCodes' ; import * as mongoose from 'mongoose' ; import ObjectId = mongoose . Types . ObjectId ; import { Course } from '../models/Course' ; import { User } from '../models/User' ; import { IWhitelistUser } from '../../../shared/models/IWhitelistUser' ; import { IUser } from '../../../shared/models/IUser' ; @ JsonController ( '/whitelist' ) @ UseBefore ( passportJwtMiddleware ) export class WhitelistController { api apiSuccessExample apiParam @ Get ( '/check/:whitelist' ) @ Authorized ( [ 'teacher' , 'admin' ] ) async checkWhitelistForExistingStudents ( @ Param ( 'whitelist' ) whitelistToCheck ) { return await Promise . all ( whitelistToCheck . map ( async ( uid ) => { { return { uid , exists : ! ! ( await User . findOne ( { uid : uid } ) ) } ; } } ) ) ; } api apiName apiGroup apiParam apiSuccess apiSuccessExample @ Get ( '/:id' ) @ Authorized ( [ 'teacher' , 'admin' ] ) async getUser ( @ Param ( 'id' ) id , @ CurrentUser ( ) currentUser ) { const whitelistUser = await WhitelistUser . findById ( id ) ; const course = await Course . findById ( whitelistUser . courseId ) ; if ( ! course . checkPrivileges ( currentUser ) . userCanEditCourse ) { throw new ForbiddenError ( ) ; } return whitelistUser . toObject ( { virtuals : true } ) ; } api apiName apiGroup apiPermission apiPermission apiParam apiSuccess apiSuccessExample apiError @ Post ( '/' ) @ Authorized ( [ 'teacher' , 'admin' ] ) async addWhitelistUser ( @ Body ( ) whitelistUser , @ CurrentUser ( ) currentUser ) { const course = await Course . findById ( whitelistUser . courseId ) ; if ( ! course . checkPrivileges ( currentUser ) . userCanEditCourse ) { throw new ForbiddenError ( ) ; } let savedWhitelistUser ; try { savedWhitelistUser = await new WhitelistUser ( this . prepareWhitelistUserData ( whitelistUser ) ) . save ( ) ; } catch ( err ) { throw new BadRequestError ( errorCodes . whitelist . duplicateWhitelistUser . text ) ; } await this . addUserIfFound ( whitelistUser ) ; return savedWhitelistUser . toObject ( ) ; } api apiName apiGroup apiPermission apiPermission apiParam apiParam apiSuccess apiSuccessExample apiError api apiName apiGroup apiPermission apiPermission apiParam apiSuccess apiSuccessExample @ Delete ( '/:id' ) @ Authorized ( [ 'teacher' , 'admin' ] ) async deleteWhitelistUser ( @ Param ( 'id' ) id , @ CurrentUser ( ) currentUser ) { const whitelistUser = await WhitelistUser . findById ( id ) ; const course = await Course . findById ( whitelistUser . courseId ) ; if ( ! course . checkPrivileges ( currentUser ) . userCanEditCourse ) { throw new ForbiddenError ( ) ; } await WhitelistUser . deleteOne ( { _id : id } ) ; await this . deleteUserIfFound ( whitelistUser ) ; return { } ; } private async deleteUserIfFound ( whitelistUser ) { const course = await Course . findById ( whitelistUser . courseId ) . populate ( 'students' ) ; if ( course ) { course . students = course . students . filter ( stud => stud . uid . toString ( ) !== whitelistUser . uid ) ; await course . save ( ) ; } } private async addUserIfFound ( whitelistUser ) { const stud = await User . findOne ( { uid : whitelistUser . uid , 'profile.firstName' : { $regex : new RegExp ( '^' + whitelistUser . firstName . toLowerCase ( ) , 'i' ) } , 'profile.lastName' : { $regex : new RegExp ( '^' + whitelistUser . lastName . toLowerCase ( ) , 'i' ) } } ) ; if ( stud ) { const course = await Course . findById ( whitelistUser . courseId ) ; course . students . push ( stud ) ; await course . save ( ) ; } } private prepareWhitelistUserData ( whitelistUser ) { return { _id : whitelistUser . _id , firstName : whitelistUser . firstName , lastName : whitelistUser . lastName , uid : whitelistUser . uid , courseId : new ObjectId ( whitelistUser . courseId ) } ; } }