notification
This commit is contained in:
parent
f985c00545
commit
ce1e30f9a3
|
|
@ -11,27 +11,36 @@ const Notification = require('../models/Notification');
|
||||||
router.get('/', auth, async (req, res) => {
|
router.get('/', auth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { page = 1, limit = 20, type, status } = req.query;
|
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
|
// Filter by type if provided
|
||||||
if (type) {
|
if (type) {
|
||||||
query.type = type;
|
where.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by status if provided
|
// Filter by status if provided
|
||||||
if (status) {
|
if (status) {
|
||||||
query.status = status;
|
where.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifications = await Notification.find(query)
|
const offset = (parseInt(page) - 1) * parseInt(limit);
|
||||||
.sort({ createdAt: -1 })
|
|
||||||
.limit(parseInt(limit))
|
|
||||||
.skip((parseInt(page) - 1) * parseInt(limit))
|
|
||||||
.populate('group_id', 'name')
|
|
||||||
.select('-__v');
|
|
||||||
|
|
||||||
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({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|
@ -39,17 +48,19 @@ router.get('/', auth, async (req, res) => {
|
||||||
notifications,
|
notifications,
|
||||||
pagination: {
|
pagination: {
|
||||||
currentPage: parseInt(page),
|
currentPage: parseInt(page),
|
||||||
totalPages: Math.ceil(total / parseInt(limit)),
|
totalPages: Math.ceil(count / parseInt(limit)),
|
||||||
totalItems: total,
|
totalItems: count,
|
||||||
itemsPerPage: parseInt(limit)
|
itemsPerPage: parseInt(limit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching notifications:', error);
|
console.error('Error fetching notifications:', error);
|
||||||
|
console.error('Error stack:', error.stack);
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
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) => {
|
router.get('/stats', auth, async (req, res) => {
|
||||||
try {
|
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 unread = await Notification.getUnreadCount(req.user.id);
|
||||||
const byType = await Notification.aggregate([
|
|
||||||
{ $match: { user_id: req.user.id } },
|
const byType = await Notification.findAll({
|
||||||
{ $group: { _id: '$type', count: { $sum: 1 } } }
|
attributes: [
|
||||||
]);
|
'type',
|
||||||
|
[Sequelize.fn('COUNT', Sequelize.col('type')), 'count']
|
||||||
|
],
|
||||||
|
where: { user_id: req.user.id },
|
||||||
|
group: ['type'],
|
||||||
|
raw: true
|
||||||
|
});
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue