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

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

    if (!permission) {
      throw new NotFoundException(`IamPermission not found`);
    }

    return permission;
  }
}
