From 8cf4478292710bf1878a14713001e7c7fee6c042 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Tue, 13 Aug 2024 16:44:44 +0200 Subject: [PATCH] feat: Add check to `_rev` attribute when persisting virtual documents In previous commit we added a check to `meta.rev` attribute in order to discriminate documents that exist in remote CouchDB from the ones that don't This approach would work only for documents handled by the cozy-stack using JSON-API But some CouchDB documents are served directly through the `data` route and are not wrapped into JSON-API When this is the case, they have a `_rev` attribute instead of a `meta.rev` one So we want to check for both forms before persisting virtual documents --- packages/cozy-client/src/CozyClient.js | 12 ++++---- packages/cozy-client/src/CozyClient.spec.js | 34 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/cozy-client/src/CozyClient.js b/packages/cozy-client/src/CozyClient.js index 323da62e0b..e29a7115ad 100644 --- a/packages/cozy-client/src/CozyClient.js +++ b/packages/cozy-client/src/CozyClient.js @@ -1142,15 +1142,15 @@ 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 + * @param {boolean} enforce - When true, save the document even if `meta.rev` or `_rev` exist * @returns {Promise} */ async persistVirtualDocument(document, enforce) { - if ( - ((document && !document.meta?.rev) || enforce) && - !document.cozyLocalOnly && - !document.cozyFromPouch - ) { + if (!document || document.cozyLocalOnly || document.cozyFromPouch) { + return + } + + if ((!document.meta?.rev && !document._rev) || enforce) { await this.chain.persistData(document) } } diff --git a/packages/cozy-client/src/CozyClient.spec.js b/packages/cozy-client/src/CozyClient.spec.js index 22d8f8a37b..5307e98079 100644 --- a/packages/cozy-client/src/CozyClient.spec.js +++ b/packages/cozy-client/src/CozyClient.spec.js @@ -1393,7 +1393,7 @@ describe('CozyClient', () => { expect(client.requestQuery).toHaveBeenCalledTimes(1) }) - it('should persist virtual document when no meta.rev', async () => { + it('should persist virtual document when no meta.rev nor _rev', async () => { jest.spyOn(client, 'requestQuery') requestHandler.mockResolvedValue({ data: { @@ -1411,7 +1411,37 @@ describe('CozyClient', () => { ) }) - it('should persist array of virtual documents when no meta.rev', async () => { + it('should not persist virtual document when meta.rev', async () => { + jest.spyOn(client, 'requestQuery') + requestHandler.mockResolvedValue({ + data: { + _id: 'some_id', + meta: { + rev: 'SOME_REV' + } + } + }) + + await client.query(query, { as: 'allTodos' }) + + expect(persistHandler).not.toHaveBeenCalled() + }) + + it('should not persist virtual document when _rev', async () => { + jest.spyOn(client, 'requestQuery') + requestHandler.mockResolvedValue({ + data: { + _id: 'some_id', + _rev: 'SOME_REV' + } + }) + + await client.query(query, { as: 'allTodos' }) + + expect(persistHandler).not.toHaveBeenCalled() + }) + + it('should persist array of virtual documents when no meta.rev nor _rev', async () => { jest.spyOn(client, 'requestQuery') requestHandler.mockResolvedValue({ data: [