From 30ef28170c70da662a37769e727271e97f13c7be Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Fri, 2 May 2025 02:54:39 +0530 Subject: [PATCH] Removed all return error ro throw new err where this will be send to middleware. --- client/src/Hooks/useBulkMonitors.js | 2 +- server/controllers/monitorController.js | 141 ++++++++++++------------ 2 files changed, 70 insertions(+), 73 deletions(-) diff --git a/client/src/Hooks/useBulkMonitors.js b/client/src/Hooks/useBulkMonitors.js index 3c4f3c00e..0caa58b41 100644 --- a/client/src/Hooks/useBulkMonitors.js +++ b/client/src/Hooks/useBulkMonitors.js @@ -16,7 +16,7 @@ export const useBulkMonitors = () => { const response = await networkService.createBulkMonitors(formData); return [true, response.data, null]; // [success, data, error] } catch (err) { - const errorMessage = err?.response?.data?.errors?.[0]?.message || err.message; + const errorMessage = err?.response?.data?.msg || err.message; return [false, null, errorMessage]; } finally { setIsLoading(false); diff --git a/server/controllers/monitorController.js b/server/controllers/monitorController.js index fc9482e59..06c2d583f 100755 --- a/server/controllers/monitorController.js +++ b/server/controllers/monitorController.js @@ -256,110 +256,107 @@ class MonitorController { createBulkMonitors = async (req, res, next) => { try { const { parse } = pkg; - + + // validate the file if (!req.file) { - return res.status(400).json({ - errors: [ - { message: "No file uploaded" } - ] - }); + throw new Error("No file uploaded"); } - + + // Check if the file is a CSV + if (!req.file.mimetype.includes("csv")) { + throw new Error("File is not a CSV"); + } + + // Validate if the file is empty + if (req.file.size === 0) { + throw new Error("File is empty"); + } + const { userId, teamId } = req.body; - + if (!userId || !teamId) { - return res.status(400).json({ - errors: [ - { message: "Missing userId or teamId in form data" } - ] - }); + throw new Error("Missing userId or teamId in form data"); } - + // Get file buffer from memory and convert to string const fileData = req.file.buffer.toString("utf-8"); - console.log("File data:", fileData); - + // Parse the CSV data parse(fileData, { header: true, skipEmptyLines: true, transform: (value, header) => { if (value === "") return undefined; // Empty fields become undefined - + // Handle 'port' and 'interval' fields, check if they're valid numbers if (["port", "interval"].includes(header)) { const num = parseInt(value, 10); if (isNaN(num)) { - return res.status(400).json({ - errors: [ - { message: `${header} should be a valid number, got: ${value}` } - ] - }); // Return error immediately with this structure + throw new Error(`${header} should be a valid number, got: ${value}`); } return num; } - + return value; }, complete: async ({ data, errors }) => { - if (errors.length > 0) { - return res.status(400).json({ - errors: [ - { message: "Error parsing CSV" } - ] - }); - } - - const enrichedData = data.map((monitor) => ({ - userId, - teamId, - ...monitor, - description: monitor.description || monitor.name || monitor.url, - name: monitor.name || monitor.url, - type: monitor.type || "http", - })); - try { - // Run Joi validation + if (errors.length > 0) { + throw new Error("Error parsing CSV"); + } + + if (!data || data.length === 0) { + throw new Error("CSV file contains no data rows"); + } + + const enrichedData = data.map((monitor) => ({ + userId, + teamId, + ...monitor, + description: monitor.description || monitor.name || monitor.url, + name: monitor.name || monitor.url, + type: monitor.type || "http", + })); + await createMonitorsBodyValidation.validateAsync(enrichedData); + + try { + const monitors = await this.db.createBulkMonitors(enrichedData); + + await Promise.all( + monitors.map(async (monitor, index) => { + const notifications = enrichedData[index].notifications; + + if (notifications?.length) { + monitor.notifications = await Promise.all( + notifications.map(async (notification) => { + notification.monitorId = monitor._id; + return await this.db.createNotification(notification); + }) + ); + await monitor.save(); + } + + this.jobQueue.addJob(monitor._id, monitor); + }) + ); + + return res.success({ + msg: this.stringService.bulkMonitorsCreate, + data: monitors, + }); + } catch (error) { + next(handleError(error, SERVICE_NAME, "createBulkMonitors")); + } } catch (error) { - return next(handleValidationError(error, SERVICE_NAME)); - } - - try { - const monitors = await this.db.createBulkMonitors(enrichedData); - - await Promise.all( - monitors.map(async (monitor, index) => { - const notifications = enrichedData[index].notifications; - - if (notifications?.length) { - monitor.notifications = await Promise.all( - notifications.map(async (notification) => { - notification.monitorId = monitor._id; - return await this.db.createNotification(notification); - }) - ); - await monitor.save(); - } - - this.jobQueue.addJob(monitor._id, monitor); - }) - ); - - return res.success({ - msg: this.stringService.bulkMonitorsCreate, - data: monitors, - }); - } catch (error) { - return next(handleError(error, SERVICE_NAME, "createBulkMonitors")); + next(handleError(error, SERVICE_NAME, "createBulkMonitors")); } }, }); } catch (error) { return next(handleError(error, SERVICE_NAME, "createBulkMonitors")); } - }; + }; /** * Checks if the endpoint can be resolved * @async