3 |
import KeyValuePair from "../../types/KeyValuePair"; |
import KeyValuePair from "../../types/KeyValuePair"; |
4 |
import Controller from "../Controller"; |
import Controller from "../Controller"; |
5 |
import RequireAuth from "../middleware/RequireAuth"; |
import RequireAuth from "../middleware/RequireAuth"; |
6 |
|
import ValidatorError from "../middleware/ValidatorError"; |
7 |
import Request from "../Request"; |
import Request from "../Request"; |
8 |
|
|
9 |
export default class ConfigController extends Controller { |
export default class ConfigController extends Controller { |
10 |
globalMiddleware(): Function[] { |
globalMiddleware(): Function[] { |
11 |
return [RequireAuth]; |
return [RequireAuth, ValidatorError]; |
12 |
} |
} |
13 |
|
|
14 |
middleware(): KeyValuePair<Function[]> { |
middleware(): KeyValuePair<Function[]> { |
39 |
const currentConfigDotObject = dot(this.client.config.props[id]); |
const currentConfigDotObject = dot(this.client.config.props[id]); |
40 |
const newConfigDotObject = {...currentConfigDotObject}; |
const newConfigDotObject = {...currentConfigDotObject}; |
41 |
|
|
42 |
console.log("Input: ", currentConfigDotObject); |
console.log("Input: ", config); |
43 |
|
|
44 |
for (const configKey in config) { |
for (const configKey in config) { |
45 |
if (!(configKey in currentConfigDotObject)) { |
if (!(configKey in currentConfigDotObject)) { |
46 |
return { error: `The key '${configKey}' is not allowed` }; |
return { error: `The key '${configKey}' is not allowed` }; |
47 |
} |
} |
48 |
|
|
49 |
if (typeof config[configKey] !== typeof currentConfigDotObject[configKey] || (config[configKey] !== currentConfigDotObject[configKey])) { |
if (typeof config[configKey] !== typeof currentConfigDotObject[configKey] || (config[configKey] !== null && currentConfigDotObject[configKey] === null) || (config[configKey] === null && currentConfigDotObject[configKey] !== null)) { |
50 |
return { error: `The key '${configKey}' has incompatible value type '${config[configKey] === null ? 'null' : typeof config[configKey]}'` }; |
return { error: `The key '${configKey}' has incompatible value type '${config[configKey] === null ? 'null' : typeof config[configKey]}'` }; |
51 |
} |
} |
52 |
|
|
53 |
newConfigDotObject[configKey] = config[configKey]; |
newConfigDotObject[configKey] = config[configKey]; |
54 |
|
console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]); |
55 |
} |
} |
56 |
|
|
57 |
console.log("Output: ", newConfigDotObject); |
console.log("Output: ", newConfigDotObject); |
58 |
this.client.config.props[id] = object(newConfigDotObject); |
|
59 |
|
this.client.config.props[id] = object({...newConfigDotObject}); |
60 |
|
this.client.config.write(); |
61 |
|
|
62 |
return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject }; |
return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject }; |
63 |
} |
} |
64 |
} |
} |