import { InjectRepository } from '@nestjs/typeorm' ; import { Repository , FindConditions , FindManyOptions } from 'typeorm' ; import { Injectable , BadRequestException } from '@nestjs/common' ; import { Interview , interviewState } from './interview.entity' ; import { InterviewApplicationDto , InterviewAssignmentDto , InterviewAddEditDto } from './dto' ; import { UserService } from '../user/user.service' ; import { User } from '../user/user.entity' ; @ Injectable ( ) export class InterviewService { constructor ( @ InjectRepository ( Interview ) private readonly interviewRepo , public userService ) { } get ( options ) { const defaultOptions = { order : { date : 'DESC' } , } ; return this . interviewRepo . find ( { ... defaultOptions , ... options , } ) ; } async findOneById ( id ) { return this . interviewRepo . findOne ( id , { relations : [ 'interviewee' , 'interviewer' ] , } ) ; } async create ( dto ) { const { interviewerId , intervieweeId , ... restDto } = dto ; let interviewee ; let interviewer ; if ( interviewerId ) { interviewer = await this . userService . findOneById ( Number ( interviewerId ) , 'interviewer' ) ; } if ( intervieweeId ) { interviewee = await this . userService . findOneById ( Number ( intervieweeId ) , 'user' ) ; } const entity = { ... restDto , interviewee , interviewer , date : restDto . date || undefined , } ; const interview = this . interviewRepo . create ( entity ) ; return interview . save ( ) ; } async update ( toUpdate , dto ) { const { interviewerId , intervieweeId , ... restDto } = dto ; const interviewee = await this . userService . findOneById ( Number ( intervieweeId ) , 'user' ) ; let interviewer = null ; if ( interviewerId ) { interviewer = ( await this . userService . findOneById ( Number ( interviewerId ) , 'interviewer' ) ) || null ; } return this . interviewRepo . save ( { ... toUpdate , ... restDto , interviewee , interviewer , date : restDto . date || undefined , } ) ; } addApplication ( interviewApplicationDto , user ) { const entity = { ... interviewApplicationDto , interviewee : user } ; const interview = this . interviewRepo . create ( entity ) ; return interview . save ( ) ; } editApplication ( application , interviewApplicationDto ) { return this . interviewRepo . save ( { ... application , ... interviewApplicationDto } ) ; } async assign ( id , interviewAssignmentDto ) { const toUpdate = await this . interviewRepo . findOne ( id ) ; const { interviewerId , ... assignData } = interviewAssignmentDto ; const interviewer = await this . userService . findOneById ( Number ( interviewerId ) ) ; if ( ! interviewer ) { throw new BadRequestException ( "Can't find such user" ) ; } return this . interviewRepo . save ( { ... toUpdate , ... assignData , interviewer , state : interviewState . COMING , } ) ; } }