diff --git a/server.js b/server.js index 826c1f5..70b7463 100644 --- a/server.js +++ b/server.js @@ -174,7 +174,11 @@ async function entryAccessible(user, entryId) { if (!rows.length) return { found: false }; const groupId = rows[0].group_id; if (user.role === 'admin') return { found: true, group_id: groupId }; - return { found: true, group_id: groupId, allowed: groupId && (await groupBelongsToBranches(user, groupId)) }; + const allowed = groupId && (await groupBelongsToBranches(user, groupId)); + if (!allowed) { + console.warn(`[ACCESS DENIED] entryAccessible: user=${user.id} (${user.username}) branch_ids=${JSON.stringify(user.branch_ids)} entry=${entryId} group_id=${groupId}`); + } + return { found: true, group_id: groupId, allowed }; } function fixFilename(str) { @@ -2220,7 +2224,11 @@ app.put('/api/entries/:id/restore', requireAuth, async (req, res) => { return res.status(403).json({ error: 'Нет доступа к этой записи' }); } } - await pool.query('UPDATE entries SET deleted_at = NULL WHERE id = $1', [req.params.id]); + const result = await pool.query('UPDATE entries SET deleted_at = NULL WHERE id = $1', [req.params.id]); + console.log(`[RESTORE] User ${req.user.id} (${req.user.username}) restored entry ${req.params.id}, rowCount=${result.rowCount}`); + if (result.rowCount === 0) { + return res.status(404).json({ error: 'Запись не найдена или уже восстановлена' }); + } await logAudit(req, 'entry.restore', { id: req.params.id }); res.json({ ok: true }); }); @@ -2253,6 +2261,7 @@ app.get('/api/trash', requireAuth, async (req, res) => { where += ' AND 1 = 0'; } } + console.log(`[TRASH] User ${req.user.id} (${req.user.username}) role=${req.user.role} branch_ids=${JSON.stringify(bw.ids)} scoped=${scoped} where=${where} params=${JSON.stringify(params)}`); const { rows: crows } = await pool.query( `SELECT count(*)::int AS n FROM entries e JOIN groups g ON g.id = e.group_id WHERE e.deleted_at IS NOT NULL` + (scoped ? (bw.ids.length ? ` AND g.branch_id IN (${bw.ids.map((_, i) => `$${i + 1}`).join(',')})` : ' AND 1 = 0') : ''), @@ -2268,6 +2277,7 @@ app.get('/api/trash', requireAuth, async (req, res) => { const off = parseInt(offset, 10); if (off > 0) { qparams.push(off); q += ` OFFSET $${qparams.length}`; } const { rows } = await pool.query(q, qparams); + console.log(`[TRASH] Found ${rows.length} entries for user ${req.user.id}`); let files = {}; if (rows.length) { const fRes = await pool.query(