Removed all return error ro throw new err where this will be send to middleware.
This commit is contained in:
1 parent
075559ffc3
commit
30ef28170c
2 files changed
+62
-65
No files matched your search
@@ -16,7 +16,7 @@ export const useBulkMonitors = () => {
|
|||||||
const response = await networkService.createBulkMonitors(formData);
|
const response = await networkService.createBulkMonitors(formData);
|
||||||
return [true, response.data, null]; // [success, data, error]
|
return [true, response.data, null]; // [success, data, error]
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errorMessage = err?.response?.data?.errors?.[0]?.message || err.message;
|
const errorMessage = err?.response?.data?.msg || err.message;
|
||||||
return [false, null, errorMessage];
|
return [false, null, errorMessage];
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|||||||
@@ -257,27 +257,29 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const { parse } = pkg;
|
const { parse } = pkg;
|
||||||
|
|
||||||
|
// validate the file
|
||||||
if (!req.file) {
|
if (!req.file) {
|
||||||
return res.status(400).json({
|
throw new Error("No file uploaded");
|
||||||
errors: [
|
}
|
||||||
{ message: "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;
|
const { userId, teamId } = req.body;
|
||||||
|
|
||||||
if (!userId || !teamId) {
|
if (!userId || !teamId) {
|
||||||
return res.status(400).json({
|
throw new Error("Missing userId or teamId in form data");
|
||||||
errors: [
|
|
||||||
{ message: "Missing userId or teamId in form data" }
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get file buffer from memory and convert to string
|
// Get file buffer from memory and convert to string
|
||||||
const fileData = req.file.buffer.toString("utf-8");
|
const fileData = req.file.buffer.toString("utf-8");
|
||||||
console.log("File data:", fileData);
|
|
||||||
|
|
||||||
// Parse the CSV data
|
// Parse the CSV data
|
||||||
parse(fileData, {
|
parse(fileData, {
|
||||||
@@ -290,11 +292,7 @@ class MonitorController {
|
|||||||
if (["port", "interval"].includes(header)) {
|
if (["port", "interval"].includes(header)) {
|
||||||
const num = parseInt(value, 10);
|
const num = parseInt(value, 10);
|
||||||
if (isNaN(num)) {
|
if (isNaN(num)) {
|
||||||
return res.status(400).json({
|
throw new Error(`${header} should be a valid number, got: ${value}`);
|
||||||
errors: [
|
|
||||||
{ message: `${header} should be a valid number, got: ${value}` }
|
|
||||||
]
|
|
||||||
}); // Return error immediately with this structure
|
|
||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
@@ -302,57 +300,56 @@ class MonitorController {
|
|||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
complete: async ({ data, errors }) => {
|
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 {
|
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);
|
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) {
|
} catch (error) {
|
||||||
return next(handleValidationError(error, SERVICE_NAME));
|
next(handleError(error, SERVICE_NAME, "createBulkMonitors"));
|
||||||
}
|
|
||||||
|
|
||||||
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"));
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user