added changes ready to ship
This commit is contained in:
51
server/routes/notifications.js
Normal file
51
server/routes/notifications.js
Normal 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;
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from 'express';
|
||||
import pool from '../db.js';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { createNotification } from './notifications.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -88,6 +89,17 @@ router.post('/', async (req, res) => {
|
||||
[actId, id, '📝 Task created']);
|
||||
|
||||
await conn.commit();
|
||||
|
||||
if (assignee) {
|
||||
await createNotification(req, {
|
||||
userId: assignee,
|
||||
type: 'assignment',
|
||||
title: 'New Task Assigned',
|
||||
message: `You have been assigned to task: ${title}`,
|
||||
link: `/tasks?id=${id}`
|
||||
});
|
||||
}
|
||||
|
||||
const task = await getFullTask(id);
|
||||
res.status(201).json(task);
|
||||
} catch (err) {
|
||||
@@ -178,6 +190,24 @@ router.post('/:id/comments', async (req, res) => {
|
||||
const timestamp = new Date();
|
||||
await pool.query('INSERT INTO comments (id, task_id, user_id, text, timestamp) VALUES (?, ?, ?, ?, ?)',
|
||||
[id, req.params.id, userId, text, timestamp]);
|
||||
|
||||
// Mention detection: @[Name](userId)
|
||||
const mentions = text.match(/@\[([^\]]+)\]\(([^)]+)\)/g);
|
||||
if (mentions) {
|
||||
const mentionedUserIds = [...new Set(mentions.map(m => m.match(/\(([^)]+)\)/)[1]))];
|
||||
for (const mId of mentionedUserIds) {
|
||||
if (mId !== userId) {
|
||||
await createNotification(req, {
|
||||
userId: mId,
|
||||
type: 'mention',
|
||||
title: 'New Mention',
|
||||
message: `You were mentioned in a comment on task ${req.params.id}`,
|
||||
link: `/tasks?id=${req.params.id}`
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.status(201).json({ id, userId, text, timestamp: timestamp.toISOString() });
|
||||
} catch (err) {
|
||||
console.error('Add comment error:', err);
|
||||
|
||||
Reference in New Issue
Block a user