2016-02-27 10:51:12 -05:00
|
|
|
// global_config.js
|
|
|
|
|
|
|
|
|
|
import PromiseRouter from '../PromiseRouter';
|
2016-03-01 20:30:29 -08:00
|
|
|
import * as middleware from "../middlewares";
|
2016-02-27 10:51:12 -05:00
|
|
|
|
|
|
|
|
export class GlobalConfigRouter extends PromiseRouter {
|
|
|
|
|
getGlobalConfig(req) {
|
2016-03-07 14:06:46 -08:00
|
|
|
return req.config.database.adaptiveCollection('_GlobalConfig')
|
|
|
|
|
.then(coll => coll.find({ '_id': 1 }, { limit: 1 }))
|
|
|
|
|
.then(results => {
|
|
|
|
|
if (results.length != 1) {
|
|
|
|
|
// If there is no config in the database - return empty config.
|
|
|
|
|
return { response: { params: {} } };
|
2016-02-27 10:51:12 -05:00
|
|
|
}
|
2016-03-07 14:06:46 -08:00
|
|
|
let globalConfig = results[0];
|
|
|
|
|
return { response: { params: globalConfig.params } };
|
|
|
|
|
});
|
2016-02-27 10:51:12 -05:00
|
|
|
}
|
2016-03-07 14:06:46 -08:00
|
|
|
|
2016-02-27 10:51:12 -05:00
|
|
|
updateGlobalConfig(req) {
|
2016-03-07 14:06:46 -08:00
|
|
|
return req.config.database.adaptiveCollection('_GlobalConfig')
|
2016-03-27 00:24:38 +00:00
|
|
|
.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 } }));
|
|
|
|
|
})
|
2016-02-27 10:51:12 -05:00
|
|
|
}
|
2016-03-07 14:06:46 -08:00
|
|
|
|
2016-02-27 10:51:12 -05:00
|
|
|
mountRoutes() {
|
|
|
|
|
this.route('GET', '/config', req => { return this.getGlobalConfig(req) });
|
2016-03-01 20:30:29 -08:00
|
|
|
this.route('PUT', '/config', middleware.promiseEnforceMasterKeyAccess, req => { return this.updateGlobalConfig(req) });
|
2016-02-27 10:51:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default GlobalConfigRouter;
|