aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/service/configPOST.js
blob: 68af46725d6703ddf797f5870ed982ea14fb2455 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');

const { schema } = require('../../structures/Setting');

const joiOptions = {
	abortEarly: false, // include all errors
	allowUnknown: true, // ignore unknown props
	stripUnknown: true // remove unknown props
};

class configGET extends Route {
	constructor() {
		super('/service/config', 'post', { adminOnly: true });
	}

	async run(req, res) {
		const { settings } = req.body;
		const { error, value } = schema.validate(settings, joiOptions);
		if (error) {
			return res.status(400).json({
				errors: error.details.reduce((acc, v) => {
					for (const p of v.path) {
						acc[p] = (acc[p] || []).concat(v.message);
					}
					return acc;
				}, {})
			});
		}

		await Util.wipeConfigDb();

		const keys = Object.keys(value);
		for await (const item of keys) {
			Util.writeConfigToDb({
				key: item,
				value: value[item]
			});
		}

		return res.status(200).json({ value });
	}
}

module.exports = configGET;