/**
 * @aurora-catalyst-generated
 * @source cliter/iam/permission.aurora.yaml
 */
import { IamPermission } from '@api/graphql';
import { IamFindPermissionByIdService } from '@app/iam/permission';
import { IamPermissionFieldSchema } from '@app/iam/permission/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 IamFindPermissionByIdHandler {
  constructor(private readonly findByIdService: IamFindPermissionByIdService) {}

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

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

    return permission;
  }
}
