full
This commit is contained in:
36
utils/tagNotification.js
Normal file
36
utils/tagNotification.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const NotificationModel = require('../models/NotificationModel')
|
||||
const UserModel = require('../models/UserModel')
|
||||
|
||||
const createTagNotifications = async ({ taggerId, taggedUserIds, postId }) => {
|
||||
try {
|
||||
if (!taggerId || !postId || !taggedUserIds?.length) return
|
||||
|
||||
const uniqueIds = [...new Set(taggedUserIds.map(String))].filter(
|
||||
(id) => id !== String(taggerId)
|
||||
)
|
||||
if (!uniqueIds.length) return
|
||||
|
||||
const tagger = await UserModel.findById(taggerId).select('user_name')
|
||||
const taggerUsername = tagger?.user_name || 'کاربر'
|
||||
const message = `${taggerUsername} شما را در پستی نشانهگذاری کرد.`
|
||||
|
||||
await Promise.all(
|
||||
uniqueIds.map((userId) =>
|
||||
NotificationModel.create({
|
||||
user_id: userId,
|
||||
project_post_id: postId,
|
||||
actor_id: taggerId,
|
||||
type: 'post_tag',
|
||||
title: message,
|
||||
description: message
|
||||
})
|
||||
)
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('Error creating tag notifications:', error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createTagNotifications
|
||||
}
|
||||
Reference in New Issue
Block a user