improve duplicate activities handle

This commit is contained in:
SrGooglo 2025-02-05 02:51:18 +00:00
parent 9d0afcb329
commit 3990ef45c9

View File

@ -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)