Skip to content

Commit

Permalink
fix: Persist io.cozy.files.shortcuts even if they have a meta.rev
Browse files Browse the repository at this point in the history
`io.cozy.files.shortcuts` doctype does not exist in Couch database

This doctype is created by cozy-stack from `io.cozy.files` documents

This has two impact:
- First, we cannot call `PouchDB.replicate` on this doctype as it does
  not actually exist
- Second, `io.cozy.files` documents have a `meta.rev` attributes that
  is included into the interpolated `io.cozy.files.shortcuts` one. So
  the virtual documents persistance mechanism is not triggered

So with actual implementation we cannot persist
`io.cozy.files.shortcuts` for offline usage

To make it possible, we want to enforce virtual documents persistance
for this doctype even if it has a `meta.rev` attribute
  • Loading branch information
Ldoppea committed Jul 22, 2024
1 parent 95a4010 commit 11c2d88
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,24 +1106,31 @@ client.query(Q('io.cozy.bills'))`)
* @returns {Promise<void>}
*/
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)
}
}
}
Expand All @@ -1133,12 +1140,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<void>}
*/
async persistVirtualDocument(document) {
async persistVirtualDocument(document, enforce) {
if (
document &&
!document.meta?.rev &&
((document && !document.meta?.rev) || enforce) &&
!document.cozyLocalOnly &&
!document.cozyFromPouch
) {
Expand Down
19 changes: 19 additions & 0 deletions packages/cozy-client/src/CozyClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,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(() => {
Expand Down

0 comments on commit 11c2d88

Please sign in to comment.