Skip to content

Commit

Permalink
fix: Document methods work with both clients
Browse files Browse the repository at this point in the history
  The `Document` methods make different internal calls depending on the
  client being used (i.e. `cozy-client` or `cozy-client-js`).
  However, the `cozy-client` calls' responses don't have the same
  signature as the `cozy-client-js` ones and were not transformed
  leading to errors in (at least) the `BankingReconciliator` when trying
  to match transactions with their account.

  We now make sure that the `cozy-client` calls returning an object with
  a `data` attribute are transformed so that the `Document` methods
  always return the content of that attribute (`cozy-client-js` already
  does that).
  • Loading branch information
taratatach committed Mar 21, 2023
1 parent 0111fa4 commit 5eba284
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
43 changes: 29 additions & 14 deletions packages/cozy-doctypes/src/Document.js
Expand Up @@ -281,7 +281,7 @@ class Document {
const update = omit(attributes, userAttributes)
const updatedDoc = this.applyUpdateIfDifferent(doc, update)
if (updatedDoc !== doc) {
return this.cozyClient.save(updatedDoc)
return this.save(updatedDoc)
} else {
return updatedDoc
}
Expand All @@ -303,10 +303,7 @@ class Document {
}

if (results.length === 0) {
return this.cozyClient.data.create(
this.doctype,
this.addCozyMetadata(attributes)
)
return this.create(this.addCozyMetadata(attributes))
} else {
results = sortBy(results, newestDocumentComparisonFunc)
if (results.length > 1) {
Expand All @@ -317,33 +314,51 @@ class Document {
const update = omit(attributes, userAttributes)
const updatedDoc = this.applyUpdateIfDifferent(doc, update)
if (updatedDoc !== doc) {
return this.cozyClient.data.updateAttributes(
this.doctype,
updatedDoc._id,
updatedDoc
)
return this.save(updatedDoc)
} else {
return doc
}
}
}

static create(attributes) {
static async create(attributes) {
if (this.usesCozyClient()) {
return this.createViaNewClient(attributes)
}

return this.createViaOldClient(attributes)
}

static createViaNewClient(attributes) {
return this.cozyClient.create(this.doctype, attributes)
static async createViaNewClient(attributes) {
const { data } = await this.cozyClient.create(this.doctype, attributes)
return data
}

static createViaOldClient(attributes) {
static async createViaOldClient(attributes) {
return this.cozyClient.data.create(this.doctype, attributes)
}

static async save(attributes) {
if (this.usesCozyClient()) {
return this.saveViaNewClient(attributes)
}

return this.saveViaOldClient(attributes)
}

static async saveViaNewClient(attributes) {
const { data } = await this.cozyClient.save(attributes)
return data
}

static async saveViaOldClient(attributes) {
return this.cozyClient.data.updateAttributes(
this.doctype,
attributes._id,
attributes
)
}

/**
* Save many documents concurrently
*/
Expand Down
12 changes: 8 additions & 4 deletions packages/cozy-doctypes/src/Document.spec.js
@@ -1,7 +1,9 @@
const MockDate = require('mockdate')

const logger = require('cozy-logger')

const Document = require('./Document')
const { cozyClientJS, cozyClient } = require('./testUtils')
const logger = require('cozy-logger')

class Simpson extends Document {}
Simpson.doctype = 'io.cozy.simpsons'
Expand Down Expand Up @@ -448,7 +450,7 @@ describe('Document used with CozyClient', () => {
cozyMetadata: { updatedAt: new Date('2019-11-20') }
}
])
jest.spyOn(cozyClient, 'save').mockReturnValueOnce()
jest.spyOn(cozyClient, 'save').mockReturnValueOnce({ data: {} })
jest.spyOn(Simpson, 'deleteAll').mockReturnValueOnce()
})

Expand Down Expand Up @@ -507,7 +509,7 @@ describe('Document used with CozyClient', () => {

it('should update the document if it already exists', async () => {
jest.spyOn(Simpson, 'queryAll').mockReturnValueOnce([{ name: 'Marge' }])
jest.spyOn(cozyClient, 'save').mockReturnValueOnce()
jest.spyOn(cozyClient, 'save').mockReturnValueOnce({ data: {} })

await Simpson.createOrUpdate({ name: 'Marge', son: 'Bart' })

Expand All @@ -523,7 +525,9 @@ describe('Document used with CozyClient', () => {
})

it('should create the document with the given attributes', async () => {
jest.spyOn(cozyClient, 'create').mockImplementation(() => {})
jest.spyOn(cozyClient, 'create').mockImplementation(() => {
return { data: {} }
})

const marge = { name: 'Marge' }
await Simpson.create(marge)
Expand Down

0 comments on commit 5eba284

Please sign in to comment.