/**
 * @aurora-catalyst-generated
 * @source cliter/iam/role.aurora.yaml
 */
import { IamCreateRoleInput, IamRole } from '@api/graphql';
import { IamCreateRoleService, IamFindRoleByIdService } from '@app/iam/role';
import { IamRoleFieldSchema } from '@app/iam/role/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreateRoleHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreateRoleService,
    private readonly findByIdService: IamFindRoleByIdService,
  ) {}

  @EmitEvent('iam.role.created')
  @ApplySchema(IamRoleFieldSchema)
  async main(payload: IamCreateRoleInput, handlerMeta?: HandlerMeta): Promise<IamRole> {
    await this.createService.main(payload, handlerMeta);

    return await this.findByIdService.main(payload.id, {}, handlerMeta);
  }
}
