Initial commit: Club DynaSQY
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
const { Router } = require('express');
|
||||
const pool = require('../config/db');
|
||||
const { authenticate } = require('../middleware/auth');
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/', authenticate, async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(
|
||||
`SELECT n.id, n.title, n.body, n.created_at,
|
||||
u.prenom AS sender_prenom, u.nom AS sender_nom,
|
||||
nr.read_at, 'admin' AS source,
|
||||
NULL AS invite_id,
|
||||
NULL AS guest_name
|
||||
FROM notifications n
|
||||
JOIN notification_recipients nr ON nr.notification_id = n.id
|
||||
JOIN users u ON u.id = n.sender_id
|
||||
WHERE nr.user_id = ?
|
||||
UNION ALL
|
||||
SELECT -(i.id) AS id,
|
||||
CONCAT('Invitation en attente - ', i.prenom, ' ', i.nom) AS title,
|
||||
CONCAT('Vous n''avez pas encore envoy?? l''email d''invitation ?? ', i.prenom, ' ', i.nom) AS body,
|
||||
i.created_at,
|
||||
NULL AS sender_prenom, NULL AS sender_nom,
|
||||
NULL AS read_at,
|
||||
'pending_invite' AS source,
|
||||
i.id AS invite_id,
|
||||
CONCAT(i.prenom, ' ', i.nom) AS guest_name
|
||||
FROM invites i
|
||||
WHERE i.invite_par_id = ? AND (i.mail_envoye IS NULL OR i.mail_envoye = 0)
|
||||
ORDER BY created_at DESC`,
|
||||
[req.user.id, req.user.id]
|
||||
);
|
||||
res.json(rows);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/unread-count', authenticate, async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(
|
||||
'SELECT COUNT(*) AS count FROM notification_recipients WHERE user_id = ? AND read_at IS NULL',
|
||||
[req.user.id]
|
||||
);
|
||||
const [inviteRows] = await pool.query(
|
||||
'SELECT COUNT(*) AS count FROM invites WHERE invite_par_id = ? AND (mail_envoye IS NULL OR mail_envoye = 0)',
|
||||
[req.user.id]
|
||||
);
|
||||
res.json({ count: Number(rows[0].count) + Number(inviteRows[0].count) });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/:id/read', authenticate, async (req, res) => {
|
||||
try {
|
||||
await pool.query(
|
||||
'UPDATE notification_recipients SET read_at = NOW() WHERE notification_id = ? AND user_id = ?',
|
||||
[req.params.id, req.user.id]
|
||||
);
|
||||
res.json({ message: 'Marqu?? comme lu' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/read-all', authenticate, async (req, res) => {
|
||||
try {
|
||||
await pool.query(
|
||||
'UPDATE notification_recipients SET read_at = NOW() WHERE user_id = ? AND read_at IS NULL',
|
||||
[req.user.id]
|
||||
);
|
||||
res.json({ message: 'Tout marqu?? comme lu' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:id/recipient', authenticate, async (req, res) => {
|
||||
try {
|
||||
await pool.query(
|
||||
'DELETE FROM notification_recipients WHERE notification_id = ? AND user_id = ?',
|
||||
[req.params.id, req.user.id]
|
||||
);
|
||||
res.json({ message: 'Supprim??' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user