/**
 * @aurora-catalyst-generated
 * @source cliter/iam/role-account.aurora.yaml
 */
import { IamCreateRoleAccountInput, IamRoleAccount } from '@api/graphql';
import { IamCreateRoleAccountService, IamFindRoleAccountByIdService } from '@app/iam/role-account';
import { IamRoleAccountFieldSchema } from '@app/iam/role-account/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreateRoleAccountHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreateRoleAccountService,
    private readonly findByIdService: IamFindRoleAccountByIdService,
  ) {}

  @EmitEvent('iam.roleAccount.created')
  @ApplySchema(IamRoleAccountFieldSchema)
  async main(
    payload: IamCreateRoleAccountInput,
    handlerMeta?: HandlerMeta,
  ): Promise<IamRoleAccount> {
    await this.createService.main(payload, handlerMeta);

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