/**
 * @aurora-catalyst-generated
 * @source cliter/iam/user.aurora.yaml
 */
import { IamUser } from '@api/graphql';
import { IamDeleteUsersService, IamGetUsersService } from '@app/iam/user';
import { IamUserFieldSchema } from '@app/iam/user/domain';
import { EmitEvent, Format, HandlerMeta } from '@aurorajs.dev/core-back';
import { QueryStatement } from '@aurorajs.dev/core-common';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamDeleteUsersHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteService: IamDeleteUsersService,
    private readonly getService: IamGetUsersService,
  ) {}

  @EmitEvent('iam.user.deleted')
  @Format(IamUserFieldSchema)
  async main(
    queryStatement?: QueryStatement,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<IamUser[]> {
    const users = await this.getService.main(queryStatement, constraint, handlerMeta);

    await this.deleteService.main(queryStatement, constraint, handlerMeta);

    return users;
  }
}
