import { Body , Controller , Get , Post , Render , Req , Res , Param , UseGuards , UseFilters , NotFoundException , } from '@nestjs/common' ; import { Response , Request } from 'express' ; import { InterviewService } from './interview.service' ; import { UserService } from '../user/user.service' ; import { InterviewAssignmentDto , InterviewAddEditDto } from './dto' ; import { Role } from '../auth/role.decorator' ; import { RoleGuard , AuthenticatedGuard } from '../../common/guards' ; import { BadRequestExceptionFilter } from '../../common/filters/bad-request-exception.filter' ; import { User } from '../user/user.entity' ; import { Interview , interviewState } from './interview.entity' ; import { FormData } from '../../common/utils/form-data' ; import { prepareDate } from '../../common/utils/prepare-data.util' ; @ Controller ( 'interview/manage' ) @ UseGuards ( AuthenticatedGuard ) @ UseGuards ( RoleGuard ) export class InterviewManageController { constructor ( public interviewService , public userService ) { } @ Get ( ':id/assignment' ) @ Role ( 'admin' ) @ Render ( 'interview/assignment' ) async getAssignment ( @ Param ( 'id' ) id , @ Req ( ) req ) { const interview = await this . interviewService . findOneById ( id ) ; if ( ! interview ) { throw new NotFoundException ( ) ; } const interviewers = await this . userService . getInterviewers ( ) ; const renderData = { interview , interviewers } ; req . session ! . savedRenderData = renderData ; return renderData ; } @ Post ( ':id/assignment' ) @ Role ( 'admin' ) @ UseFilters ( new BadRequestExceptionFilter ( 'interview/assignment' ) ) async assign ( @ Param ( 'id' ) id , @ Body ( ) dto , @ Res ( ) res , @ Req ( ) req ) { await this . interviewService . assign ( id , dto ) ; delete req . session ! . savedRenderData ; res . redirect ( '/interview/manage/application' ) ; } @ Get ( 'new' ) @ Role ( 'admin' ) @ Render ( 'interview/add-edit' ) async getNew ( @ Req ( ) req ) { const interviewers = await this . userService . getInterviewers ( ) ; const interviewees = await this . userService . getInterviewees ( ) ; const renderData = { interviewees , interviewers , interviewState } ; req . session ! . savedRenderData = renderData ; return renderData ; } @ Post ( 'new' ) @ Role ( 'admin' ) @ UseFilters ( new BadRequestExceptionFilter ( 'interview/add-edit' ) ) async create ( @ Body ( ) dto , @ Res ( ) res , @ Req ( ) req ) { await this . interviewService . create ( dto ) ; delete req . session ! . savedRenderData ; res . redirect ( '/interview/manage/application' ) ; } @ Get ( ':id/edit' ) @ Role ( 'admin' ) @ Render ( 'interview/add-edit' ) async getEdit ( @ Req ( ) req , @ Param ( 'id' ) id ) { const interview = await this . interviewService . findOneById ( id ) ; if ( ! interview ) { throw new NotFoundException ( ) ; } const { interviewer , interviewee , date , createdAt , updatedAt , ... restInterview } = interview ; const editFormData = new FormData ( { ... restInterview , date : date ? prepareDate ( date ) : '' , intervieweeId : interview . interviewee ? interview . interviewee . id : '' , interviewerId : interview . interviewer ? interview . interviewer . id : '' , } ) ; const interviewers = await this . userService . getInterviewers ( ) ; const interviewees = await this . userService . getInterviewees ( ) ; const renderData = { editFormData , interviewees , interviewers , interviewState , id } ; req . session ! . savedRenderData = renderData ; return renderData ; } @ Post ( ':id/edit' ) @ Role ( 'admin' ) @ UseFilters ( new BadRequestExceptionFilter ( 'interview/add-edit' ) ) async edit ( @ Body ( ) dto , @ Param ( 'id' ) id , @ Res ( ) res , @ Req ( ) req ) { const interview = await this . interviewService . findOneById ( id ) ; if ( ! interview ) { throw new NotFoundException ( ) ; } await this . interviewService . update ( interview , dto ) ; delete req . session ! . savedRenderData ; res . redirect ( '/interview/manage/application' ) ; } @ Get ( ':state' ) @ Role ( 'admin' ) @ Render ( 'interview/manage' ) async findAll ( @ Param ( 'state' ) state ) { const stateOptions = { application : { where : { state : interviewState . WAIT_FOR_INTERVIEWER } , relations : [ 'interviewee' ] , } , coming : { where : { state : interviewState . COMING } , relations : [ 'interviewee' , 'interviewer' ] , } , passed : { where : { state : interviewState . PASSED } , relations : [ 'interviewee' , 'interviewer' ] , } , cancelled : { where : { state : interviewState . CANCELLED } , relations : [ 'interviewee' , 'interviewer' ] , } , } ; const options = stateOptions [ state ] ; if ( ! options ) { throw new NotFoundException ( ) ; } const interviews = await this . interviewService . get ( options ) ; return { interviews , options , state } ; } }