Enhance account synchronization with optional tariff syncing
- Updated `syncAccount` function to accept options for selective synchronization. - Modified `syncFromBillmanager` to handle new options for skipping tariff syncs. - Adjusted `TariffsPage` to utilize the new option for syncing only tariffs. - Removed unused tariff tracking from `VpsPage` and updated sync message logic accordingly.
This commit is contained in:
@@ -64,6 +64,7 @@ router.post('/:accountId', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const db = getDb()
|
const db = getDb()
|
||||||
const { accountId } = req.params
|
const { accountId } = req.params
|
||||||
|
const { onlyTariffs = false } = req.body || {}
|
||||||
const row = db.prepare('SELECT * FROM provider_accounts WHERE id = ?').get(accountId)
|
const row = db.prepare('SELECT * FROM provider_accounts WHERE id = ?').get(accountId)
|
||||||
if (!row) {
|
if (!row) {
|
||||||
return res.status(404).json({ error: 'Account not found' })
|
return res.status(404).json({ error: 'Account not found' })
|
||||||
@@ -81,7 +82,8 @@ router.post('/:accountId', async (req, res) => {
|
|||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
`).run(logId, accountId, new Date().toISOString(), 'running')
|
`).run(logId, accountId, new Date().toISOString(), 'running')
|
||||||
|
|
||||||
const result = await syncFromBillmanager(row, db)
|
const opts = onlyTariffs ? { skipVpsPayments: true } : { skipTariffs: true }
|
||||||
|
const result = await syncFromBillmanager(row, db, opts)
|
||||||
|
|
||||||
db.prepare(`
|
db.prepare(`
|
||||||
UPDATE sync_log SET finishedAt=?, status=?, vpsCount=?, paymentsCount=?
|
UPDATE sync_log SET finishedAt=?, status=?, vpsCount=?, paymentsCount=?
|
||||||
|
|||||||
+5
-2
@@ -135,8 +135,11 @@ export async function bulkUpdateVps(ids, action, value) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function syncAccount(accountId) {
|
export async function syncAccount(accountId, opts = {}) {
|
||||||
return fetchApi(`/api/sync/${encodeURIComponent(accountId)}`, { method: 'POST' })
|
return fetchApi(`/api/sync/${encodeURIComponent(accountId)}`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(opts),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchAccountBalance(accountId) {
|
export async function fetchAccountBalance(accountId) {
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ export function TariffsPage({ db, actions, settings, ratesData }) {
|
|||||||
let lastError = null
|
let lastError = null
|
||||||
for (const account of billmanagerAccounts) {
|
for (const account of billmanagerAccounts) {
|
||||||
try {
|
try {
|
||||||
const result = await syncAccount(account.id)
|
const result = await syncAccount(account.id, { onlyTariffs: true })
|
||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
totalTariffs += result.synced?.tariffsCount ?? 0
|
totalTariffs += result.synced?.tariffsCount ?? 0
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -328,7 +328,6 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
|||||||
setSyncMessage(null)
|
setSyncMessage(null)
|
||||||
let totalVps = 0
|
let totalVps = 0
|
||||||
let totalPayments = 0
|
let totalPayments = 0
|
||||||
let totalTariffs = 0
|
|
||||||
let lastError = null
|
let lastError = null
|
||||||
for (const account of billmanagerAccounts) {
|
for (const account of billmanagerAccounts) {
|
||||||
try {
|
try {
|
||||||
@@ -336,7 +335,6 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
|||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
totalVps += result.synced?.vpsCount ?? 0
|
totalVps += result.synced?.vpsCount ?? 0
|
||||||
totalPayments += result.synced?.paymentsCount ?? 0
|
totalPayments += result.synced?.paymentsCount ?? 0
|
||||||
totalTariffs += result.synced?.tariffsCount ?? 0
|
|
||||||
} else {
|
} else {
|
||||||
lastError = result.error
|
lastError = result.error
|
||||||
}
|
}
|
||||||
@@ -344,16 +342,15 @@ export function VpsPage({ db, actions, settings, ratesData }) {
|
|||||||
lastError = err.message
|
lastError = err.message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (lastError && totalVps === 0 && totalPayments === 0 && totalTariffs === 0) {
|
if (lastError && totalVps === 0 && totalPayments === 0) {
|
||||||
setSyncMessage(lastError)
|
setSyncMessage(lastError)
|
||||||
} else {
|
} else {
|
||||||
const parts = []
|
const parts = []
|
||||||
if (totalVps > 0) parts.push(`${totalVps} VPS`)
|
if (totalVps > 0) parts.push(`${totalVps} VPS`)
|
||||||
if (totalPayments > 0) parts.push(`${totalPayments} платежей`)
|
if (totalPayments > 0) parts.push(`${totalPayments} платежей`)
|
||||||
if (totalTariffs > 0) parts.push(`${totalTariffs} тарифов`)
|
|
||||||
setSyncMessage(`Синхронизировано: ${parts.join(', ')}${lastError ? `. Ошибки: ${lastError}` : ''}`)
|
setSyncMessage(`Синхронизировано: ${parts.join(', ')}${lastError ? `. Ошибки: ${lastError}` : ''}`)
|
||||||
}
|
}
|
||||||
if (totalVps > 0 || totalPayments > 0 || totalTariffs > 0) await actions.refreshData()
|
if (totalVps > 0 || totalPayments > 0) await actions.refreshData()
|
||||||
setSyncLoading(false)
|
setSyncLoading(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user