2018-09-06 11:54:04 +02:00
|
|
|
const utils = require('./utils');
|
|
|
|
|
2018-01-28 19:30:14 -05:00
|
|
|
async function getOption(name) {
|
2018-07-21 08:55:24 +02:00
|
|
|
const option = await require('./repository').getOption(name);
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
if (!option) {
|
2019-11-01 23:05:33 +01:00
|
|
|
throw new Error(`Option ${name} doesn't exist`);
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
return option.value;
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2019-11-03 11:43:04 +01:00
|
|
|
async function getOptionInt(name) {
|
|
|
|
const val = await getOption(name);
|
|
|
|
|
|
|
|
const intVal = parseInt(val);
|
|
|
|
|
|
|
|
if (isNaN(intVal)) {
|
|
|
|
throw new Error(`Could not parse "${val}" into integer for option "${name}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return intVal;
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
async function setOption(name, value) {
|
2018-07-21 08:55:24 +02:00
|
|
|
const option = await require('./repository').getOption(name);
|
2018-01-11 22:45:25 -05:00
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
if (!option) {
|
2018-01-28 19:30:14 -05:00
|
|
|
throw new Error(`Option ${name} doesn't exist`);
|
2018-01-11 22:45:25 -05:00
|
|
|
}
|
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
option.value = value;
|
|
|
|
|
|
|
|
await option.save();
|
2018-01-11 22:45:25 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 19:41:54 -04:00
|
|
|
async function createOption(name, value, isSynced) {
|
2018-06-10 15:49:22 -04:00
|
|
|
// to avoid circular dependency, need to find better solution
|
|
|
|
const Option = require('../entities/option');
|
|
|
|
|
2018-05-22 00:22:43 -04:00
|
|
|
await new Option({
|
2018-01-28 19:30:14 -05:00
|
|
|
name: name,
|
|
|
|
value: value,
|
2018-05-22 00:22:43 -04:00
|
|
|
isSynced: isSynced
|
|
|
|
}).save();
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2018-09-06 11:54:04 +02:00
|
|
|
async function getOptions(allowedOptions) {
|
|
|
|
let options = await require('./repository').getEntities("SELECT * FROM options ORDER BY name");
|
|
|
|
|
|
|
|
if (allowedOptions) {
|
|
|
|
options = options.filter(opt => allowedOptions.includes(opt.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getOptionsMap(allowedOptions) {
|
|
|
|
const options = await getOptions(allowedOptions);
|
|
|
|
|
|
|
|
return utils.toObject(options, opt => [opt.name, opt.value]);
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:48:02 -04:00
|
|
|
module.exports = {
|
|
|
|
getOption,
|
2019-11-03 11:43:04 +01:00
|
|
|
getOptionInt,
|
2017-11-02 20:48:02 -04:00
|
|
|
setOption,
|
2018-09-06 11:54:04 +02:00
|
|
|
createOption,
|
|
|
|
getOptions,
|
|
|
|
getOptionsMap
|
2017-11-02 20:48:02 -04:00
|
|
|
};
|