diff --git a/packages/cozy-client/src/CozyClient.js b/packages/cozy-client/src/CozyClient.js index a9f033dc3f..cf3bd68e1d 100644 --- a/packages/cozy-client/src/CozyClient.js +++ b/packages/cozy-client/src/CozyClient.js @@ -1107,24 +1107,31 @@ client.query(Q('io.cozy.bills'))`) * @returns {Promise} */ async persistVirtualDocuments(definition, data) { + const enforceList = ['io.cozy.files.shortcuts'] + + const enforce = enforceList.includes(definition.doctype) + if (definition.doctype === 'io.cozy.apps_registry') { // io.cozy.apps_registry has a dedicated endpoint on cozy-stack that // returns data different than the one stored in database // We want to store the full answer data under the `maintenance` id // so we can query it from the Pouch the same way we query it from the stack - return await this.persistVirtualDocument({ - _type: 'io.cozy.apps_registry', - _id: 'maintenance', - // @ts-ignore - cozyPouchData: data - }) + return await this.persistVirtualDocument( + { + _type: 'io.cozy.apps_registry', + _id: 'maintenance', + // @ts-ignore + cozyPouchData: data + }, + enforce + ) } if (!Array.isArray(data)) { - await this.persistVirtualDocument(data) + await this.persistVirtualDocument(data, enforce) } else { for (const document of data) { - await this.persistVirtualDocument(document) + await this.persistVirtualDocument(document, enforce) } } } @@ -1134,12 +1141,12 @@ client.query(Q('io.cozy.bills'))`) * * @private * @param {CozyClientDocument} document - Document to be saved + * @param {boolean} enforce - When true, save the document even if `meta.rev` exists * @returns {Promise} */ - async persistVirtualDocument(document) { + async persistVirtualDocument(document, enforce) { if ( - document && - !document.meta?.rev && + ((document && !document.meta?.rev) || enforce) && !document.cozyLocalOnly && !document.cozyFromPouch ) { diff --git a/packages/cozy-client/src/CozyClient.spec.js b/packages/cozy-client/src/CozyClient.spec.js index 503983abf6..22d8f8a37b 100644 --- a/packages/cozy-client/src/CozyClient.spec.js +++ b/packages/cozy-client/src/CozyClient.spec.js @@ -1456,6 +1456,25 @@ describe('CozyClient', () => { expect(persistHandler).not.toHaveBeenCalled() }) + it('should enforce persisting io.cozy.files.shortcuts as virtual documents even if meta.rev exists', async () => { + jest.spyOn(client, 'requestQuery') + requestHandler.mockResolvedValue({ + data: [ + { + _id: 'some_id', + meta: { + rev: 'SOME_REV' + } + } + ] + }) + const shortcutsQuery = Q('io.cozy.files.shortcuts') + + await client.query(shortcutsQuery, { as: 'allShortcuts' }) + + expect(persistHandler).toHaveBeenCalled() + }) + describe('relationship with query failure', () => { beforeEach(() => { jest.spyOn(HasManyFiles, 'query').mockImplementation(() => {