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

@Injectable()
export class IamCreateUserHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreateUserService,
    private readonly findByIdService: IamFindUserByIdService,
  ) {}

  @EmitEvent('iam.user.created')
  @ApplySchema(IamUserFieldSchema)
  async main(payload: IamCreateUserInput, handlerMeta?: HandlerMeta): Promise<IamUser> {
    await this.createService.main(payload, handlerMeta);

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