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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 326 by rakin, Mon Jul 29 17:29:33 2024 UTC revision 368 by rakin, Mon Jul 29 17:29:49 2024 UTC
# Line 1  Line 1 
1  import { Request } from "express";  import { dot, object } from "dot-object";
2    import { body } from "express-validator";
3    import KeyValuePair from "../../types/KeyValuePair";
4  import Controller from "../Controller";  import Controller from "../Controller";
5    import RequireAuth from "../middleware/RequireAuth";
6    import ValidatorError from "../middleware/ValidatorError";
7    import Request from "../Request";
8    
9  export default class ConfigController extends Controller {  export default class ConfigController extends Controller {
10        globalMiddleware(): Function[] {
11            return [RequireAuth, ValidatorError];
12        }
13    
14        middleware(): KeyValuePair<Function[]> {
15            return {
16                update: [
17                    body(["config"]).isObject()
18                ]
19            };
20        }
21    
22      public async index(request: Request) {      public async index(request: Request) {
23                        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      }      }
35    
36      public async update(request: Request) {      public async update(request: Request) {
37                    const { id } = request.params;
38            const { config } = request.body;
39    
40            console.log(config);        
41    
42            const currentConfigDotObject = dot(this.client.config.props[id]);
43            const newConfigDotObject = {...currentConfigDotObject};
44    
45            console.log("Input: ", config);
46    
47            for (const configKey in config) {
48                if (typeof currentConfigDotObject[configKey] === 'undefined') {
49                    return this.response({ error: `The key '${configKey}' is not allowed` }, 422);
50                }
51    
52                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                }
67    
68                newConfigDotObject[configKey] = config[configKey];
69                console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]);
70            }
71    
72            console.log("Output: ", newConfigDotObject);
73    
74            this.client.config.props[id] = object({...newConfigDotObject});
75            this.client.config.write();
76    
77            return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject };
78      }      }
79  }  }

Legend:
Removed from v.326  
changed lines
  Added in v.368

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26