Skip to content

Commit

Permalink
feat: Allow Document.bulkSave to force documents update
Browse files Browse the repository at this point in the history
Even if Document.checkAttributes are the same.
We can do this with:

```javascript
Document.bulkSave({...}, {
  forceUpdate: true
})
```

This is needed in special cases in banking konnectors, to also update
categorized transactions even if they already have been saved once.
  • Loading branch information
doubleface authored and doubleface committed Apr 15, 2024
1 parent 645403a commit 2a42ce8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
31 changes: 23 additions & 8 deletions packages/cozy-doctypes/src/Document.js
Expand Up @@ -201,20 +201,28 @@ class Document {

/**
* Update a document with `update` attributes. If the
* `update` does not concern "important" attributes, the original
* `update` does not concern deduplication attributes (checkAttributes)
* or is not forced with forceUpdate option, the original
* document is returned. Otherwise, the update document is
* returned with metadata updated.
*
* @param {object} doc - The document already existing in db
* @param {object} update - The update to apply to the document
* @param {object} options - Options object
* @param {boolean} options.forceUpdate - Should the method force the update even if checkAttributes are identical in db document and updated document
* @returns {object} - The updated document with cozy new metadata when an update has been done
*
* @private
*/
static applyUpdateIfDifferent(doc, update) {
static applyUpdateIfDifferent(doc, update, options) {
// only update if some fields are different
if (
!this.checkAttributes ||
isDifferent(
pick(doc, this.checkAttributes),
pick(update, this.checkAttributes)
)
) ||
options?.forceUpdate
) {
// do not emit a mail for those attribute updates
delete update.dateImport
Expand Down Expand Up @@ -279,8 +287,8 @@ class Document {
}
const doc = results[0]
const update = omit(attributes, userAttributes)
const updatedDoc = this.applyUpdateIfDifferent(doc, update)
if (updatedDoc !== doc) {
const updatedDoc = this.applyUpdateIfDifferent(doc, update, options)
if (options.forceUpdate || updatedDoc !== doc) {
return this.save(updatedDoc)
} else {
return updatedDoc
Expand Down Expand Up @@ -312,8 +320,8 @@ class Document {

const doc = results[0]
const update = omit(attributes, userAttributes)
const updatedDoc = this.applyUpdateIfDifferent(doc, update)
if (updatedDoc !== doc) {
const updatedDoc = this.applyUpdateIfDifferent(doc, update, options)
if (options.forceUpdate || updatedDoc !== doc) {
return this.save(updatedDoc)
} else {
return doc
Expand Down Expand Up @@ -360,7 +368,14 @@ class Document {
}

/**
* Save many documents concurrently
* Save many documents concurrently using the createOrUpdate method
*
* @param {Array<object>} documents - The document to save
* @param {object|number} optionsOrConcurrency - The maximum number of possible concurrent updates OR options object
* @param {boolean} optionsOrConcurrency.forceUpdate - Should the method force the update even if checkAttributes are identical in db document and updated document
* @param {function} [logProgressOrNothing] - Callback with the progress of the save
*
* @returns {Array<object>} - The list of updated documents with cozy new metadata when an update has been done
*/
static bulkSave(documents, optionsOrConcurrency, logProgressOrNothing) {
if (logProgressOrNothing || typeof optionsOrConcurrency !== 'object') {
Expand Down
4 changes: 2 additions & 2 deletions packages/cozy-doctypes/src/Document.spec.js
Expand Up @@ -518,7 +518,7 @@ describe('Document used with CozyClient', () => {
)
})

it('should not update the document if important attributes are the same', async () => {
it('should not update the document if checked attributes are the same', async () => {
class SpecialSimpson extends Simpson {}
SpecialSimpson.checkAttributes = ['name']
jest
Expand All @@ -531,7 +531,7 @@ describe('Document used with CozyClient', () => {
expect(cozyClient.save).not.toHaveBeenCalled()
})

it('should update the document if important attributes are the same but forceUpdate option is true', async () => {
it('should update the document if checked attributes are the same but forceUpdate option is true', async () => {
class SpecialSimpson extends Simpson {}
SpecialSimpson.checkAttributes = ['name']
jest
Expand Down

0 comments on commit 2a42ce8

Please sign in to comment.