Skip to content

Commit

Permalink
removing TO.BE.REMOVED in deep objects in rest-utils.js fixed #488 #487
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgirard committed Nov 21, 2017
1 parent 90788b0 commit ac4774a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions server/lib/rest-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ const replaceModel = (Model, save, getPermissions) => {
// Apply diff to the original object
let updated = doc.toObject()
const diff = req.body.diff
let toBeRemoved = []

diff.forEach(operation => {
debug('Applying diff operation', Model.modelName, operation)

if (operation.type === 'update') {
if (!('value' in operation) || operation.value === null || operation.value === '') {
setIn(updated, operation.path, 'TO.BE.REMOVED')
toBeRemoved.push(operation.path)
}
else {
setIn(updated, operation.path, operation.value)
Expand All @@ -218,10 +220,16 @@ const replaceModel = (Model, save, getPermissions) => {
getIn(updated, operation.path).unshift(removeEmptyFields(operation.value))
}
})

debug('updated', updated)
toBeRemoved.forEach(pathToBeRemoved => {
debug(pathToBeRemoved, updated[pathToBeRemoved])
if (getIn(updated, pathToBeRemoved)=== 'TO.BE.REMOVED')
setIn(updated, pathToBeRemoved, undefined)
})
for (const f in updated) {
if (updated[f] === 'TO.BE.REMOVED') doc.set(f, undefined, {strict: false} )
else doc[f] = updated[f]
//if (updated[f] === 'TO.BE.REMOVED') doc.set(f, undefined, {strict: false} )
//else
doc[f] = updated[f]
}

// Delete
Expand Down

0 comments on commit ac4774a

Please sign in to comment.