import { Arg, Ctx, Mutation, Resolver, Directive } from 'type-graphql'

import { EvaluationStatus } from './evaluation-status'
import { NewEvaluationStatus, EvaluationStatusPatch } from './evaluation-status-type'

@Resolver(EvaluationStatus)
export class EvaluationStatusMutation {
  @Directive('@transaction')
  @Mutation(returns => EvaluationStatus, { nullable: true, description: 'To create new EvaluationStatus' })
  async createEvaluationStatus(
    @Arg('evaluationStatus') evaluationStatus: NewEvaluationStatus,
    @Ctx() context: ResolverContext
  ): Promise<EvaluationStatus> {
    const { tx } = context.state
    return await tx.getRepository(EvaluationStatus).save({
      ...evaluationStatus
    })
  }

  @Directive('@transaction')
  @Mutation(returns => EvaluationStatus, { description: 'To modify EvaluationStatus information' })
  async updateEvaluationStatus(
    @Arg('id') id: string,
    @Arg('patch') patch: EvaluationStatusPatch,
    @Ctx() context: ResolverContext
  ): Promise<EvaluationStatus> {
    const { tx } = context.state
    const repository = tx.getRepository(EvaluationStatus)

    const evaluateStatus = await repository.findOneBy({ id })

    return await repository.save({
      ...evaluateStatus,
      ...patch
    })
  }
}
