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

[Bug]: No way to add a before hook custom filter depending on an array #1703

Open
CharlesParmentier opened this issue Aug 20, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@CharlesParmentier
Copy link

CharlesParmentier commented Aug 20, 2024

Contact Details

No response

What happened?

I want to add a before hook on the list action that can pre filter my list of records depending on an array.
In the following example I want to pre filter the list of users depending on if their id is in a list of ids.
I'm using fastify as a server and typeORM for my database.

the '$in' operator seems not to be supported and always returns error like 'Cannot read properties of undefined (reading 'type')'
I also tried to do custom filter but there is no way to actual create a new Filter and use it inside adminJS hook function.

There is a similar issue opened with no response : #1564

Bug prevalence

Whenever I want to load the list of users

AdminJS dependencies version

"@adminjs/design-system": "^4.1.1",
"@adminjs/fastify": "^4.1.0",
"@adminjs/relations": "^1.1.2",
"@adminjs/themes": "^1.0.1",
"@adminjs/typeorm": "^5.0.0",

What browsers do you see the problem on?

Chrome

Relevant log output

"res":{"statusCode":500},"err":{"type":"TypeError","message":"Cannot read properties of undefined (reading 'type')","stack":"TypeError: Cannot read properties of undefined (reading 'type')\n    at Object.isParserForType (file://node_modules/@adminjs/typeorm/lib/utils/filter/date-filter.parser.js:3:80)\n    at file://node_modules/@adminjs/typeorm/lib/utils/filter/filter.converter.js:10:46\n    at Array.find (<anonymous>)\n    at file://node_modules/@adminjs/typeorm/lib/utils/filter/filter.converter.js:10:32\n    at Array.forEach (<anonymous>)\n    at convertFilter (file://node_modules/@adminjs/typeorm/lib/utils/filter/filter.converter.js:9:35)\n    at Resource.find (file://node_modules/@adminjs/typeorm/lib/Resource.js:48:20)\n    at Object.handler (file://node_modules/adminjs/lib/backend/actions/list/list-action.js:66:36)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"},"msg":"Cannot read properties of undefined (reading 'type')"}

Relevant code that's giving you issues

export const filter = async (request: ActionRequest, context: ActionContext) => {
  const { query = {} } = request;
  const { currentAdmin } = context;

  // If the current user is admin, do not filter
  if (currentAdmin.role === Role.Admin) {
    return request;
  } else {
    // For non-admin users, find associated centers
    const userRepository = datasource.getRepository(User);
    const connectedUser = await userRepository.findOne({
      where: { email: currentAdmin.email },
    });
    if(connectedUser) {

      const prescriptionIds = ['id1', 'id2', 'id3'];
  
      const newQuery = {
        ...query,
        filters: {
          ...query.filters,
          id: { $in: prescriptionIds }, // <--- Here is the problem
        },
      };
  
      request.query = newQuery;
      return request;
    }
  }
};
@CharlesParmentier CharlesParmentier added the bug Something isn't working label Aug 20, 2024
@kraynel
Copy link

kraynel commented Aug 20, 2024

Not pretty but you can override the find method called on the resource to bypass the whole unflatten/flatten filter mechanism. It is specific to typeorm:

const customBefore = (request, context) => {
  const oldFind = context.resource.find.bind(context.resource);

  context.resource.find = (filter, options, context) => {
    filter.filters = {
      ...filter.filters,
      id: {
        custom: In(['id1', 'id2', 'id3'])
      }
    };
    return oldFind(filter, options, context);
  }

  return request
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants