fix(client): sync digital approval decisions
This commit is contained in:
@@ -75,6 +75,66 @@ describe("digital employee state service", () => {
|
||||
status: "pending",
|
||||
});
|
||||
});
|
||||
|
||||
it("updates formal approval records when UI approval decisions are saved", async () => {
|
||||
const { saveDigitalEmployeeUiState } = await import("./stateService");
|
||||
mockState.db?.approvals.set("approval-1", {
|
||||
id: "approval-1",
|
||||
plan_id: "plan-1",
|
||||
task_id: "task-1",
|
||||
run_id: "run-1",
|
||||
title: "确认发送日报给管理端",
|
||||
status: "pending",
|
||||
payload: JSON.stringify({ source: "test" }),
|
||||
sync_status: "synced",
|
||||
created_at: "2026-06-07T07:00:00.000Z",
|
||||
updated_at: "2026-06-07T07:00:00.000Z",
|
||||
});
|
||||
|
||||
saveDigitalEmployeeUiState({
|
||||
action: "decide_approval",
|
||||
date: "2026-06-07",
|
||||
metadata: {
|
||||
decision_id: "approval-1",
|
||||
decision: "approved",
|
||||
},
|
||||
state: {
|
||||
selectedDutyIdsByDate: {},
|
||||
confirmedDates: {},
|
||||
reportScheduleTime: "18:00",
|
||||
reportGeneratedAtByDate: {},
|
||||
decisionResultsByDate: {
|
||||
"2026-06-07": {
|
||||
"approval-1": "approved",
|
||||
},
|
||||
},
|
||||
profile: {
|
||||
name: "飞天数字员工",
|
||||
company: "qimingclaw 客户端",
|
||||
position: "客户端任务执行员",
|
||||
currentStatus: "working",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockState.db?.approvals.get("approval-1")).toMatchObject({
|
||||
id: "approval-1",
|
||||
status: "approved",
|
||||
sync_status: "pending",
|
||||
updated_at: "2026-06-07T08:00:00.000Z",
|
||||
});
|
||||
expect(JSON.parse(mockState.db?.approvals.get("approval-1")?.payload ?? "{}")).toMatchObject({
|
||||
source: "test",
|
||||
decision: "approved",
|
||||
decidedAt: "2026-06-07T08:00:00.000Z",
|
||||
previousStatus: "pending",
|
||||
});
|
||||
expect(mockState.db?.outbox.get("approval:upsert:approval-1")).toMatchObject({
|
||||
entity_type: "approval",
|
||||
entity_id: "approval-1",
|
||||
status: "pending",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
interface TestArtifactRow {
|
||||
@@ -133,11 +193,18 @@ class TestDb {
|
||||
const normalized = sql.replace(/\s+/g, " ").trim();
|
||||
return {
|
||||
all: () => [],
|
||||
get: () => ({ count: 0 }),
|
||||
get: (...args: unknown[]) => this.get(normalized, args),
|
||||
run: (...args: unknown[]) => this.run(normalized, args),
|
||||
};
|
||||
}
|
||||
|
||||
private get(sql: string, args: unknown[]): unknown {
|
||||
if (sql.startsWith("SELECT id, plan_id, task_id, run_id, title, status, payload, created_at FROM digital_approvals")) {
|
||||
return this.approvals.get(args[0] as string);
|
||||
}
|
||||
return { count: 0 };
|
||||
}
|
||||
|
||||
private run(sql: string, args: unknown[]): unknown {
|
||||
if (sql.startsWith("INSERT INTO digital_plans")) return { changes: 1 };
|
||||
if (sql.startsWith("INSERT INTO digital_tasks")) return { changes: 1 };
|
||||
@@ -184,6 +251,7 @@ class TestDb {
|
||||
string,
|
||||
string,
|
||||
];
|
||||
const previous = this.approvals.get(id);
|
||||
this.approvals.set(id, {
|
||||
id,
|
||||
plan_id: planId,
|
||||
@@ -192,8 +260,8 @@ class TestDb {
|
||||
title,
|
||||
status,
|
||||
payload,
|
||||
sync_status: "pending",
|
||||
created_at: createdAt,
|
||||
sync_status: previous?.sync_status === "synced" ? "pending" : previous?.sync_status ?? "pending",
|
||||
created_at: previous?.created_at ?? createdAt,
|
||||
updated_at: updatedAt,
|
||||
});
|
||||
return { changes: 1 };
|
||||
|
||||
@@ -337,6 +337,7 @@ export function saveDigitalEmployeeUiState(
|
||||
): DigitalEmployeeUiState {
|
||||
const state = normalizeUiState(update.state);
|
||||
writeSetting(UI_STATE_KEY, state);
|
||||
recordUiApprovalDecision(update);
|
||||
if (update.action) {
|
||||
recordDigitalEmployeeRuntimeEvent({
|
||||
kind: `digital_workday_${update.action}`,
|
||||
@@ -353,6 +354,59 @@ export function saveDigitalEmployeeUiState(
|
||||
return state;
|
||||
}
|
||||
|
||||
function recordUiApprovalDecision(update: DigitalEmployeeUiStateUpdate): void {
|
||||
if (update.action !== "decide_approval") return;
|
||||
const metadata = objectRecord(update.metadata);
|
||||
const decisionId = stringValue(metadata?.decision_id);
|
||||
const decision = stringValue(metadata?.decision);
|
||||
if (!decisionId || !decision) return;
|
||||
|
||||
const db = getDb();
|
||||
if (!db) return;
|
||||
const existing = db.prepare(`
|
||||
SELECT id, plan_id, task_id, run_id, title, status, payload, created_at
|
||||
FROM digital_approvals
|
||||
WHERE id = ?
|
||||
`).get(decisionId) as Record<string, unknown> | undefined;
|
||||
if (!existing) return;
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const previousStatus = stringField(existing, "status") || "pending";
|
||||
const storedPayload = parseStoredPayload(existing["payload"]);
|
||||
const storedPayloadRecord = objectRecord(storedPayload);
|
||||
const payload = storedPayloadRecord
|
||||
? {
|
||||
...storedPayloadRecord,
|
||||
decision,
|
||||
decidedAt: now,
|
||||
previousStatus,
|
||||
}
|
||||
: {
|
||||
value: storedPayload,
|
||||
decision,
|
||||
decidedAt: now,
|
||||
previousStatus,
|
||||
};
|
||||
upsertApproval({
|
||||
id: decisionId,
|
||||
planId: stringField(existing, "plan_id"),
|
||||
taskId: stringField(existing, "task_id"),
|
||||
runId: stringField(existing, "run_id"),
|
||||
title: stringField(existing, "title") || "待确认事项",
|
||||
status: approvalDecisionStatus(decision),
|
||||
payload,
|
||||
now,
|
||||
});
|
||||
}
|
||||
|
||||
function approvalDecisionStatus(decision: string): string {
|
||||
const normalized = decision.toLowerCase();
|
||||
if (["approve", "approved", "accept", "accepted"].includes(normalized)) return "approved";
|
||||
if (["reject", "rejected", "deny", "denied"].includes(normalized)) return "rejected";
|
||||
if (["dismiss", "dismissed", "skip", "skipped"].includes(normalized)) return "dismissed";
|
||||
return "completed";
|
||||
}
|
||||
|
||||
export function readDigitalEmployeeRuntimeRecords(
|
||||
limit = 120,
|
||||
): DigitalEmployeeRuntimeRecords {
|
||||
|
||||
Reference in New Issue
Block a user