From 94b10de7b85b258521386a87fe7b06a7ef5ec693 Mon Sep 17 00:00:00 2001 From: Federico Rampazzo Date: Sun, 27 Mar 2016 00:24:38 +0000 Subject: [PATCH] Fixed config upsert implementation to handle nested object and __op:Delete --- src/Routers/GlobalConfigRouter.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Routers/GlobalConfigRouter.js b/src/Routers/GlobalConfigRouter.js index 156cecf6..5d375e3f 100644 --- a/src/Routers/GlobalConfigRouter.js +++ b/src/Routers/GlobalConfigRouter.js @@ -19,8 +19,19 @@ export class GlobalConfigRouter extends PromiseRouter { updateGlobalConfig(req) { return req.config.database.adaptiveCollection('_GlobalConfig') - .then(coll => coll.upsertOne({ _id: 1 }, { $set: req.body })) - .then(() => ({ response: { result: true } })); + .then(coll => coll.find({ '_id': 1 }, { limit: 1 })) + .then(results => { + const previousConfig = results && results[0] && results[0].params || {}; + const newConfig = Object.assign({}, previousConfig, req.body.params); + for (var key in newConfig) { + if (newConfig[key] && newConfig[key].__op && newConfig[key].__op === "Delete") { + delete newConfig[key]; + } + } + return req.config.database.adaptiveCollection('_GlobalConfig') + .then(coll => coll.upsertOne({ _id: 1 }, { $set: { params: newConfig } })) + .then(() => ({ response: { result: true } })); + }) } mountRoutes() {