gid-oss / dataui-nestjs-crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
124 stars 31 forks source link

CrudAuth Filter not Working Properly #64

Open abdussamadbello opened 4 weeks ago

abdussamadbello commented 4 weeks ago

I have my users joined to my organization entity but it's not being fetched/joined when I want to use the filter in crudAuth decorators .

 @OneToMany(() => User, (user) => user.organization)
  users?: User[];
import { Body, Controller, HttpStatus, Patch, Res, UseInterceptors } from "@nestjs/common";
import { OrganizationService } from "./organization.service";
import { CreateOrganizationDto } from "./dto/create-organization.dto";
import { UpdateOrganizationDto } from "./dto/update-organization.dto";
import { Crud, CrudAuth, CrudRequest, CrudRequestInterceptor, ParsedRequest } from "@dataui/crud";
import { ApiSecurity, ApiBearerAuth, ApiParam, ApiTags } from "@nestjs/swagger";
import { Organization } from "./entities/organization.entity";
import { FastifyReply } from "fastify";
import { MergeJsonb } from "src/interceptors/mergeJsonb.interceptor";

@ApiSecurity("x-organization-id")
@ApiBearerAuth()
@ApiTags("Organization")
@Crud({
  model: {
    type: Organization,
  },
  routes: {
    only: ["getOneBase", "getManyBase", "updateOneBase"],
    updateOneBase: {
      decorators: [ApiParam({ name: "id", type: String, description: "The ID of the organization to update" })],
      returnShallow: true,
    },
  },
  query: {
    alwaysPaginate: true,
    join: {
      entitlement: {
        eager: true,
        allow: ["id", "name"],
      },
      user: {
        eager: true,
        alias: "user",
        allow: ["id"],
        required: true,
      },
    },
  },
  dto: {
    create: CreateOrganizationDto,
    update: UpdateOrganizationDto,
  },
  params: {
    id: {
      field: "id",
      type: "string",
      primary: true,
    },
  },
})
@CrudAuth({
  property: "user",
  filter: (user) => {
    return { '"user"."id"': user.sub };
  },
  persist: (user) => ({ organization: { id: user.organizationId } }),
})
@Controller("organization")
export class OrganizationController {
  constructor(public service: OrganizationService) {}

  @UseInterceptors(CrudRequestInterceptor, MergeJsonb(Organization, ["onboarding"]))
  @ApiParam({ name: "id", type: String, description: "The ID of the organization to update" })
  @Patch(":id/onboarding")
  async updateCurrentUser(@ParsedRequest() req: CrudRequest, @Body() dto: UpdateOrganizationDto, @Res() res: FastifyReply) {
    const updatedUser = await this.service.updateOne(req, dto);
    res.code(HttpStatus.OK).send({ data: updatedUser });
  }
}

Query Generated and error

query failed: SELECT "Organization"."id" AS "Organization_id", "Organization"."created_at" AS "Organization_created_at", "Organization"."updated_at" AS "Organization_updated_at", "Organization"."deleted_at" AS "Organization_deleted_at", "Organization"."version" AS "Organization_version", "Organization"."created_by" AS "Organization_created_by", "Organization"."updated_by" AS "Organization_updated_by", "Organization"."name" AS "Organization_name", "Organization"."website" AS "Organization_website", "Organization"."address" AS "Organization_address", "Organization"."city" AS "Organization_city", "Organization"."state" AS "Organization_state", "Organization"."zip_code" AS "Organization_zip_code", "Organization"."country" AS "Organization_country", "Organization"."phone" AS "Organization_phone", "Organization"."type" AS "Organization_type", "Organization"."onboarding" AS "Organization_onboarding", "Organization"."owner_id" AS "Organization_owner_id" FROM "organization" "Organization" WHERE ( ("user"."id" = $1 AND "Organization"."id" = $2) ) AND ( "Organization"."deleted_at" IS NULL ) -- PARAMETERS: ["01J3XZT65BB24BY854ZGFWZS0G","01J3XZT52M46QHH39VPEMG0MRH"]
error: error: missing FROM-clause entry for table "user"
[Nest] 19491  - 08/16/2024, 1:58:46 AM   ERROR [ExceptionsHandler] missing FROM-clause entry for table "user"
QueryFailedError: missing FROM-clause entry for table "user"
    at PostgresQueryRunner.query (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/typeorm/driver/src/driver/postgres/PostgresQueryRunner.ts:331:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at SelectQueryBuilder.loadRawResults (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/typeorm/query-builder/src/query-builder/SelectQueryBuilder.ts:3805:25)
    at SelectQueryBuilder.executeEntitiesAndRawResults (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/typeorm/query-builder/src/query-builder/SelectQueryBuilder.ts:3551:26)
    at SelectQueryBuilder.getRawAndEntities (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/typeorm/query-builder/src/query-builder/SelectQueryBuilder.ts:1670:29)
    at SelectQueryBuilder.getOne (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/typeorm/query-builder/src/query-builder/SelectQueryBuilder.ts:1697:25)
    at OrganizationService.getOneOrFail (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/@dataui/crud-typeorm/src/typeorm-crud.service.ts:446:9)
    at OrganizationService.updateOne (/home/abdussamad/Advatyzly/Advatyzly-Backend/node_modules/@dataui/crud-typeorm/src/typeorm-crud.service.ts:187:19)
abdussamadbello commented 4 weeks ago

Please any Ideas @zaro ?

danyalutsevich commented 1 week ago

could you provide some repo with project that will reproduce your problem