Add full project files

This commit is contained in:
root
2026-07-04 19:24:56 +03:30
parent d54f5441a3
commit b245e7b71a
835 changed files with 30149 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
const jwt = require('jsonwebtoken')
const NotificationModel = require('../../../models/NotificationModel')
const jMoment = require('moment-jalaali')
const getNotifications = async (req, res, next) => {
try {
const token = req.header('Authorization').split(' ')[1]
if (!token) return res.status(401).send('Access Denied')
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
const userId = decodedToken.id
const { page = 1, limit = 10, search } = req.query // افزودن پارامترهای صفحه‌بندی به درخواست
const filter = { user_id: userId }
if (search) {
filter.$or = [
{ title: { $regex: search, $options: 'i' } },
{ description: { $regex: search, $options: 'i' } }
]
}
// استفاده از مدل پیجینیت شده برای دریافت نتایج صفحه‌بندی شده
const options = {
page: parseInt(page), // تبدیل صفحه به عدد صحیح
limit: parseInt(limit), // تبدیل محدودیت به عدد صحیح
sort: { createdAt: -1 } // بر اساس زمان ایجاد (createdAt) مرتب کنید (نزولی)
}
// دریافت اعلان‌های مربوط به کاربر با استفاده از userId
let notifications = await NotificationModel.paginate(filter, options)
const totalPages = notifications.totalPages
const totalItems = notifications.totalDocs
notifications = notifications?.docs.map(notification => {
const jDate = jMoment(notification.createdAt).format('jYYYY-jMM-jDD HH:mm')
return {
...notification._doc,
createdAt: jDate
}
})
// تغییر وضعیت "read" اعلان‌ها به "true"
await NotificationModel.updateMany({ user_id: userId }, { read: true })
res.json({
notifications,
totalPages, // ارسال تعداد کل صفحات
totalItems // ارسال تعداد کل آیتم‌ها
})
} catch (error) {
next(error)
}
}
const unreadNotifications = async (req, res, next) => {
try {
const token = req.header('Authorization').split(' ')[1]
if (!token) return res.status(401).send('Access Denied')
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
const userId = decodedToken.id
// دریافت تعداد اعلان‌های جدید برای کاربر
const newNotificationsCount = await NotificationModel.countDocuments({ user_id: userId, read: false }).exec()
res.json({ newNotificationsCount })
} catch (error) {
next(error)
}
}
module.exports = {
getNotifications, unreadNotifications
}