/**
 * @aurora-catalyst-generated
 * @source cliter/iam/role.aurora.yaml
 */
import { IamRole } from '@api/graphql';
import { IamDeleteRolesService, IamGetRolesService } from '@app/iam/role';
import { IamRoleFieldSchema } from '@app/iam/role/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 IamDeleteRolesHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteService: IamDeleteRolesService,
    private readonly getService: IamGetRolesService,
  ) {}

  @EmitEvent('iam.role.deleted')
  @Format(IamRoleFieldSchema)
  async main(
    queryStatement?: QueryStatement,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<IamRole[]> {
    const roles = await this.getService.main(queryStatement, constraint, handlerMeta);

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

    return roles;
  }
}
