Replace notifications with activity feed
This commit is contained in:
30
backend/routes/activityRoutes.js
Normal file
30
backend/routes/activityRoutes.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const express = require('express');
|
||||
|
||||
const createActivityRouter = ({ pool }) => {
|
||||
const router = express.Router();
|
||||
|
||||
const listActivity = async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(
|
||||
`SELECT id, type, title, message, link, created_at
|
||||
FROM notifications
|
||||
WHERE user_id = ?
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 50`,
|
||||
[req.user.id]
|
||||
);
|
||||
res.json(rows);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
router.get('/activity', listActivity);
|
||||
router.get('/notifications', listActivity);
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createActivityRouter,
|
||||
};
|
||||
@@ -4,6 +4,7 @@ const jwt = require('jsonwebtoken');
|
||||
const crypto = require('crypto');
|
||||
const { hashSecret, maskSecret } = require('../utils/security');
|
||||
const { requireRole } = require('../middleware/auth');
|
||||
const { recordActivity } = require('../services/activityService');
|
||||
|
||||
const createAuthRouter = ({ pool, transporter, getBaseUrl, jwtSecret }) => {
|
||||
const router = express.Router();
|
||||
@@ -229,19 +230,25 @@ const createAuthRouter = ({ pool, transporter, getBaseUrl, jwtSecret }) => {
|
||||
[user.tenant_id, user.id]
|
||||
);
|
||||
for (const n of notifiable) {
|
||||
await pool.query(
|
||||
'INSERT INTO notifications (id, user_id, type, title, message, link) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[crypto.randomUUID(), n.id, 'info', 'Novo Membro Ativo', `${name} concluiu o cadastro e já pode acessar o sistema.`, `/users/${user.id}`]
|
||||
);
|
||||
await recordActivity(pool, {
|
||||
userId: n.id,
|
||||
type: 'info',
|
||||
title: 'Novo Membro Ativo',
|
||||
message: `${name} concluiu o cadastro e já pode acessar o sistema.`,
|
||||
link: `/users/${user.id}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (user.role === 'admin') {
|
||||
const [superAdmins] = await pool.query("SELECT id FROM users WHERE role = 'super_admin'");
|
||||
for (const sa of superAdmins) {
|
||||
await pool.query(
|
||||
'INSERT INTO notifications (id, user_id, type, title, message, link) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[crypto.randomUUID(), sa.id, 'success', 'Admin Ativo', `O admin ${name} da organização configurou sua conta.`, `/super-admin`]
|
||||
);
|
||||
await recordActivity(pool, {
|
||||
userId: sa.id,
|
||||
type: 'success',
|
||||
title: 'Admin Ativo',
|
||||
message: `O admin ${name} da organização configurou sua conta.`,
|
||||
link: '/super-admin',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const express = require('express');
|
||||
const crypto = require('crypto');
|
||||
const { requireRole } = require('../middleware/auth');
|
||||
const { recordActivity } = require('../services/activityService');
|
||||
|
||||
const createTenantsRouter = ({ pool, transporter, getBaseUrl }) => {
|
||||
const router = express.Router();
|
||||
@@ -37,10 +38,13 @@ const createTenantsRouter = ({ pool, transporter, getBaseUrl }) => {
|
||||
|
||||
const [superAdmins] = await connection.query("SELECT id FROM users WHERE role = 'super_admin'");
|
||||
for (const sa of superAdmins) {
|
||||
await connection.query(
|
||||
'INSERT INTO notifications (id, user_id, type, title, message, link) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[crypto.randomUUID(), sa.id, 'success', 'Nova Organização', `A organização ${name} foi criada.`, '/super-admin']
|
||||
);
|
||||
await recordActivity(connection, {
|
||||
userId: sa.id,
|
||||
type: 'success',
|
||||
title: 'Nova Organização',
|
||||
message: `A organização ${name} foi criada.`,
|
||||
link: '/super-admin',
|
||||
});
|
||||
}
|
||||
|
||||
await transporter.sendMail({
|
||||
|
||||
@@ -8,8 +8,9 @@ const {
|
||||
canChangeUserEmail,
|
||||
canManageUserRoleOrTeam,
|
||||
} = require('../policies/accessPolicy');
|
||||
const { recordActivity } = require('../services/activityService');
|
||||
|
||||
const USER_PUBLIC_FIELDS = 'id, tenant_id, team_id, name, email, slug, role, status, bio, avatar_url, sound_enabled, created_at';
|
||||
const USER_PUBLIC_FIELDS = 'id, tenant_id, team_id, name, email, slug, role, status, bio, avatar_url, created_at';
|
||||
|
||||
const createUsersRouter = ({ pool, upload, transporter, getBaseUrl }) => {
|
||||
const router = express.Router();
|
||||
@@ -118,7 +119,7 @@ const createUsersRouter = ({ pool, upload, transporter, getBaseUrl }) => {
|
||||
});
|
||||
|
||||
router.put('/users/:id', async (req, res) => {
|
||||
const { name, bio, role, team_id, status, email, sound_enabled } = req.body;
|
||||
const { name, bio, role, team_id, status, email } = req.body;
|
||||
try {
|
||||
const [existing] = await pool.query('SELECT * FROM users WHERE id = ?', [req.params.id]);
|
||||
if (existing.length === 0) return res.status(404).json({ error: 'Not found' });
|
||||
@@ -129,7 +130,6 @@ const createUsersRouter = ({ pool, upload, transporter, getBaseUrl }) => {
|
||||
const finalTeamId = canManageUserRoleOrTeam(req.user) && team_id !== undefined ? team_id : existing[0].team_id;
|
||||
const finalStatus = canManageUserStatus(req.user) && status !== undefined ? status : existing[0].status;
|
||||
const finalEmail = canChangeUserEmail(req.user, existing[0]) && email !== undefined ? email : existing[0].email;
|
||||
const finalSoundEnabled = req.user.id === req.params.id && sound_enabled !== undefined ? sound_enabled : (existing[0].sound_enabled ?? true);
|
||||
|
||||
if (finalEmail !== existing[0].email) {
|
||||
const [emailCheck] = await pool.query('SELECT id FROM users WHERE email = ? AND id != ?', [finalEmail, req.params.id]);
|
||||
@@ -137,17 +137,20 @@ const createUsersRouter = ({ pool, upload, transporter, getBaseUrl }) => {
|
||||
}
|
||||
|
||||
await pool.query(
|
||||
'UPDATE users SET name = ?, bio = ?, email = ?, role = ?, team_id = ?, status = ?, sound_enabled = ? WHERE id = ?',
|
||||
[name || existing[0].name, bio !== undefined ? bio : existing[0].bio, finalEmail, finalRole, finalTeamId || null, finalStatus, finalSoundEnabled, req.params.id]
|
||||
'UPDATE users SET name = ?, bio = ?, email = ?, role = ?, team_id = ?, status = ? WHERE id = ?',
|
||||
[name || existing[0].name, bio !== undefined ? bio : existing[0].bio, finalEmail, finalRole, finalTeamId || null, finalStatus, req.params.id]
|
||||
);
|
||||
|
||||
if (finalTeamId && finalTeamId !== existing[0].team_id && existing[0].status === 'active') {
|
||||
const [team] = await pool.query('SELECT name FROM teams WHERE id = ?', [finalTeamId]);
|
||||
if (team.length > 0) {
|
||||
await pool.query(
|
||||
'INSERT INTO notifications (id, user_id, type, title, message, link) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[crypto.randomUUID(), req.params.id, 'info', 'Novo Time', `Você foi adicionado ao time ${team[0].name}.`, '/']
|
||||
);
|
||||
await recordActivity(pool, {
|
||||
userId: req.params.id,
|
||||
type: 'info',
|
||||
title: 'Novo Time',
|
||||
message: `Você foi adicionado ao time ${team[0].name}.`,
|
||||
link: '/',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user