From 3990ef45c96ee8d82b29febd31d07cb9ceb6fab5 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Wed, 5 Feb 2025 02:51:18 +0000 Subject: [PATCH] improve duplicate activities handle --- .../services/main/routes/events/client/post.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/server/services/main/routes/events/client/post.js b/packages/server/services/main/routes/events/client/post.js index b80e603b..bdf4f7c2 100644 --- a/packages/server/services/main/routes/events/client/post.js +++ b/packages/server/services/main/routes/events/client/post.js @@ -29,7 +29,7 @@ export default { const type = IdToTypes[id] // get latest 20 activities - const latestActivities = await RecentActivity.find({ + let latestActivities = await RecentActivity.find({ user_id: user_id, type: type, }) @@ -37,12 +37,15 @@ export default { .sort({ created_at: -1 }) // check if the activity is already in some position and remove - latestActivities.find((activity, index) => { - if (activity.payload === payload && activity.type === type) { - latestActivities.splice(index, 1) - } + const sameLatestActivityIndex = latestActivities.findIndex((activity) => { + return activity.payload === payload && activity.type === type }) + // if the activity is already in some position, remove it from that position + if (sameLatestActivityIndex !== -1) { + latestActivities.splice(sameLatestActivityIndex, 1) + } + // if the list is full, remove the oldest activity and add the new one if (latestActivities.length >= 20) { await RecentActivity.findByIdAndDelete(latestActivities[latestActivities.length - 1]._id)