import {Args, Context, Int, Mutation, Resolver} from '@nestjs/graphql';
import { Organizer } from '../models/Organizer';
import { Client } from '../../client';
import { OrganizerService } from '../organizer.service';
import { HttpException } from '@nestjs/common';

@Resolver(() => Organizer)
export class OrganizerResolverM {
  constructor(
    private readonly service: OrganizerService,
  ) {}

  @Mutation(() => Organizer, { nullable: false })
  async OrganizerRegister(
    @Context('client') client: Client,
    @Args('name') name: string,
    @Args('plz', { type: () => Int, nullable: true }) plz?: number,
    @Args('ort', { nullable: true }) ort?: string,
  ): Promise<Organizer> {
    const filter: any = {}

    if (name) filter.name = name;
    if (plz) filter.plz = plz;
    if (ort) filter.ort = ort;

    let tmp = await this.service.find(filter);
    if (tmp.length === 0)
      return this.service.create(client, name, plz, ort);
    return tmp[0];
  }
}
