Skip to content

Commit

Permalink
feat: Do not query /jobs/triggers with a partialFilter
Browse files Browse the repository at this point in the history
One might want to avoid querying the `/jobs/triggers` route and stick to
the `/data` route, to avoid extra work from the stack, that can be
costful.
We were using the existence of `worker` and `type` keys in selector, but
a partialFilter existence should also force the use of the `/data`
route.
  • Loading branch information
paultranvan committed Dec 10, 2024
1 parent e945d59 commit bb380fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/cozy-stack-client/src/TriggerCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class TriggerCollection extends DocumentCollection {
*/
async find(selector = {}, options = {}) {
const { worker, type, ...rest } = selector
const hasOnlyWorkerAndType = Object.keys(rest).length === 0
const hasOnlyWorkerAndType =
Object.keys(rest).length === 0 && !options.partialFilter
if (hasOnlyWorkerAndType) {
// @see https://github.com/cozy/cozy-stack/blob/master/docs/jobs.md#get-jobstriggers
const url = `/jobs/triggers?${buildParamsUrl(worker, type)}`
Expand Down
14 changes: 14 additions & 0 deletions packages/cozy-stack-client/src/TriggerCollection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ describe('TriggerCollection', () => {
}
)
})

it('should call /data/io.cozy.triggers/_find route if partialFilter is passed', async () => {
stackClient.fetchJSON.mockReturnValue(FIND_RESPONSE_FIXTURES)
await collection.find({}, { partialFilter: { worker: 'konnector' } })
expect(stackClient.fetchJSON).toHaveBeenLastCalledWith(
'POST',
'/data/io.cozy.triggers/_find',
{
selector: { worker: 'konnector' },
skip: 0,
use_index: '_design/by__filter_(worker_konnector)'
}
)
})
})

describe('destroy', () => {
Expand Down

0 comments on commit bb380fa

Please sign in to comment.