// ═══════════════════════════════════════════════════════════════════════════════ // V2 — CRUD API-слой (чистые функции, без Express) // // Принимает clientId (W-номер), сам резолвит companyId через getOrCreateCompany. // Фронтенд (user/) и тесты (test/) дергают эти функции. // // Сигнатуры: // list(clientId) → { entries, used, limit } // add(clientId, cidr, comment, email, impBy) → { entry, wasNormalized } // edit(entryId, clientId, cidr, comment, email, impBy) → { entry, wasNormalized } // remove(entryId, clientId, email, impBy) → void // ═══════════════════════════════════════════════════════════════════════════════ const q = require('../db/queries'); async function resolve(clientId) { return q.getOrCreateCompany(clientId, clientId); } async function list(clientId, includeDeleted = false) { const co = await resolve(clientId); const entries = await q.listEntries(co.id, includeDeleted); const limit = await q.getLimit(co); const used = entries.filter(e => !e.deleted_at).length; return { entries, used, limit }; } async function add(clientId, cidr, comment, email, impBy) { const co = await resolve(clientId); return q.createEntry(co.id, cidr, comment, email, impBy); } async function edit(entryId, clientId, cidr, comment, email, impBy) { const co = await resolve(clientId); return q.updateEntry(entryId, co.id, cidr, comment, email, impBy); } async function remove(entryId, clientId, email, impBy) { const co = await resolve(clientId); return q.deleteEntry(entryId, co.id, email, impBy); } module.exports = { list, add, edit, remove };