diff --git a/apps/backend/src/services/scheduler/scheduler-realtime-scope.test.ts b/apps/backend/src/services/scheduler/scheduler-realtime-scope.test.ts new file mode 100644 index 00000000..92570a1a --- /dev/null +++ b/apps/backend/src/services/scheduler/scheduler-realtime-scope.test.ts @@ -0,0 +1,30 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import { isFinancePipelineRealtimeJobId } from './scheduler-service.js' + +test('只有财务履约管道任务允许广播 fulfillment_pipeline.changed', () => { + for (const jobId of [ + 'channel-assign', + 'carrier-dispatch', + 'product-normalize', + 'cost-snapshot', + 'sales-facts', + 'ledger-post', + ]) { + assert.equal(isFinancePipelineRealtimeJobId(jobId), true) + } + + for (const jobId of [ + 'voucher-consume-dispatch', + 'pdd_order_sync', + 'work-order-timeout', + 'deposit-unfreeze', + 'order-service-auto-publish', + 'data-retention', + '', + null, + ]) { + assert.equal(isFinancePipelineRealtimeJobId(jobId), false) + } +}) diff --git a/apps/backend/src/services/scheduler/scheduler-service.ts b/apps/backend/src/services/scheduler/scheduler-service.ts index 441d739c..17fd0a0e 100644 --- a/apps/backend/src/services/scheduler/scheduler-service.ts +++ b/apps/backend/src/services/scheduler/scheduler-service.ts @@ -20,6 +20,18 @@ import { publishRealtimeEvent } from '../realtime/realtime-event-service.js' const timers = new Map() const jobStates = new Map() +const FINANCE_PIPELINE_REALTIME_JOB_IDS = new Set([ + 'channel-assign', + 'carrier-dispatch', + 'product-normalize', + 'cost-snapshot', + 'sales-facts', + 'ledger-post', +]) + +export function isFinancePipelineRealtimeJobId(jobId: unknown) { + return FINANCE_PIPELINE_REALTIME_JOB_IDS.has(String(jobId || '').trim()) +} export function startScheduledJobs() { reloadScheduledJobs() @@ -204,13 +216,15 @@ async function runJob(job: JsonObject, { manual = false }: { manual?: boolean } } function publishPipelineRealtimeChange(jobId: unknown, phase: string) { + const normalizedJobId = String(jobId || '').trim() + if (!isFinancePipelineRealtimeJobId(normalizedJobId)) return publishRealtimeEvent({ type: 'fulfillment_pipeline.changed', entityId: 0, scopes: ['admin_fulfillment_pipelines'], operation: 'refresh', data: { - jobId: String(jobId || '').trim(), + jobId: normalizedJobId, phase: String(phase || '').trim(), }, admin: true, diff --git a/apps/frontend/src/layouts/AdminLayout.tsx b/apps/frontend/src/layouts/AdminLayout.tsx index 6fdb164e..63cf4060 100644 --- a/apps/frontend/src/layouts/AdminLayout.tsx +++ b/apps/frontend/src/layouts/AdminLayout.tsx @@ -564,7 +564,6 @@ function applyAdminRealtimeEvent( event.scopes.includes('admin_fulfillment_pipelines') ) { void queryClient.invalidateQueries({ queryKey: ['fulfillment-pipelines'] }) - void queryClient.invalidateQueries({ queryKey: ['fulfillment-pending'] }) } if (event.type === 'work_order.changed' && event.scopes.includes('admin_work_orders')) { diff --git a/apps/frontend/src/pages/admin/AdminFulfillmentChannelPage.tsx b/apps/frontend/src/pages/admin/AdminFulfillmentChannelPage.tsx index 27e5c15c..7b558417 100644 --- a/apps/frontend/src/pages/admin/AdminFulfillmentChannelPage.tsx +++ b/apps/frontend/src/pages/admin/AdminFulfillmentChannelPage.tsx @@ -668,7 +668,7 @@ function PendingPanel() { rowKey(row)} size="small" - loading={query.isFetching} + loading={query.isLoading} dataSource={query.data?.data?.items || []} columns={columns} pagination={buildAdminTablePagination({ diff --git a/apps/frontend/tests/fulfillment-realtime-flow.test.ts b/apps/frontend/tests/fulfillment-realtime-flow.test.ts index 8b6ebb79..6771ebb1 100644 --- a/apps/frontend/tests/fulfillment-realtime-flow.test.ts +++ b/apps/frontend/tests/fulfillment-realtime-flow.test.ts @@ -26,6 +26,12 @@ test('履约管道状态由后台 SSE 失效刷新,不再 30 秒轮询', () => assert.match(page, /SSE\s+自动刷新/) assert.ok(layout.includes("event.type === 'fulfillment_pipeline.changed'")) assert.ok(layout.includes("queryKey: ['fulfillment-pipelines']")) + const pipelineRealtimeBlock = layout.slice( + layout.indexOf("event.type === 'fulfillment_pipeline.changed'"), + layout.indexOf("event.type === 'work_order.changed'"), + ) + assert.ok(!pipelineRealtimeBlock.includes("queryKey: ['fulfillment-pending']")) + assert.ok(page.includes('loading={query.isLoading}')) }) test('公共订单 SSE 允许匿名连接且只携带重读信号', () => {