From 9e773f6917ce74d96d215da0166c66a0bbce031f Mon Sep 17 00:00:00 2001 From: Vishnu Sreekumaran Nair <200557136@student.georgianc.on.ca> Date: Sat, 15 Mar 2025 00:21:37 -0400 Subject: [PATCH] format notifications to return object and added to validation list --- .../infrastructureMonitorsSlice.js | 8 ++-- src/Pages/Infrastructure/Create/index.jsx | 41 ++++++++++--------- src/Validation/validation.js | 6 +++ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/Features/InfrastructureMonitors/infrastructureMonitorsSlice.js b/src/Features/InfrastructureMonitors/infrastructureMonitorsSlice.js index af453a7ee..15d9c05cd 100644 --- a/src/Features/InfrastructureMonitors/infrastructureMonitorsSlice.js +++ b/src/Features/InfrastructureMonitors/infrastructureMonitorsSlice.js @@ -96,9 +96,8 @@ export const getInfrastructureMonitorsByTeamId = createAsyncThunk( export const updateInfrastructureMonitor = createAsyncThunk( "infrastructureMonitors/updateMonitor", - async (data, thunkApi) => { + async ({ monitorId, monitor }, thunkApi) => { try { - const { monitor } = data; const updatedFields = { name: monitor.name, description: monitor.description, @@ -107,8 +106,9 @@ export const updateInfrastructureMonitor = createAsyncThunk( threshold: monitor.threshold, }; const res = await networkService.updateMonitor({ - monitorId: monitor._id, - updatedFields: updatedFields, + monitorId, + monitor, + updatedFields, }); return res.data; } catch (error) { diff --git a/src/Pages/Infrastructure/Create/index.jsx b/src/Pages/Infrastructure/Create/index.jsx index fa228828a..c291b04b5 100644 --- a/src/Pages/Infrastructure/Create/index.jsx +++ b/src/Pages/Infrastructure/Create/index.jsx @@ -70,6 +70,7 @@ const CreateInfrastructureMonitor = () => { url: "", name: "", notifications: [], + notify_email: false, interval: 0.25, cpu: false, usage_cpu: "", @@ -89,7 +90,8 @@ const CreateInfrastructureMonitor = () => { setInfrastructureMonitor({ url: monitor.url.replace(/^https?:\/\//, ""), 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, cpu: monitor.cpu || false, usage_cpu: monitor.usage_cpu || "", @@ -108,6 +110,14 @@ const CreateInfrastructureMonitor = () => { const handleCreateInfrastructureMonitor = async (event) => { 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 let form = { url: `http${https ? "s" : ""}://` + infrastructureMonitor.url, @@ -133,6 +143,7 @@ const CreateInfrastructureMonitor = () => { ? { usage_temperature: infrastructureMonitor.usage_temperature } : {}), secret: infrastructureMonitor.secret, + notifications: formattedNotifications, }; const { error } = infrastructureMonitorValidation.validate(form, { @@ -220,30 +231,24 @@ const CreateInfrastructureMonitor = () => { }; const handleNotifications = (event, type) => { - const { value } = event.target; + const { value, checked } = event.target; let notifications = [...infrastructureMonitor.notifications]; - const notificationExists = notifications.some((notification) => { - if (notification.type === type && notification.address === value) { - return true; + + if (checked) { + 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 { - notifications.push({ type, address: value }); + notifications = notifications.filter((n) => !(n.type === type && n.address === value)); } - + setInfrastructureMonitor((prev) => ({ ...prev, notifications, + notify_email: checked, })); }; + return ( @@ -363,9 +368,7 @@ const CreateInfrastructureMonitor = () => { notification.type === "email" - )} + isChecked={infrastructureMonitor.notify_email} value={user?.email} onChange={(event) => handleNotifications(event, "email")} /> diff --git a/src/Validation/validation.js b/src/Validation/validation.js index ed0aaf6b8..6e8ff76cb 100644 --- a/src/Validation/validation.js +++ b/src/Validation/validation.js @@ -349,6 +349,12 @@ const infrastructureMonitorValidation = joi.object({ "number.base": "Frequency must be a number.", "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 {