/**
 * @aurora-catalyst-generated
 * @source cliter/iam/tenant.aurora.yaml
 */
import { IamCreateTenantInput, IamTenant } from '@api/graphql';
import { IamCreateTenantService, IamFindTenantByIdService } from '@app/iam/tenant';
import { IamTenantFieldSchema } from '@app/iam/tenant/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreateTenantHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreateTenantService,
    private readonly findByIdService: IamFindTenantByIdService,
  ) {}

  @EmitEvent('iam.tenant.created')
  @ApplySchema(IamTenantFieldSchema)
  async main(payload: IamCreateTenantInput, handlerMeta?: HandlerMeta): Promise<IamTenant> {
    await this.createService.main(payload, handlerMeta);

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