Skip to content

Commit

Permalink
fix: Optimize the request
Browse files Browse the repository at this point in the history
No need to get the current_state (of mapping all the jobs) for the
triggers here. So let's make a call to /data/io.cozy.triggers instead
  • Loading branch information
Crash-- committed Oct 12, 2023
1 parent db4a954 commit b948b48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
17 changes: 16 additions & 1 deletion packages/cozy-harvest-lib/src/connections/accounts.js
Expand Up @@ -237,7 +237,22 @@ export const fetchAccountsWithoutTriggers = async (client, triggers) => {
* @return {Account} An account without trigger for the given konnector
*/
export const fetchReusableAccount = async (client, konnector) => {
const { data: triggers } = await client.collection('io.cozy.triggers').all()
// the where is there because ATM TriggerCollection check if there is
// a selector to know if it should call /jobs/triggers or
// /data/io.cozy.triggers.
// In our case, we only want the triggers without the job, so we want
// to use /data/io.cozy.triggers
const triggers = await client.queryAll(
Q('io.cozy.triggers')
.where({
_id: { $gt: null }
})
.partialIndex({
worker: { $in: ['konnector', 'client'] }
})
.indexFields(['_id'])
)

const accountsWithoutTrigger = await fetchAccountsWithoutTriggers(
client,
triggers
Expand Down
13 changes: 4 additions & 9 deletions packages/cozy-harvest-lib/src/connections/accounts.spec.js
Expand Up @@ -417,20 +417,15 @@ describe('Account mutations', () => {
describe('fetchReusableAccount', () => {
const setup = ({ accounts, triggers }) => {
const client = new CozyClient({})
client.collection = jest.fn(doctype => {
if (doctype === 'io.cozy.triggers') {
return {
all: jest.fn().mockResolvedValue({ data: triggers })
}
} else {
throw new Error(`client.collection for ${doctype} is not mocked`)
}
})
client.query = jest.fn().mockImplementation(() => {
return {
data: accounts
}
})

client.queryAll = jest.fn().mockImplementation(() => {
return triggers
})
return { client }
}
it('should return the right account when possible', async () => {
Expand Down

0 comments on commit b948b48

Please sign in to comment.