Files
vps-tracker/server/sync-account-job.js
T
Denozordec 196c5be532 Enhance database management and synchronization features
- Added new backup and restore functionality for JSON and SQLite database formats.
- Implemented notification settings for low balance alerts and sync digests in the settings page.
- Updated database schema to include new columns for balance alert thresholds and notification preferences.
- Refactored synchronization logic to provide detailed summaries and improved error handling.
- Enhanced user interface for managing accounts and settings, including new input fields for balance alerts.
- Improved sync log display to include summary information for better tracking of synchronization results.
2026-03-20 23:11:58 +07:00

55 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Запуск синхронизации BILLmanager с записью в sync_log (для API и планировщика)
*/
import { getDb } from './db.js'
import { syncFromBillmanager } from './adapters/billmanager/index.js'
/**
* @param {object} account - строка provider_accounts
* @param {object} [opts] - { skipTariffs, skipVpsPayments }
* @returns {Promise<object>} результат syncFromBillmanager + ok, logId
*/
export async function runBillmanagerAccountSync(account, opts = {}) {
const db = getDb()
const logId = `sync-${account.id}-${Date.now()}`
db.prepare(`
INSERT INTO sync_log (id, accountId, startedAt, status)
VALUES (?, ?, ?, ?)
`).run(logId, account.id, new Date().toISOString(), 'running')
try {
const result = await syncFromBillmanager(account, db, opts)
const summaryPayload = {
...(result.syncSummary || {}),
vpsCount: result.vpsCount,
paymentsCount: result.paymentsCount,
tariffsCount: result.tariffsCount ?? 0,
}
db.prepare(`
UPDATE sync_log SET finishedAt=?, status=?, vpsCount=?, paymentsCount=?, summary=?
WHERE id=?
`).run(
new Date().toISOString(),
'ok',
result.vpsCount,
result.paymentsCount,
JSON.stringify(summaryPayload),
logId,
)
return { ok: true, logId, ...result }
} catch (err) {
db.prepare(`
UPDATE sync_log SET finishedAt=?, status=?, error=?, summary=?
WHERE id=?
`).run(
new Date().toISOString(),
'error',
err.message || 'Unknown error',
JSON.stringify({ error: err.message || 'Unknown error' }),
logId,
)
throw err
}
}