From d173112baef1da4ab146fc50f4c04fb75bc1ff08 Mon Sep 17 00:00:00 2001 From: shats Date: Tue, 20 Jan 2026 17:20:47 +0700 Subject: [PATCH] feat(ServerManager): automate billing entry creation for new servers; synchronize existing servers with billing records --- frontend/src/ServerManager.jsx | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/frontend/src/ServerManager.jsx b/frontend/src/ServerManager.jsx index 1d71da1..560cfd2 100644 --- a/frontend/src/ServerManager.jsx +++ b/frontend/src/ServerManager.jsx @@ -351,6 +351,41 @@ function ServerManager() { setServers([...servers, serverToAdd]); handleCloseAddServerModal(); + + // Автоматически создаём запись в биллинге + (async () => { + try { + const billingRes = await api.get(`/billing`); + const billingData = Array.isArray(billingRes.data) ? billingRes.data : []; + + // Проверяем, нет ли уже такого сервера в биллинге + const alreadyExists = billingData.some(b => b.serverId === serverToAdd.id); + if (!alreadyExists) { + const newBillingItem = { + id: `billing-${serverToAdd.id}`, + hostName: serverToAdd.dns || serverToAdd.ip, + purpose: '', + country: serverToAdd.country || '', + provider: serverToAdd.provider || '', + loginUrl: '', + monthlyCost: 0, + monthlyCostCurrency: 'USD', + nextPaymentDate: '', + lastPaymentDate: '', + lastPaymentAmount: 0, + lastPaymentCurrency: 'USD', + status: 'active', + notes: 'Автоматически добавлен из серверов', + serverId: serverToAdd.id, + payments: [] + }; + await api.post(`/billing`, { domains: [...billingData, newBillingItem] }); + } + } catch (e) { + console.warn('Не удалось создать запись биллинга:', e); + } + })(); + setSuccess('Сервер успешно добавлен!'); setTimeout(() => setSuccess(''), 3000); }; @@ -549,6 +584,49 @@ function ServerManager() { setServers(normalizedServers); await api.post(`/servers`, { domains: normalizedServers }); await api.post(`/server-connections`, { domains: connections }); + + // Синхронизация с биллингом: добавляем новые серверы + try { + const billingRes = await api.get(`/billing`); + const billingData = Array.isArray(billingRes.data) ? billingRes.data : []; + + // Создаём Set из существующих serverId в биллинге + const existingServerIds = new Set( + billingData.map(b => b.serverId).filter(Boolean) + ); + + // Находим серверы, которых нет в биллинге + const newBillingItems = normalizedServers + .filter(server => server.id && !existingServerIds.has(server.id)) + .map(server => ({ + id: `billing-${server.id}`, + hostName: server.dns || server.ip, + purpose: '', + country: server.country || '', + provider: server.provider || '', + loginUrl: '', + monthlyCost: 0, + monthlyCostCurrency: 'USD', + nextPaymentDate: '', + lastPaymentDate: '', + lastPaymentAmount: 0, + lastPaymentCurrency: 'USD', + status: 'active', + notes: `Автоматически добавлен из серверов`, + serverId: server.id, + payments: [] + })); + + if (newBillingItems.length > 0) { + const updatedBilling = [...billingData, ...newBillingItems]; + await api.post(`/billing`, { domains: updatedBilling }); + console.log(`Добавлено ${newBillingItems.length} записей в биллинг`); + } + } catch (billingError) { + console.warn('Не удалось синхронизировать биллинг:', billingError); + // Не прерываем основную операцию + } + setSuccess('Изменения успешно сохранены!'); setTimeout(() => setSuccess(''), 3000); } catch (error) {