Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CrudAuth Filter not Working Properly #64

Open
abdussamadbello opened this issue Aug 16, 2024 · 3 comments
Open

CrudAuth Filter not Working Properly #64

abdussamadbello opened this issue Aug 16, 2024 · 3 comments

Comments

@abdussamadbello
Copy link

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
Copy link
Author

Please any Ideas @zaro ?

@danyalutsevich
Copy link

danyalutsevich commented Aug 31, 2024

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

@helmsonsen
Copy link

there seems to be a mismatch between

@OneToMany(() => User, (user) => user.organization)
  users?: User[];

and the query join definition

user: {
  eager: true,
  alias: "user",
  allow: ["id"],
  required: true,
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants