/**
 * @aurora-catalyst-generated
 * @source cliter/iam/account.aurora.yaml
 */
import { IamAccount } from '@api/graphql';
import { IamDeleteAccountsService, IamGetAccountsService } from '@app/iam/account';
import { IamAccountFieldSchema } from '@app/iam/account/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 IamDeleteAccountsHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteService: IamDeleteAccountsService,
    private readonly getService: IamGetAccountsService,
  ) {}

  @EmitEvent('iam.account.deleted')
  @Format(IamAccountFieldSchema)
  async main(
    queryStatement?: QueryStatement,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<IamAccount[]> {
    const accounts = await this.getService.main(queryStatement, constraint, handlerMeta);

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

    return accounts;
  }
}
