import { Authorized , BodyParam , Get , JsonController , Put , UseBefore , CurrentUser , NotFoundError , ForbiddenError } from 'routing-controllers' ; import passportJwtMiddleware from '../security/passportJwtMiddleware' ; import { NotificationSettings } from '../models/NotificationSettings' ; import { Course } from '../models/Course' ; import { IUser } from '../../../shared/models/IUser' ; @ JsonController ( '/notificationSettings' ) @ UseBefore ( passportJwtMiddleware ) export class NotificationSettingsController { api apiName apiGroup apiPermission apiPermission apiPermission apiSuccess apiSuccessExample @ Authorized ( [ 'student' , 'teacher' , 'admin' ] ) @ Get ( '/' ) async getNotificationSettings ( @ CurrentUser ( ) currentUser ) { const notificationSettings = await NotificationSettings . find ( { user : currentUser } ) . populate ( 'user' ) . populate ( 'course' ) ; return notificationSettings . map ( settings => settings . forView ( ) ) ; } api apiName apiGroup apiPermission apiPermission apiPermission apiParam apiParam apiParam apiSuccess apiSuccessExample apiError apiError @ Authorized ( [ 'student' , 'teacher' , 'admin' ] ) @ Put ( '/' ) async putNotificationSettings ( @ BodyParam ( 'course' , { required : true } ) course , @ BodyParam ( 'notificationType' , { required : true } ) notificationType , @ BodyParam ( 'emailNotification' , { required : true } ) emailNotification , @ CurrentUser ( ) currentUser ) { const courseDoc = await Course . findById ( course ) . orFail ( new NotFoundError ( ) ) ; if ( ! courseDoc . checkPrivileges ( currentUser ) . userCanViewCourse ) { throw new ForbiddenError ( ) ; } await NotificationSettings . findOneAndUpdate ( { user : currentUser , course } , { user : currentUser , course , notificationType , emailNotification } , { new : true , upsert : true } ) ; return { } ; } }