2016-02-27 10:51:12 -05:00
|
|
|
// global_config.js
|
|
|
|
|
|
2016-04-25 20:42:19 -07:00
|
|
|
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-08-20 16:07:48 -04:00
|
|
|
return req.config.database.find('_GlobalConfig', { objectId: "1" }, { limit: 1 }).then((results) => {
|
2016-04-14 19:24:56 -04:00
|
|
|
if (results.length != 1) {
|
|
|
|
|
// If there is no config in the database - return empty config.
|
|
|
|
|
return { response: { params: {} } };
|
|
|
|
|
}
|
2016-12-07 15:17:05 -08:00
|
|
|
const globalConfig = results[0];
|
2016-04-14 19:24:56 -04:00
|
|
|
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-12-07 15:17:05 -08:00
|
|
|
const params = req.body.params;
|
2016-04-14 19:24:56 -04:00
|
|
|
// Transform in dot notation to make sure it works
|
2016-03-27 04:32:58 +01:00
|
|
|
const update = Object.keys(params).reduce((acc, key) => {
|
2016-04-14 19:24:56 -04:00
|
|
|
acc[`params.${key}`] = params[key];
|
2016-03-27 04:32:58 +01:00
|
|
|
return acc;
|
|
|
|
|
}, {});
|
2016-11-24 15:47:41 -05:00
|
|
|
return req.config.database.update('_GlobalConfig', {objectId: "1"}, update, {upsert: true}).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;
|