fix: sort on get all jobs#2745
Open
nitrosx wants to merge 5 commits into
Open
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The detection of "impossible" access queries via
JSON.stringify(q) === JSON.stringify({ $expr: { $eq: [0, 1] }})is brittle and hard to read; consider centralizing this sentinel expression in a shared constant or helper and comparing structurally instead of via stringification. - In
jobsMongoQueryReadAccess, the different return shapes ({}, the$exprimpossible match, single query,$orarray) are quite implicit; extracting small helpers (e.g.hasUnrestrictedAccess,hasNoAccess) would make the control flow and intent clearer.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The detection of "impossible" access queries via `JSON.stringify(q) === JSON.stringify({ $expr: { $eq: [0, 1] }})` is brittle and hard to read; consider centralizing this sentinel expression in a shared constant or helper and comparing structurally instead of via stringification.
- In `jobsMongoQueryReadAccess`, the different return shapes (`{}`, the `$expr` impossible match, single query, `$or` array) are quite implicit; extracting small helpers (e.g. `hasUnrestrictedAccess`, `hasNoAccess`) would make the control flow and intent clearer.
## Individual Comments
### Comment 1
<location path="src/casl/casl-ability.factory.ts" line_range="1936-1939" />
<code_context>
+ ];
+
+ // Remove impossible queries
+ const meaningfulQueries = queries.filter(
+ (q) =>
+ JSON.stringify(q) !==
+ JSON.stringify({
+ $expr: { $eq: [0, 1] },
+ }),
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid using JSON.stringify-based equality to detect the "no access" query shape.
Using `JSON.stringify` for query equality is brittle because it depends on property order and the exact current shape of the "no access" query from `accessibleBy`. Instead, either:
- Detect an explicit sentinel value (if `accessibleBy` can provide one),
- Check the key structure directly (e.g. `q.$expr?.$eq?.[0] === 0 && q.$expr?.$eq?.[1] === 1`), or
- Wrap `accessibleBy` to return a discriminated type for the impossible case.
This avoids subtle breakages if the upstream query shape changes.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Junjiequan
reviewed
May 20, 2026
Junjiequan
approved these changes
May 21, 2026
Member
Junjiequan
left a comment
There was a problem hiding this comment.
I'm approving it as it's a bug fix and fixes the root cause that PR #2736 did not address, please merge after the failign tests are resolved.
A few things maybe worth noting for future considerations:
meaningfulQueriescould be renamed to something more descriptive based on the context, such asvalidQueries- The mongoQuery building logic
isImpossibleQuery,isEmptyObject,meaningfulQueriesetc. might be worth moving to utility layer, so we can scope down casl-ability.ts to rule definitions only. { $expr: { $eq: [0, 1] } }could benefit from a short comment. e.g,// it's generated by accessibleBy() when no rules match, forcing the query to return no results.
Member
Author
|
@Junjiequan I addressed your concerns, except moving the functions to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR implement and fix the sorting of jobs when retrieving a number of jobs using
/api/v4/jobs. It also improves the building process for the mongo pipeline used to retrieve the jobs in the jobs service.Motivation
Working on an ESS specific task, I realized that the jobs were returned in the wrong order when requesting them from the most recent to the oldest. The Filter passed to the GET
/api/v4/jobsis:Fixes
Changes:
Tests included
Documentation
official documentation info
Summary by Sourcery
Fix job listing sort behavior and refine MongoDB query construction for job access control and retrieval.
Bug Fixes:
Enhancements: