/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/credential.aurora.yaml
 */
import { OAuthCredential } from '@api/graphql';
import { OAuthFindCredentialByIdService } from '@app/o-auth/credential';
import { OAuthCredentialFieldSchema } from '@app/o-auth/credential/domain';
import { Format, HandlerMeta } from '@aurorajs.dev/core-back';
import { QueryStatement } from '@aurorajs.dev/core-common';
import { Injectable, NotFoundException } from '@nestjs/common';

@Injectable()
export class OAuthFindCredentialByIdHandler {
  constructor(private readonly findByIdService: OAuthFindCredentialByIdService) {}

  @Format(OAuthCredentialFieldSchema)
  async main(
    id: string,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthCredential> {
    const credential = await this.findByIdService.main(id, constraint, handlerMeta);

    if (!credential) {
      throw new NotFoundException(`OAuthCredential with id: ${id}, not found`);
    }

    return credential;
  }
}
