v2: выделение валидации — validate() в crud, db/queries получает готовый cidr

This commit is contained in:
2026-06-13 07:11:53 +04:00
parent 3c061b6c4b
commit fe5a7d0d37
4 changed files with 10 additions and 9 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "ipwhitelist", "name": "ipwhitelist",
"version": "0.5.90", "version": "0.5.91",
"description": "IP WhiteList microservice for cloud provider", "description": "IP WhiteList microservice for cloud provider",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
+1 -1
View File
@@ -6,7 +6,7 @@
// ═══════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════
module.exports = { module.exports = {
version: '0.5.90', version: '0.5.91',
iamUrl: process.env.V2_IAM_API_URL || 'https://auth-api.ngcloud.ru', iamUrl: process.env.V2_IAM_API_URL || 'https://auth-api.ngcloud.ru',
appUrl: process.env.V2_APP_URL || 'https://whitelist.nodejsk8s.services.ngcloud.ru', appUrl: process.env.V2_APP_URL || 'https://whitelist.nodejsk8s.services.ngcloud.ru',
}; };
+5 -2
View File
@@ -12,6 +12,7 @@
// ═══════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════
const q = require('../db/queries'); const q = require('../db/queries');
const { validate } = require('../validators');
async function resolve(clientId) { async function resolve(clientId) {
return q.getOrCreateCompany(clientId, clientId); return q.getOrCreateCompany(clientId, clientId);
@@ -26,13 +27,15 @@ async function list(clientId, includeDeleted = false) {
} }
async function add(clientId, cidr, comment, email, impBy) { async function add(clientId, cidr, comment, email, impBy) {
const { cidr: validated, wasNormalized } = validate(cidr);
const co = await resolve(clientId); const co = await resolve(clientId);
return q.createEntry(co.id, cidr, comment, email, impBy); return q.createEntry(co.id, validated, comment, email, impBy, wasNormalized);
} }
async function edit(entryId, clientId, cidr, comment, email, impBy) { async function edit(entryId, clientId, cidr, comment, email, impBy) {
const { cidr: validated, wasNormalized } = validate(cidr);
const co = await resolve(clientId); const co = await resolve(clientId);
return q.updateEntry(entryId, co.id, cidr, comment, email, impBy); return q.updateEntry(entryId, co.id, validated, comment, email, impBy, wasNormalized);
} }
async function remove(entryId, clientId, email, impBy) { async function remove(entryId, clientId, email, impBy) {
+3 -5
View File
@@ -4,7 +4,7 @@
// ═══════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════
const { pool } = require('./index'); const { pool } = require('./index');
const { validate, overlaps } = require('../validators'); const { overlaps } = require('../validators');
// ── Компании ────────────────────────────────────────────────────────────────── // ── Компании ──────────────────────────────────────────────────────────────────
@@ -48,8 +48,7 @@ async function listEntries(companyId, includeDeleted = false) {
/** /**
* Создать запись (с валидацией, проверкой лимита и пересечений). * Создать запись (с валидацией, проверкой лимита и пересечений).
*/ */
async function createEntry(companyId, rawValue, comment, userEmail, impersonatedBy) { async function createEntry(companyId, cidr, comment, userEmail, impersonatedBy, wasNormalized = false) {
const { cidr, wasNormalized } = validate(rawValue);
const client = await pool.connect(); const client = await pool.connect();
try { try {
await client.query('BEGIN'); await client.query('BEGIN');
@@ -95,8 +94,7 @@ async function createEntry(companyId, rawValue, comment, userEmail, impersonated
/** /**
* Обновить запись. * Обновить запись.
*/ */
async function updateEntry(entryId, companyId, rawValue, comment, userEmail, impersonatedBy) { async function updateEntry(entryId, companyId, cidr, comment, userEmail, impersonatedBy, wasNormalized = false) {
const { cidr, wasNormalized } = validate(rawValue);
const client = await pool.connect(); const client = await pool.connect();
try { try {
await client.query('BEGIN'); await client.query('BEGIN');