/[sudobot]/trunk/src/api/controllers/ConfigController.ts
ViewVC logotype

Annotation of /trunk/src/api/controllers/ConfigController.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 368 - (hide annotations)
Mon Jul 29 17:29:49 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 3091 byte(s)
fix(api): string to number config set error
1 rakin 352 import { dot, object } from "dot-object";
2     import { body } from "express-validator";
3     import KeyValuePair from "../../types/KeyValuePair";
4 rakin 326 import Controller from "../Controller";
5 rakin 349 import RequireAuth from "../middleware/RequireAuth";
6 rakin 353 import ValidatorError from "../middleware/ValidatorError";
7 rakin 349 import Request from "../Request";
8 rakin 326
9     export default class ConfigController extends Controller {
10 rakin 349 globalMiddleware(): Function[] {
11 rakin 353 return [RequireAuth, ValidatorError];
12 rakin 349 }
13    
14 rakin 352 middleware(): KeyValuePair<Function[]> {
15     return {
16     update: [
17     body(["config"]).isObject()
18     ]
19     };
20     }
21    
22 rakin 326 public async index(request: Request) {
23 rakin 349 const { id } = request.params;
24    
25     if (!request.user?.guilds.includes(id)) {
26     return this.response({ error: "You don't have permission to access configuration of this guild." }, 403);
27     }
28    
29     if (id === "global" || !this.client.config.props[id]) {
30     return this.response({ error: "No configuration found for the given guild ID" }, 404);
31     }
32    
33     return this.client.config.props[id];
34 rakin 326 }
35    
36     public async update(request: Request) {
37 rakin 352 const { id } = request.params;
38     const { config } = request.body;
39 rakin 368
40     console.log(config);
41    
42 rakin 352 const currentConfigDotObject = dot(this.client.config.props[id]);
43     const newConfigDotObject = {...currentConfigDotObject};
44    
45 rakin 353 console.log("Input: ", config);
46 rakin 352
47     for (const configKey in config) {
48 rakin 368 if (typeof currentConfigDotObject[configKey] === 'undefined') {
49     return this.response({ error: `The key '${configKey}' is not allowed` }, 422);
50 rakin 352 }
51    
52 rakin 368 if (config[configKey] !== null && config[configKey] !== null && typeof config[configKey] !== typeof currentConfigDotObject[configKey]) {
53     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 rakin 352 }
67    
68     newConfigDotObject[configKey] = config[configKey];
69 rakin 353 console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]);
70 rakin 352 }
71    
72     console.log("Output: ", newConfigDotObject);
73 rakin 353
74     this.client.config.props[id] = object({...newConfigDotObject});
75     this.client.config.write();
76    
77 rakin 352 return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject };
78 rakin 326 }
79     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26