fix(client): ensure digital employee schema

This commit is contained in:
baiyanyun
2026-06-07 18:39:19 +08:00
parent 6deea42de1
commit 5e76828afe
6 changed files with 196 additions and 164 deletions

View File

@@ -1,7 +1,7 @@
import log from "electron-log";
import { getDeviceId } from "../system/deviceId";
import { getDomainTokenKey } from "@shared/utils/domain";
import { getDb, readSetting } from "../../db";
import { ensureDigitalEmployeeSchema, getDb, readSetting } from "../../db";
const DEFAULT_SYNC_PATH = "/api/digital-employee/sync/outbox";
const MANAGEMENT_SYNC_RECORD_PATH =
@@ -18,6 +18,11 @@ let syncing = false;
let lastSyncAt: string | null = null;
let lastSyncError: string | null = null;
function getDigitalEmployeeDb(): ReturnType<typeof getDb> {
if (!ensureDigitalEmployeeSchema()) return null;
return getDb();
}
interface DigitalEmployeeSyncConfig {
enabled: boolean;
endpoint: string | null;
@@ -296,7 +301,7 @@ function readAuthToken(serverHost: string | null): string | null {
}
function readPendingOutbox(limit: number, force: boolean): OutboxRow[] {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return [];
const candidateLimit = force
? limit
@@ -367,7 +372,7 @@ async function postOutbox(
}
function markRowsSyncing(rows: OutboxRow[], now: string): void {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return;
const tx = db.transaction(() => {
const stmt = db.prepare(`
@@ -385,7 +390,7 @@ function markRowsSynced(
now: string,
sourceRows: OutboxRow[] = [],
): void {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return;
const tx = db.transaction(() => {
const outboxStmt = db.prepare(`
@@ -420,7 +425,7 @@ function recordSyncAttempts(
startedAt: string,
finishedAt: string,
): void {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return;
const tx = db.transaction(() => {
const stmt = db.prepare(`
@@ -454,7 +459,7 @@ function updateEntitySyncStatus(ack: SyncAck, now: string): void {
if (!ack.entity_type || !ack.entity_id) return;
const table = entityTable(ack.entity_type);
if (!table) return;
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return;
db.prepare(`
UPDATE ${table}
@@ -467,7 +472,7 @@ function updateEntitySyncStatus(ack: SyncAck, now: string): void {
}
function markRowsFailed(rows: OutboxRow[], now: string, error: string): void {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return;
const tx = db.transaction(() => {
const stmt = db.prepare(`
@@ -485,7 +490,7 @@ function markRowsFailed(rows: OutboxRow[], now: string, error: string): void {
function markEntityFailed(entityType: string, entityId: string, error: string): void {
const table = entityTable(entityType);
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db || !table) return;
db.prepare(`
UPDATE ${table}
@@ -495,7 +500,7 @@ function markEntityFailed(entityType: string, entityId: string, error: string):
}
function countOutbox(status: string): number {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return 0;
const row = db
.prepare("SELECT COUNT(*) AS count FROM digital_sync_outbox WHERE status = ?")
@@ -504,7 +509,7 @@ function countOutbox(status: string): number {
}
function readFailureSummary(): DigitalEmployeeSyncFailureSummary {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return { total: 0, dueForRetry: 0, backoff: 0, byEntityType: [] };
const rows = db
.prepare(
@@ -544,7 +549,7 @@ function readRecentFailures(
config: DigitalEmployeeSyncConfig,
limit = 3,
): DigitalEmployeeSyncFailure[] {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return [];
const rows = db
.prepare(
@@ -593,7 +598,7 @@ function readAttemptHistory(
outboxId: string,
limit = 5,
): DigitalEmployeeSyncAttempt[] {
const db = getDb();
const db = getDigitalEmployeeDb();
if (!db) return [];
const rows = db
.prepare(