notification

This commit is contained in:
Deep Koluguri 2025-11-05 21:14:46 -05:00
parent f985c00545
commit ce1e30f9a3
1 changed files with 40 additions and 18 deletions

View File

@ -11,27 +11,36 @@ const Notification = require('../models/Notification');
router.get('/', auth, async (req, res) => {
try {
const { page = 1, limit = 20, type, status } = req.query;
const ChitGroup = require('../models/ChitGroup');
const query = { user_id: req.user.id };
const where = { user_id: req.user.id };
// Filter by type if provided
if (type) {
query.type = type;
where.type = type;
}
// Filter by status if provided
if (status) {
query.status = status;
where.status = status;
}
const notifications = await Notification.find(query)
.sort({ createdAt: -1 })
.limit(parseInt(limit))
.skip((parseInt(page) - 1) * parseInt(limit))
.populate('group_id', 'name')
.select('-__v');
const offset = (parseInt(page) - 1) * parseInt(limit);
const total = await Notification.countDocuments(query);
const { count, rows: notifications } = await Notification.findAndCountAll({
where,
order: [['created_at', 'DESC']],
limit: parseInt(limit),
offset: offset,
include: [
{
model: ChitGroup,
as: 'group',
attributes: ['name'],
required: false
}
]
});
res.json({
success: true,
@ -39,17 +48,19 @@ router.get('/', auth, async (req, res) => {
notifications,
pagination: {
currentPage: parseInt(page),
totalPages: Math.ceil(total / parseInt(limit)),
totalItems: total,
totalPages: Math.ceil(count / parseInt(limit)),
totalItems: count,
itemsPerPage: parseInt(limit)
}
}
});
} catch (error) {
console.error('Error fetching notifications:', error);
console.error('Error stack:', error.stack);
res.status(500).json({
success: false,
message: 'Server error'
message: 'Server error',
error: process.env.NODE_ENV === 'development' ? error.message : undefined
});
}
});
@ -174,12 +185,23 @@ router.delete('/:id', auth, async (req, res) => {
*/
router.get('/stats', auth, async (req, res) => {
try {
const total = await Notification.countDocuments({ user_id: req.user.id });
const { Sequelize } = require('sequelize');
const total = await Notification.count({
where: { user_id: req.user.id }
});
const unread = await Notification.getUnreadCount(req.user.id);
const byType = await Notification.aggregate([
{ $match: { user_id: req.user.id } },
{ $group: { _id: '$type', count: { $sum: 1 } } }
]);
const byType = await Notification.findAll({
attributes: [
'type',
[Sequelize.fn('COUNT', Sequelize.col('type')), 'count']
],
where: { user_id: req.user.id },
group: ['type'],
raw: true
});
res.json({
success: true,