import {Args, Resolver, Subscription} from '@nestjs/graphql'
import {Organizer} from '../models/Organizer'
import {pubsub} from '../../main'
import {UUID} from '../../global/scalars/UUID'
import {OrganizerService} from '../organizer.service'

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

  @Subscription(() => Organizer, {
    nullable: false,
    filter: async (payload, variables, context) => {
      let ret = true;

      if (ret && !!variables.organizer)
        ret = payload?.OrganizerUpdated?._id === variables?.organizer
          && (context?.client?.isMaster() || context?.client?.isAdmin(variables.organizer));

      return ret;
    }
  })
  async OrganizerUpdated(
    @Args('organizer', { type: () => UUID, nullable: true }) organizer: UUID,
  ) {
    return pubsub.asyncIterator('OrganizerUpdated');
  }

  @Subscription(() => UUID, { nullable: false })
  async OrganizerDeleted() {
    return pubsub.asyncIterator('OrganizerDeleted');
  }
}