2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2017-10-16 07:47:05 +08:00
|
|
|
const sql = require('../../services/sql');
|
2017-11-03 08:48:02 +08:00
|
|
|
const options = require('../../services/options');
|
2017-10-16 07:47:05 +08:00
|
|
|
const auth = require('../../services/auth');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-05 07:57:40 +08:00
|
|
|
// options allowed to be updated directly in settings dialog
|
2017-11-15 11:44:45 +08:00
|
|
|
const ALLOWED_OPTIONS = ['protected_session_timeout', 'history_snapshot_time_interval'];
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/all', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-05 07:57:40 +08:00
|
|
|
const settings = await sql.getMap("SELECT opt_name, opt_value FROM options");
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-05 07:57:40 +08:00
|
|
|
res.send(settings);
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-05 07:57:40 +08:00
|
|
|
const settings = await sql.getMap("SELECT opt_name, opt_value FROM options WHERE opt_name IN ("
|
|
|
|
+ ALLOWED_OPTIONS.map(x => '?').join(",") + ")", ALLOWED_OPTIONS);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-05 07:57:40 +08:00
|
|
|
res.send(settings);
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-10-25 10:58:59 +08:00
|
|
|
const body = req.body;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
if (ALLOWED_OPTIONS.includes(body['name'])) {
|
2017-11-03 08:48:02 +08:00
|
|
|
const optionName = await options.getOption(body['name']);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-12-17 09:48:34 +08:00
|
|
|
await options.setOption(body['name'], body['value'], sourceId);
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
res.send({});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send("not allowed option to set");
|
|
|
|
}
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
module.exports = router;
|