/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/refresh-token.aurora.yaml
 */
import { OAuthCreateRefreshTokenInput, OAuthRefreshToken } from '@api/graphql';
import {
  OAuthCreateRefreshTokenService,
  OAuthFindRefreshTokenByIdService,
} from '@app/o-auth/refresh-token';
import { OAuthRefreshTokenFieldSchema } from '@app/o-auth/refresh-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 OAuthCreateRefreshTokenHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: OAuthCreateRefreshTokenService,
    private readonly findByIdService: OAuthFindRefreshTokenByIdService,
  ) {}

  @EmitEvent('oAuth.refreshToken.created')
  @ApplySchema(OAuthRefreshTokenFieldSchema)
  async main(
    payload: OAuthCreateRefreshTokenInput,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthRefreshToken> {
    await this.createService.main(payload, handlerMeta);

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