import { ERROR_MESSAGES } from '@koh/common' ; import { BadRequestException , Injectable , NotFoundException , } from '@nestjs/common' ; import { RolesGuard } from './role.guard' ; import { UserModel } from '../profile/user.entity' ; import { QueueModel } from '../queue/queue.entity' ; import { QuestionModel } from '../question/question.entity' ; @ Injectable ( ) export class QuestionRolesGuard extends RolesGuard { async setupData ( request ) { let queueId ; if ( request . params . questionId ) { const question = await QuestionModel . findOne ( request . params . questionId ) ; if ( ! question ) { throw new NotFoundException ( ERROR_MESSAGES . questionRoleGuard . questionNotFound ) ; } queueId = question . queueId ; } else if ( request . body . queueId ) { queueId = request . body . queueId ; } else { throw new BadRequestException ( ERROR_MESSAGES . questionRoleGuard . queueOfQuestionNotFound ) ; } const queue = await QueueModel . findOne ( queueId ) ; if ( ! queue ) { throw new NotFoundException ( ERROR_MESSAGES . questionRoleGuard . queueDoesNotExist ) ; } const courseId = queue . courseId ; const user = await UserModel . findOne ( request . user . userId , { relations : [ 'courses' ] , } ) ; return { courseId , user } ; } }