format notifications to return object and added to validation list

This commit is contained in:
Vishnu Sreekumaran Nair committed 2025-03-15 00:21:37 -04:00
1 parent a320967949
commit 9e773f6917
3 files changed
+32 -23

No files matched your search

@@ -96,9 +96,8 @@ export const getInfrastructureMonitorsByTeamId = createAsyncThunk(
export const updateInfrastructureMonitor = createAsyncThunk( export const updateInfrastructureMonitor = createAsyncThunk(
"infrastructureMonitors/updateMonitor", "infrastructureMonitors/updateMonitor",
async (data, thunkApi) => { async ({ monitorId, monitor }, thunkApi) => {
try { try {
const { monitor } = data;
const updatedFields = { const updatedFields = {
name: monitor.name, name: monitor.name,
description: monitor.description, description: monitor.description,
@@ -107,8 +106,9 @@ export const updateInfrastructureMonitor = createAsyncThunk(
threshold: monitor.threshold, threshold: monitor.threshold,
}; };
const res = await networkService.updateMonitor({ const res = await networkService.updateMonitor({
monitorId: monitor._id, monitorId,
updatedFields: updatedFields, monitor,
updatedFields,
}); });
return res.data; return res.data;
} catch (error) { } catch (error) {
+22 -19
View File
@@ -70,6 +70,7 @@ const CreateInfrastructureMonitor = () => {
url: "", url: "",
name: "", name: "",
notifications: [], notifications: [],
notify_email: false,
interval: 0.25, interval: 0.25,
cpu: false, cpu: false,
usage_cpu: "", usage_cpu: "",
@@ -89,7 +90,8 @@ const CreateInfrastructureMonitor = () => {
setInfrastructureMonitor({ setInfrastructureMonitor({
url: monitor.url.replace(/^https?:\/\//, ""), url: monitor.url.replace(/^https?:\/\//, ""),
name: monitor.name || "", name: monitor.name || "",
notifications: monitor.notifications || [], notifications: monitor.notifications?.filter(n => typeof n === "object") || [],
notify_email: (monitor.notifications?.length ?? 0) > 0,
interval: monitor.interval / MS_PER_MINUTE, interval: monitor.interval / MS_PER_MINUTE,
cpu: monitor.cpu || false, cpu: monitor.cpu || false,
usage_cpu: monitor.usage_cpu || "", usage_cpu: monitor.usage_cpu || "",
@@ -108,6 +110,14 @@ const CreateInfrastructureMonitor = () => {
const handleCreateInfrastructureMonitor = async (event) => { const handleCreateInfrastructureMonitor = async (event) => {
event.preventDefault(); event.preventDefault();
const formattedNotifications = infrastructureMonitor.notifications.map((n) =>
typeof n === "string" ? { type: "email", address: n } : n
);
if (infrastructureMonitor.notify_email) {
formattedNotifications.push({ type: "email", address: user.email });
}
// Build the form // Build the form
let form = { let form = {
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url, url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
@@ -133,6 +143,7 @@ const CreateInfrastructureMonitor = () => {
? { usage_temperature: infrastructureMonitor.usage_temperature } ? { usage_temperature: infrastructureMonitor.usage_temperature }
: {}), : {}),
secret: infrastructureMonitor.secret, secret: infrastructureMonitor.secret,
notifications: formattedNotifications,
}; };
const { error } = infrastructureMonitorValidation.validate(form, { const { error } = infrastructureMonitorValidation.validate(form, {
@@ -220,30 +231,24 @@ const CreateInfrastructureMonitor = () => {
}; };
const handleNotifications = (event, type) => { const handleNotifications = (event, type) => {
const { value } = event.target; const { value, checked } = event.target;
let notifications = [...infrastructureMonitor.notifications]; let notifications = [...infrastructureMonitor.notifications];
const notificationExists = notifications.some((notification) => {
if (notification.type === type && notification.address === value) { if (checked) {
return true; if (!notifications.some((n) => n.type === type && n.address === value)) {
notifications.push({ type, address: value });
} }
return false;
});
if (notificationExists) {
notifications = notifications.filter((notification) => {
if (notification.type === type && notification.address === value) {
return false;
}
return true;
});
} else { } else {
notifications.push({ type, address: value }); notifications = notifications.filter((n) => !(n.type === type && n.address === value));
} }
setInfrastructureMonitor((prev) => ({ setInfrastructureMonitor((prev) => ({
...prev, ...prev,
notifications, notifications,
notify_email: checked,
})); }));
}; };
return ( return (
<Box className="create-infrastructure-monitor"> <Box className="create-infrastructure-monitor">
@@ -363,9 +368,7 @@ const CreateInfrastructureMonitor = () => {
<Checkbox <Checkbox
id="notify-email-default" id="notify-email-default"
label={`Notify via email (to ${user.email})`} label={`Notify via email (to ${user.email})`}
isChecked={infrastructureMonitor.notifications.some( isChecked={infrastructureMonitor.notify_email}
(notification) => notification.type === "email"
)}
value={user?.email} value={user?.email}
onChange={(event) => handleNotifications(event, "email")} onChange={(event) => handleNotifications(event, "email")}
/> />
+6
View File
@@ -349,6 +349,12 @@ const infrastructureMonitorValidation = joi.object({
"number.base": "Frequency must be a number.", "number.base": "Frequency must be a number.",
"any.required": "Frequency is required.", "any.required": "Frequency is required.",
}), }),
notifications: joi.array().items(
joi.object({
type: joi.string().valid("email").required(),
address: joi.string().email({ tlds: { allow: false } }).required(),
})
)
}); });
export { export {