Skip to content

Commit

Permalink
Added merging in Config with scoped set
Browse files Browse the repository at this point in the history
  • Loading branch information
framp committed Mar 27, 2016
1 parent 94b10de commit 6b9fd16
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Routers/GlobalConfigRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ export class GlobalConfigRouter extends PromiseRouter {
}

updateGlobalConfig(req) {
const params = req.body.params;
const update = {};
Object.keys(params).forEach((key) => {
if(params[key] && params[key].__op && params[key].__op === "Delete") {
if (!update.$unset) update.$unset = {};
update.$unset["params." + key] = "";
} else {
if (!update.$set) update.$set = {};
update.$set["params." + key] = params[key];
}
});
return req.config.database.adaptiveCollection('_GlobalConfig')
.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 } }));
})
.then(coll => coll.upsertOne({ _id: 1 }, update))
.then(() => ({ response: { result: true } }));
}

mountRoutes() {
Expand Down

0 comments on commit 6b9fd16

Please sign in to comment.