feat: version 2.12.0 -- editar y borrar comentarios

Hasta ahora un comentario (de proyecto o actividad) solo se podia crear, nunca tocar. Se
agrega PUT/DELETE /comments/:id, restringido a quien lo escribio o a superadmin/admin (igual
criterio que useria para notas/tareas: nunca otro colaborador aunque tenga permiso de
escritura en el proyecto). Migracion 021 agrega updated_at a comments para mostrar "Editado"
igual que ya hace Notas.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Milton Gonzalez
2026-10-14 12:22:27 -05:00
parent e5dfc2feff
commit 3257c4f065
10 changed files with 140 additions and 16 deletions
+20 -7
View File
@@ -2,7 +2,7 @@ import { pool } from '../../config/db.js';
export async function findByEntity(entityType, entityId) {
const [rows] = await pool.query(
`SELECT c.id, c.entity_type, c.entity_id, c.user_id, u.name AS user_name, c.message, c.created_at
`SELECT c.id, c.entity_type, c.entity_id, c.user_id, u.name AS user_name, c.message, c.created_at, c.updated_at
FROM comments c JOIN users u ON u.id = c.user_id
WHERE c.entity_type = ? AND c.entity_id = ? ORDER BY c.created_at`,
[entityType, entityId]
@@ -10,6 +10,24 @@ export async function findByEntity(entityType, entityId) {
return rows;
}
export async function findById(id) {
const [rows] = await pool.query(
`SELECT c.id, c.entity_type, c.entity_id, c.user_id, u.name AS user_name, c.message, c.created_at, c.updated_at
FROM comments c JOIN users u ON u.id = c.user_id WHERE c.id = ?`,
[id]
);
return rows[0] || null;
}
export async function update(id, message) {
await pool.query('UPDATE comments SET message = ? WHERE id = ?', [message, id]);
return findById(id);
}
export async function remove(id) {
await pool.query('DELETE FROM comments WHERE id = ?', [id]);
}
// Usado al borrar la entidad duena (actividad, proyecto): limpia sus comentarios de paso, ya que
// no hay FK fisica cruzada (entity_type/entity_id es polimorfico) que lo haga por cascada.
export async function removeByEntity(entityType, entityId) {
@@ -27,10 +45,5 @@ export async function create({ entityType, entityId, userId, message }) {
'INSERT INTO comments (entity_type, entity_id, user_id, message) VALUES (?, ?, ?, ?)',
[entityType, entityId, userId, message]
);
const [rows] = await pool.query(
`SELECT c.id, c.entity_type, c.entity_id, c.user_id, u.name AS user_name, c.message, c.created_at
FROM comments c JOIN users u ON u.id = c.user_id WHERE c.id = ?`,
[result.insertId]
);
return rows[0];
return findById(result.insertId);
}