feat(ServerManager): automate billing entry creation for new servers; synchronize existing servers with billing records
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m45s
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m45s
This commit is contained in:
@@ -351,6 +351,41 @@ function ServerManager() {
|
|||||||
|
|
||||||
setServers([...servers, serverToAdd]);
|
setServers([...servers, serverToAdd]);
|
||||||
handleCloseAddServerModal();
|
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('Сервер успешно добавлен!');
|
setSuccess('Сервер успешно добавлен!');
|
||||||
setTimeout(() => setSuccess(''), 3000);
|
setTimeout(() => setSuccess(''), 3000);
|
||||||
};
|
};
|
||||||
@@ -549,6 +584,49 @@ function ServerManager() {
|
|||||||
setServers(normalizedServers);
|
setServers(normalizedServers);
|
||||||
await api.post(`/servers`, { domains: normalizedServers });
|
await api.post(`/servers`, { domains: normalizedServers });
|
||||||
await api.post(`/server-connections`, { domains: connections });
|
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('Изменения успешно сохранены!');
|
setSuccess('Изменения успешно сохранены!');
|
||||||
setTimeout(() => setSuccess(''), 3000);
|
setTimeout(() => setSuccess(''), 3000);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user