Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/fe-be/notifications #1079

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Server/db/mongo/modules/monitorModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,12 @@ const getMonitorById = async (monitorId) => {
const notifications = await Notification.find({
monitorId: monitorId,
});
monitor.notifications = notifications;
const monitorWithNotifications = await monitor.save();
return monitorWithNotifications;
const updatedMonitor = await Monitor.findByIdAndUpdate(
monitorId,
{ notifications },
{ new: true }
).populate("notifications");
Comment on lines +299 to +303
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Yo! The current implementation might still have concurrency issues, dawg! 🍝

The separate find and findByIdAndUpdate operations create a potential race condition. If notifications are modified between these operations, the monitor could end up with stale data.

Consider using MongoDB's aggregation pipeline for atomic operations:

-const notifications = await Notification.find({
-  monitorId: monitorId,
-});
-const updatedMonitor = await Monitor.findByIdAndUpdate(
-  monitorId,
-  { notifications },
-  { new: true }
-).populate("notifications");
+const updatedMonitor = await Monitor.aggregate([
+  { $match: { _id: monitorId } },
+  {
+    $lookup: {
+      from: "notifications",
+      localField: "_id",
+      foreignField: "monitorId",
+      as: "notifications"
+    }
+  },
+  { $limit: 1 }
+]).exec();
+
+if (!updatedMonitor[0]) {
+  const error = new Error(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId));
+  error.status = 404;
+  throw error;
+}
+
+return updatedMonitor[0];

Alternatively, if you need to stick with the current approach, wrap it in a transaction:

const session = await Monitor.startSession();
session.startTransaction();
try {
  const notifications = await Notification.find({ monitorId }).session(session);
  const updatedMonitor = await Monitor.findByIdAndUpdate(
    monitorId,
    { notifications },
    { new: true, session }
  ).populate("notifications");
  await session.commitTransaction();
  return updatedMonitor;
} catch (error) {
  await session.abortTransaction();
  throw error;
} finally {
  session.endSession();
}

return updatedMonitor;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getMonitorById";
Expand Down