fix(sync): не стирать тарифы при ошибке загрузки прайс-листа

Ошибка запроса тарифов превращалась в пустой список: active_tariffs
удалялись, а следующий успешный синк объявлял все тарифы новыми.
Пустой ответ теперь оставляет старый список, замена идёт в транзакции.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DenozordecandClaude Opus 5.5 committed 2026-11-06 22:32:50 +07:00
1 parent 01a771407d
commit 78f413324a
6 files changed
+34 -10

No files matched your search

+5 -2
View File
@@ -3,7 +3,7 @@
*/ */
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { getCurrentSpaceId, getDb, schema } from '@cfdm/db' import { getCurrentSpaceId, getDb, getSqlite, schema } from '@cfdm/db'
import { findSyncedVps } from '../providers/find-synced-vps.js' import { findSyncedVps } from '../providers/find-synced-vps.js'
import type { BillmanagerSyncAccount } from './context.js' import type { BillmanagerSyncAccount } from './context.js'
@@ -355,7 +355,9 @@ export async function syncFromBillmanager(
let tariffsCount = 0 let tariffsCount = 0
const newTariffs: { name: string; price: string; providerId: string }[] = [] const newTariffs: { name: string; price: string; providerId: string }[] = []
if (fetchTariffs) { // Fetch errors come back as [] — keep the old list instead of wiping it (and re-announcing all as new).
if (fetchTariffs && tariffItems.length > 0) {
getSqlite().transaction(() => {
const existingTariffIds = new Set( const existingTariffIds = new Set(
db db
.select({ id: schema.activeTariffs.id }) .select({ id: schema.activeTariffs.id })
@@ -423,6 +425,7 @@ export async function syncFromBillmanager(
}) })
.run() .run()
} }
})()
} }
if (!fetchVpsPayments) { if (!fetchVpsPayments) {
@@ -162,4 +162,13 @@ describe('syncFromFourvps', () => {
const ids = (getSqlite().prepare(`SELECT id FROM vps ORDER BY id`).all() as { id: string }[]).map((r) => r.id) const ids = (getSqlite().prepare(`SELECT id FROM vps ORDER BY id`).all() as { id: string }[]).map((r) => r.id)
expect(ids).toEqual(['manual-12', 'vps-4vps-acc-4vps-100']) expect(ids).toEqual(['manual-12', 'vps-4vps-acc-4vps-100'])
}) })
it('keeps tariffs when the tariff fetch fails and does not re-announce them', async () => {
await syncFromFourvps(account)
vi.mocked(fetchTarifList).mockRejectedValueOnce(new Error('timeout'))
await syncFromFourvps(account)
const count = getSqlite().prepare(`SELECT COUNT(*) as c FROM active_tariffs`).get() as { c: number }
expect(count.c).toBe(1)
const third = await syncFromFourvps(account)
expect(third.newTariffs).toEqual([])
})
}) })
+5 -2
View File
@@ -3,7 +3,7 @@
*/ */
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { getCurrentSpaceId, getDb, schema } from '@cfdm/db' import { getCurrentSpaceId, getDb, getSqlite, schema } from '@cfdm/db'
import { findSyncedVps } from '../providers/find-synced-vps.js' import { findSyncedVps } from '../providers/find-synced-vps.js'
import type { FourvpsSyncAccount } from './context.js' import type { FourvpsSyncAccount } from './context.js'
@@ -223,7 +223,9 @@ export async function syncFromFourvps(
let tariffsCount = 0 let tariffsCount = 0
const newTariffs: { name: string; price: string; providerId: string }[] = [] const newTariffs: { name: string; price: string; providerId: string }[] = []
if (fetchTariffs) { // Fetch errors come back as [] — keep the old list instead of wiping it (and re-announcing all as new).
if (fetchTariffs && tariffItems.length > 0) {
getSqlite().transaction(() => {
const existingTariffIds = new Set( const existingTariffIds = new Set(
db db
.select({ id: schema.activeTariffs.id }) .select({ id: schema.activeTariffs.id })
@@ -273,6 +275,7 @@ export async function syncFromFourvps(
.run() .run()
tariffsCount++ tariffsCount++
} }
})()
} }
if (!fetchVpsData) { if (!fetchVpsData) {
+5 -2
View File
@@ -3,7 +3,7 @@
*/ */
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { getCurrentSpaceId, getDb, schema } from '@cfdm/db' import { getCurrentSpaceId, getDb, getSqlite, schema } from '@cfdm/db'
import { findSyncedVps } from '../providers/find-synced-vps.js' import { findSyncedVps } from '../providers/find-synced-vps.js'
import type { RuvdsSyncAccount } from './context.js' import type { RuvdsSyncAccount } from './context.js'
@@ -286,7 +286,9 @@ export async function syncFromRuvds(
let tariffsCount = 0 let tariffsCount = 0
const newTariffs: { name: string; price: string; providerId: string }[] = [] const newTariffs: { name: string; price: string; providerId: string }[] = []
if (fetchTariffs) { // Fetch errors come back as [] — keep the old list instead of wiping it (and re-announcing all as new).
if (fetchTariffs && tariffItems.length > 0) {
getSqlite().transaction(() => {
const existingTariffIds = new Set( const existingTariffIds = new Set(
db db
.select({ id: schema.activeTariffs.id }) .select({ id: schema.activeTariffs.id })
@@ -336,6 +338,7 @@ export async function syncFromRuvds(
.run() .run()
tariffsCount++ tariffsCount++
} }
})()
} }
if (!fetchVpsData) { if (!fetchVpsData) {
+5 -2
View File
@@ -3,7 +3,7 @@
*/ */
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { getCurrentSpaceId, getDb, schema } from '@cfdm/db' import { getCurrentSpaceId, getDb, getSqlite, schema } from '@cfdm/db'
import { findSyncedVps } from '../providers/find-synced-vps.js' import { findSyncedVps } from '../providers/find-synced-vps.js'
import type { UserApiSyncAccount } from './context.js' import type { UserApiSyncAccount } from './context.js'
@@ -274,7 +274,9 @@ export async function syncFromUserApi(
let tariffsCount = 0 let tariffsCount = 0
const newTariffs: { name: string; price: string; providerId: string }[] = [] const newTariffs: { name: string; price: string; providerId: string }[] = []
if (fetchTariffs) { // Fetch errors come back as [] — keep the old list instead of wiping it (and re-announcing all as new).
if (fetchTariffs && tariffItems.length > 0) {
getSqlite().transaction(() => {
const existingTariffIds = new Set( const existingTariffIds = new Set(
db db
.select({ id: schema.activeTariffs.id }) .select({ id: schema.activeTariffs.id })
@@ -324,6 +326,7 @@ export async function syncFromUserApi(
.run() .run()
tariffsCount++ tariffsCount++
} }
})()
} }
if (!fetchVpsData) { if (!fetchVpsData) {
+5 -2
View File
@@ -3,7 +3,7 @@
*/ */
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { getCurrentSpaceId, getDb, schema } from '@cfdm/db' import { getCurrentSpaceId, getDb, getSqlite, schema } from '@cfdm/db'
import { findSyncedVps } from '../providers/find-synced-vps.js' import { findSyncedVps } from '../providers/find-synced-vps.js'
import type { VeespSyncAccount } from './context.js' import type { VeespSyncAccount } from './context.js'
@@ -275,7 +275,9 @@ export async function syncFromVeesp(
let tariffsCount = 0 let tariffsCount = 0
const newTariffs: { name: string; price: string; providerId: string }[] = [] const newTariffs: { name: string; price: string; providerId: string }[] = []
if (fetchTariffs) { // Fetch errors come back as [] — keep the old list instead of wiping it (and re-announcing all as new).
if (fetchTariffs && tariffItems.length > 0) {
getSqlite().transaction(() => {
const existingTariffIds = new Set( const existingTariffIds = new Set(
db db
.select({ id: schema.activeTariffs.id }) .select({ id: schema.activeTariffs.id })
@@ -325,6 +327,7 @@ export async function syncFromVeesp(
.run() .run()
tariffsCount++ tariffsCount++
} }
})()
} }
if (!fetchVpsData) { if (!fetchVpsData) {