/**
 * @aurora-catalyst-generated
 * @source cliter/iam/permission.aurora.yaml
 */
import { IamCreatePermissionInput, IamPermission } from '@api/graphql';
import { IamCreatePermissionService, IamFindPermissionByIdService } from '@app/iam/permission';
import { IamPermissionFieldSchema } from '@app/iam/permission/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreatePermissionHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreatePermissionService,
    private readonly findByIdService: IamFindPermissionByIdService,
  ) {}

  @EmitEvent('iam.permission.created')
  @ApplySchema(IamPermissionFieldSchema)
  async main(payload: IamCreatePermissionInput, handlerMeta?: HandlerMeta): Promise<IamPermission> {
    await this.createService.main(payload, handlerMeta);

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