import { activateSourceSettingsRevision, createSourceSettingsRevision, fetchSourceSettingsRevisions, } from './sourceConnections' import type { SourceConnectionListItem, SourceSettingsRevision } from './sourceConnections' export function configuredInterval(connection: SourceConnectionListItem, revisions: SourceSettingsRevision[]): number { const active = revisions.find((item) => item.state === 'active') if (active) { const seconds = Number(active.refresh_interval_seconds ?? 0) if (!Number.isInteger(seconds) || seconds < 0) throw new Error('刷新周期无效,未修改设置') return seconds } if (connection.schedule_configured) throw new Error(`${connection.name}:无法读取活动刷新策略,未修改设置`) return 0 } export async function saveSourceRefresh( connection: SourceConnectionListItem, enabled: boolean, interval?: number ): Promise { const seconds = interval ?? configuredInterval(connection, await fetchSourceSettingsRevisions(connection.id)) if (!enabled && connection.source_type === 'manual') throw new Error('手工来源不支持自动刷新') const created = await createSourceSettingsRevision(connection.id, { source_type: connection.source_type, name: connection.name, endpoint: '', credential_ref: '', enabled, schedule: { refresh_interval_seconds: seconds > 0 ? seconds : null }, }) await activateSourceSettingsRevision(connection.id, created.id) }