This commit is contained in:
payacom
2026-08-31 20:45:41 +03:30
parent fa9f23c61d
commit 3f416a7a60
367 changed files with 621895 additions and 6443 deletions

28
utils/notifyUser.js Normal file
View File

@@ -0,0 +1,28 @@
const NotificationModel = require('../models/NotificationModel')
const { sendPushToUser } = require('./pushNotification')
/**
* Creates a Notification row and, if a socket.io instance is available,
* pushes it live to the user's `user:{id}` room so it can be surfaced
* immediately (e.g. as a browser Notification) without waiting for a poll.
* همچنین یه پوش FCM هم برای گوشی کاربر می‌فرسته — این تنها نقطه‌ایه که باعث میشه
* همه‌ی نوتیفیکیشن‌هایی که از این تابع رد میشن به‌صورت پوش هم نمایش داده بشن
*/
async function notifyUser (io, userId, { type, title, description, project_post_id, actor_id } = {}) {
await NotificationModel.create({ user_id: userId, type, title, description, project_post_id, actor_id })
if (io) {
try {
io.to(`user:${String(userId)}`).emit('notification', { type, title, description })
} catch (_) {}
}
sendPushToUser(userId, {
title,
description,
type,
data: project_post_id ? { project_post_id: String(project_post_id) } : undefined
}).catch(() => {})
}
module.exports = { notifyUser }