/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/access-token.aurora.yaml
 */
import { OAuthAccessToken, OAuthCreateAccessTokenInput } from '@api/graphql';
import {
  OAuthCreateAccessTokenService,
  OAuthFindAccessTokenByIdService,
} from '@app/o-auth/access-token';
import { OAuthAccessTokenFieldSchema } from '@app/o-auth/access-token/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthCreateAccessTokenHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: OAuthCreateAccessTokenService,
    private readonly findByIdService: OAuthFindAccessTokenByIdService,
  ) {}

  @EmitEvent('oAuth.accessToken.created')
  @ApplySchema(OAuthAccessTokenFieldSchema)
  async main(
    payload: OAuthCreateAccessTokenInput,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthAccessToken> {
    await this.createService.main(payload, handlerMeta);

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