fix(sync): не стирать тарифы при ошибке загрузки прайс-листа
Ошибка запроса тарифов превращалась в пустой список: active_tariffs удалялись, а следующий успешный синк объявлял все тарифы новыми. Пустой ответ теперь оставляет старый список, замена идёт в транзакции. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01a771407d
commit
78f413324a
6 files changed
+34
-10
No files matched your search
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
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 type { BillmanagerSyncAccount } from './context.js'
|
||||
@@ -355,7 +355,9 @@ export async function syncFromBillmanager(
|
||||
|
||||
let tariffsCount = 0
|
||||
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(
|
||||
db
|
||||
.select({ id: schema.activeTariffs.id })
|
||||
@@ -423,6 +425,7 @@ export async function syncFromBillmanager(
|
||||
})
|
||||
.run()
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
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)
|
||||
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([])
|
||||
})
|
||||
})
|
||||
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
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 type { FourvpsSyncAccount } from './context.js'
|
||||
@@ -223,7 +223,9 @@ export async function syncFromFourvps(
|
||||
|
||||
let tariffsCount = 0
|
||||
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(
|
||||
db
|
||||
.select({ id: schema.activeTariffs.id })
|
||||
@@ -273,6 +275,7 @@ export async function syncFromFourvps(
|
||||
.run()
|
||||
tariffsCount++
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
if (!fetchVpsData) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
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 type { RuvdsSyncAccount } from './context.js'
|
||||
@@ -286,7 +286,9 @@ export async function syncFromRuvds(
|
||||
|
||||
let tariffsCount = 0
|
||||
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(
|
||||
db
|
||||
.select({ id: schema.activeTariffs.id })
|
||||
@@ -336,6 +338,7 @@ export async function syncFromRuvds(
|
||||
.run()
|
||||
tariffsCount++
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
if (!fetchVpsData) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
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 type { UserApiSyncAccount } from './context.js'
|
||||
@@ -274,7 +274,9 @@ export async function syncFromUserApi(
|
||||
|
||||
let tariffsCount = 0
|
||||
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(
|
||||
db
|
||||
.select({ id: schema.activeTariffs.id })
|
||||
@@ -324,6 +326,7 @@ export async function syncFromUserApi(
|
||||
.run()
|
||||
tariffsCount++
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
if (!fetchVpsData) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
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 type { VeespSyncAccount } from './context.js'
|
||||
@@ -275,7 +275,9 @@ export async function syncFromVeesp(
|
||||
|
||||
let tariffsCount = 0
|
||||
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(
|
||||
db
|
||||
.select({ id: schema.activeTariffs.id })
|
||||
@@ -325,6 +327,7 @@ export async function syncFromVeesp(
|
||||
.run()
|
||||
tariffsCount++
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
if (!fetchVpsData) {
|
||||
|
||||
Reference in new issue
Block a user