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[]> { |
36 |
public async update(request: Request) { |
public async update(request: Request) { |
37 |
const { id } = request.params; |
const { id } = request.params; |
38 |
const { config } = request.body; |
const { config } = request.body; |
39 |
|
|
40 |
|
console.log(config); |
41 |
|
|
42 |
const currentConfigDotObject = dot(this.client.config.props[id]); |
const currentConfigDotObject = dot(this.client.config.props[id]); |
43 |
const newConfigDotObject = {...currentConfigDotObject}; |
const newConfigDotObject = {...currentConfigDotObject}; |
44 |
|
|
45 |
console.log("Input: ", currentConfigDotObject); |
console.log("Input: ", config); |
46 |
|
|
47 |
for (const configKey in config) { |
for (const configKey in config) { |
48 |
if (!(configKey in currentConfigDotObject)) { |
if (typeof currentConfigDotObject[configKey] === 'undefined') { |
49 |
return { error: `The key '${configKey}' is not allowed` }; |
return this.response({ error: `The key '${configKey}' is not allowed` }, 422); |
50 |
} |
} |
51 |
|
|
52 |
if (typeof config[configKey] !== typeof currentConfigDotObject[configKey] || (config[configKey] !== currentConfigDotObject[configKey])) { |
if (config[configKey] !== null && config[configKey] !== null && typeof config[configKey] !== typeof currentConfigDotObject[configKey]) { |
53 |
return { error: `The key '${configKey}' has incompatible value type '${config[configKey] === null ? 'null' : typeof config[configKey]}'` }; |
console.log(typeof config[configKey], typeof currentConfigDotObject[configKey]); |
54 |
|
|
55 |
|
if (typeof currentConfigDotObject[configKey] === 'number' && typeof config[configKey] === 'string') { |
56 |
|
const int = parseInt(config[configKey]); |
57 |
|
|
58 |
|
if (int !== NaN) { |
59 |
|
newConfigDotObject[configKey] = int; |
60 |
|
console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]); |
61 |
|
continue; |
62 |
|
} |
63 |
|
} |
64 |
|
|
65 |
|
return this.response({ error: `The key '${configKey}' has incompatible value type '${config[configKey] === null ? 'null' : typeof config[configKey]}'` }, 422); |
66 |
} |
} |
67 |
|
|
68 |
newConfigDotObject[configKey] = config[configKey]; |
newConfigDotObject[configKey] = config[configKey]; |
69 |
|
console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]); |
70 |
} |
} |
71 |
|
|
72 |
console.log("Output: ", newConfigDotObject); |
console.log("Output: ", newConfigDotObject); |
73 |
this.client.config.props[id] = object(newConfigDotObject); |
|
74 |
|
this.client.config.props[id] = object({...newConfigDotObject}); |
75 |
|
this.client.config.write(); |
76 |
|
|
77 |
return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject }; |
return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject }; |
78 |
} |
} |
79 |
} |
} |