37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
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
|
|
}
|