aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/service/configPOST.js
blob: 9129950bb4069830463ba44bf8afdbb9b8ed88b2 (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
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.writeConfigToDb(value);

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

module.exports = configGET;