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

  @Format(IamPermissionRoleFieldSchema)
  async main(
    permissionId: string,
    roleId: string,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<IamPermissionRole> {
    const permissionRole = await this.findByIdService.main(
      permissionId,
      roleId,
      constraint,
      handlerMeta,
    );

    if (!permissionRole) {
      throw new NotFoundException(
        `IamPermissionRole with permissionId: ${permissionId}, roleId: ${roleId}, not found`,
      );
    }

    return permissionRole;
  }
}
