/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/scope.aurora.yaml
 */
import { OAuthCreateScopeInput, OAuthScope } from '@api/graphql';
import { OAuthCreateScopeService, OAuthFindScopeByIdService } from '@app/o-auth/scope';
import { OAuthScopeFieldSchema } from '@app/o-auth/scope/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthCreateScopeHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: OAuthCreateScopeService,
    private readonly findByIdService: OAuthFindScopeByIdService,
  ) {}

  @EmitEvent('oAuth.scope.created')
  @ApplySchema(OAuthScopeFieldSchema)
  async main(payload: OAuthCreateScopeInput, handlerMeta?: HandlerMeta): Promise<OAuthScope> {
    await this.createService.main(payload, handlerMeta);

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