Skip to content

Commit 4fe7dbd

Browse files
authored
Merge pull request #83 from moda20/improvement/categorize-configs-with-a-new-route
2 parents ec760fb + ba5202c commit 4fe7dbd

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

src/api/system/config.controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ObjectifyFlattenedProperties,
66
updateMultiConfig,
77
} from "@config/config.service";
8+
import { categorizeConfig, transposedConfigMap } from "@utils/convictUtils";
89
import { createElysia } from "@utils/createElysia";
910
import qs from "qs";
1011

@@ -18,7 +19,14 @@ export const configController = createElysia({ prefix: "/system/config" })
1819
})
1920
.get("/getConfig", async () => {
2021
const config = await getConfigWithDBEncryptionStatus();
21-
return ObjectifyFlattenedProperties(config);
22+
return {
23+
configArray: ObjectifyFlattenedProperties(config),
24+
categoriesMap: transposedConfigMap,
25+
};
26+
})
27+
.get("/getCategorizedConfig", async () => {
28+
const config = await getConfigWithDBEncryptionStatus();
29+
return categorizeConfig(ObjectifyFlattenedProperties(config));
2230
})
2331
.post(
2432
"/updateConfig",

src/api/websocket/mainSocket.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export default {
103103
}
104104
},
105105
unsubscribeFromTopics(userId: string, topics: string[]) {
106-
this.topicsSubscriptions[userId] = this.topicsSubscriptions[userId].filter(
107-
(e) => !topics.includes(e),
108-
);
106+
this.topicsSubscriptions[userId] =
107+
this.topicsSubscriptions[userId]?.filter((e) => !topics.includes(e)) ??
108+
[];
109109
},
110110
};

src/repositories/configs.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { basePrisma } from "@initialization/index";
2+
import { LogEventNames } from "@typesDef/api/jobs";
23
import encryptionUtils from "@utils/encryptionUtils";
3-
4+
import logger, { eventLog } from "@utils/loggers";
45
export const getAllConfigs = (
56
limit?: number,
67
offset?: number,
@@ -67,6 +68,14 @@ export const saveConfig = async (
6768
key: key,
6869
},
6970
});
71+
if (existingConfig?.is_encrypted !== is_encrypted) {
72+
const sysLog = eventLog(LogEventNames.SysLogEvent);
73+
const message = `config ${key} has changed encryption status from ${existingConfig?.is_encrypted} to ${is_encrypted}`;
74+
sysLog.warn(message, {
75+
eventName: "CONFIG_ENCRYPTION_STATUS_CHANGED",
76+
});
77+
logger.warn(message);
78+
}
7079
const finalValue = is_encrypted
7180
? encryptionUtils.encryptWithMasterKey(value)
7281
: value;
@@ -78,14 +87,15 @@ export const saveConfig = async (
7887
},
7988
data: {
8089
value: finalValue,
90+
is_encrypted: is_encrypted ?? existingConfig.is_encrypted,
8191
appConfigAudit: {
8292
create: {
8393
changedBy: {
8494
connect: {
8595
id: userId,
8696
},
8797
},
88-
newValue: value,
98+
newValue: finalValue,
8999
oldValue: existingConfig.value,
90100
},
91101
},
@@ -108,7 +118,7 @@ export const saveConfig = async (
108118
id: userId,
109119
},
110120
},
111-
newValue: value,
121+
newValue: finalValue,
112122
oldValue: null,
113123
},
114124
},

src/utils/convictUtils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,32 @@ export const toSafeString = (input: any) => {
136136
return input;
137137
}
138138
};
139+
140+
// A human-readable map of config categories, might change to the convict schema if possible
141+
const categoriesMap: any = {
142+
system: ["DB", "baseDB", "env", "appName", "server", "jobs", "swaggerServer"],
143+
logging: ["files"],
144+
notifications: ["notifications", "grafana"],
145+
};
146+
147+
export const transposedConfigMap = Object.keys(categoriesMap).reduce(
148+
(p: any, c: string) => {
149+
categoriesMap[c].forEach((e: any) => {
150+
p[e] = c;
151+
});
152+
return p;
153+
},
154+
{},
155+
);
156+
157+
export const categorizeConfig = (inputConfigObject: any) => {
158+
return Object.keys(inputConfigObject).reduce((p: any, c: string) => {
159+
const categoryName = transposedConfigMap[c] ?? "custom";
160+
if (p[categoryName]) {
161+
p[categoryName][c] = inputConfigObject[c];
162+
} else {
163+
p[categoryName] = { [c]: inputConfigObject[c] };
164+
}
165+
return p;
166+
}, {});
167+
};

0 commit comments

Comments
 (0)