added changes ready to ship

This commit is contained in:
tusuii
2026-02-21 12:06:16 +05:30
parent 1788e364f1
commit 82077d38e6
39 changed files with 694 additions and 5600 deletions

View File

@@ -0,0 +1,51 @@
import { Router } from 'express';
import pool from '../db.js';
import { randomUUID } from 'crypto';
const router = Router();
// GET /api/notifications/:userId
router.get('/:userId', async (req, res) => {
try {
const [rows] = await pool.query(
'SELECT * FROM notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT 50',
[req.params.userId]
);
res.json(rows);
} catch (err) {
console.error('Fetch notifications error:', err);
res.status(500).json({ error: 'Internal server error' });
}
});
// PUT /api/notifications/:id/read
router.put('/:id/read', async (req, res) => {
try {
await pool.query('UPDATE notifications SET is_read = TRUE WHERE id = ?', [req.params.id]);
res.json({ success: true });
} catch (err) {
console.error('Mark notification read error:', err);
res.status(500).json({ error: 'Internal server error' });
}
});
// Helper: Create notification and emit if possible
export async function createNotification(req, { userId, type, title, message, link }) {
try {
const id = randomUUID();
await pool.query(
'INSERT INTO notifications (id, user_id, type, title, message, link) VALUES (?, ?, ?, ?, ?, ?)',
[id, userId, type, title, message, link]
);
if (req.io) {
req.io.to(userId).emit('notification', {
id, user_id: userId, type, title, message, link, is_read: false, created_at: new Date()
});
}
} catch (err) {
console.error('Create notification error:', err);
}
}
export default router;