From ac4774aa4956a1dbfb80729fc60bb2495d59fd54 Mon Sep 17 00:00:00 2001 From: Paul Girard Date: Tue, 21 Nov 2017 16:00:23 +0100 Subject: [PATCH] removing TO.BE.REMOVED in deep objects in rest-utils.js fixed #488 #487 --- server/lib/rest-utils.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/lib/rest-utils.js b/server/lib/rest-utils.js index 993b7126..8afbf63f 100644 --- a/server/lib/rest-utils.js +++ b/server/lib/rest-utils.js @@ -190,6 +190,7 @@ 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) @@ -197,6 +198,7 @@ const replaceModel = (Model, save, getPermissions) => { 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) @@ -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