/[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 349 by rakin, Mon Jul 29 17:29:43 2024 UTC revision 352 by rakin, Mon Jul 29 17:29:44 2024 UTC
# Line 1  Line 1 
1    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";  import RequireAuth from "../middleware/RequireAuth";
6  import Request from "../Request";  import Request from "../Request";
# Line 7  export default class ConfigController ex Line 10  export default class ConfigController ex
10          return [RequireAuth];          return [RequireAuth];
11      }      }
12    
13        middleware(): KeyValuePair<Function[]> {
14            return {
15                update: [
16                    body(["config"]).isObject()
17                ]
18            };
19        }
20    
21      public async index(request: Request) {      public async index(request: Request) {
22          const { id } = request.params;          const { id } = request.params;
23    
# Line 22  export default class ConfigController ex Line 33  export default class ConfigController ex
33      }      }
34    
35      public async update(request: Request) {      public async update(request: Request) {
36                    const { id } = request.params;
37            const { config } = request.body;
38            const currentConfigDotObject = dot(this.client.config.props[id]);
39            const newConfigDotObject = {...currentConfigDotObject};
40    
41            console.log("Input: ", currentConfigDotObject);
42    
43            for (const configKey in config) {
44                if (!(configKey in currentConfigDotObject)) {
45                    return { error: `The key '${configKey}' is not allowed` };
46                }
47    
48                if (typeof config[configKey] !== typeof currentConfigDotObject[configKey] || (config[configKey] !== currentConfigDotObject[configKey])) {
49                    return { error: `The key '${configKey}' has incompatible value type '${config[configKey] === null ? 'null' : typeof config[configKey]}'` };
50                }
51    
52                newConfigDotObject[configKey] = config[configKey];
53            }
54    
55            console.log("Output: ", newConfigDotObject);
56            this.client.config.props[id] = object(newConfigDotObject);
57            return { message: "Configuration updated", previous: currentConfigDotObject, new: newConfigDotObject };
58      }      }
59  }  }

Legend:
Removed from v.349  
changed lines
  Added in v.352

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26