v0.5.134: восстановление записей, кнопка удалённых, время МСК
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
module.exports = {
|
||||
version: '0.5.133',
|
||||
version: '0.5.134',
|
||||
|
||||
// ── IAM ──────────────────────────────────────────────────────────────────
|
||||
iamUrl: process.env.V2_IAM_URL || 'https://auth-api.ngcloud.ru/api/v1/auth/user',
|
||||
|
||||
@@ -74,4 +74,11 @@ async function remove(entryId, clientId, email, impBy) {
|
||||
return q.deleteEntry(entryId, co.id, email, impBy);
|
||||
}
|
||||
|
||||
module.exports = { list, add, edit, remove };
|
||||
// ── restore(entryId, clientId, email, impBy) → void ─────────────────────────
|
||||
// Восстановление удалённой записи.
|
||||
async function restore(entryId, clientId, email, impBy) {
|
||||
const co = await resolve(clientId);
|
||||
return q.restoreEntry(entryId, co.id, email, impBy);
|
||||
}
|
||||
|
||||
module.exports = { list, add, edit, remove, restore };
|
||||
|
||||
+39
-1
@@ -177,6 +177,44 @@ async function deleteEntry(entryId, companyId, userEmail, impersonatedBy) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Восстановить удалённую запись (убрать deleted_at).
|
||||
*/
|
||||
async function restoreEntry(entryId, companyId, userEmail, impersonatedBy) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
const old = (await client.query(
|
||||
'SELECT * FROM whitelist_entries WHERE id = $1 AND company_id = $2 AND deleted_at IS NOT NULL',
|
||||
[entryId, companyId]
|
||||
)).rows[0];
|
||||
if (!old) throw new Error('Удалённая запись не найдена');
|
||||
|
||||
await client.query(
|
||||
'UPDATE whitelist_entries SET deleted_by = NULL, deleted_at = NULL WHERE id = $1 AND company_id = $2',
|
||||
[entryId, companyId]
|
||||
);
|
||||
await logAudit(userEmail, companyId, 'RESTORE', old.value_cidr, old.value_cidr, entryId, impersonatedBy, client);
|
||||
|
||||
// Проверить лимит
|
||||
const company = (await client.query('SELECT * FROM companies WHERE id = $1', [companyId])).rows[0];
|
||||
const limit = company.custom_limit != null ? company.custom_limit : (parseInt(process.env.DEFAULT_LIMIT, 10) || 15);
|
||||
const cnt = (await client.query(
|
||||
'SELECT COUNT(*)::int AS c FROM whitelist_entries WHERE company_id = $1 AND deleted_at IS NULL',
|
||||
[companyId]
|
||||
)).rows[0].c;
|
||||
if (cnt > limit) throw new Error('Восстановление превысит лимит (' + limit + ')');
|
||||
|
||||
await client.query('COMMIT');
|
||||
} catch (e) {
|
||||
await client.query('ROLLBACK');
|
||||
throw e;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
// ── Админ ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
async function getCompanyById(id) {
|
||||
@@ -241,7 +279,7 @@ async function getAudit(companyId = null) {
|
||||
|
||||
module.exports = {
|
||||
getOrCreateCompany, getLimit,
|
||||
listEntries, createEntry, updateEntry, deleteEntry,
|
||||
listEntries, createEntry, updateEntry, deleteEntry, restoreEntry,
|
||||
getExportCIDRs, getAudit,
|
||||
getCompanyById, getAllCompanies, setLimit,
|
||||
};
|
||||
|
||||
+17
-1
@@ -46,7 +46,7 @@ async function ensureSchema(pool) {
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_email VARCHAR(255) NOT NULL,
|
||||
company_id INTEGER NOT NULL,
|
||||
action VARCHAR(32) NOT NULL CHECK (action IN ('CREATE','UPDATE','DELETE')),
|
||||
action VARCHAR(32) NOT NULL CHECK (action IN ('CREATE','UPDATE','DELETE','RESTORE')),
|
||||
old_value TEXT,
|
||||
new_value TEXT,
|
||||
entry_id INTEGER,
|
||||
@@ -56,6 +56,22 @@ async function ensureSchema(pool) {
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_log_company_time
|
||||
ON audit_log(company_id, created_at DESC);
|
||||
|
||||
-- Миграция: добавить RESTORE в CHECK (если ещё нет)
|
||||
DO $$
|
||||
DECLARE
|
||||
cn text;
|
||||
BEGIN
|
||||
SELECT con.conname INTO cn
|
||||
FROM pg_constraint con
|
||||
JOIN pg_class rel ON rel.oid = con.conrelid
|
||||
WHERE rel.relname = 'audit_log' AND con.contype = 'c';
|
||||
IF cn IS NOT NULL THEN
|
||||
EXECUTE 'ALTER TABLE audit_log DROP CONSTRAINT ' || cn;
|
||||
END IF;
|
||||
EXECUTE 'ALTER TABLE audit_log ADD CONSTRAINT audit_log_action_check CHECK (action IN (''CREATE'',''UPDATE'',''DELETE'',''RESTORE''))';
|
||||
END;
|
||||
$$;
|
||||
`);
|
||||
|
||||
console.log('[db] Schema ensured');
|
||||
|
||||
@@ -122,6 +122,16 @@ function createUserRouter() {
|
||||
}
|
||||
});
|
||||
|
||||
// ── POST /restore/:id ──────────────────────────────────────────────
|
||||
router.post('/restore/:id', async (req, res) => {
|
||||
try {
|
||||
await crud.restore(parseInt(req.params.id), req.clientId, req.email, req.impersonatedBy);
|
||||
res.redirect('/v2/app?msg=Восстановлено');
|
||||
} catch (e) {
|
||||
res.redirect('/v2/app?error=' + encodeURIComponent(e.message));
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user