feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认

This commit is contained in:
hugo committed 2026-07-16 08:16:19 +08:00
1 parent b595e157e9
commit 4fbaed2b4c
176 files changed
+14011 -994

No files matched your search

@@ -0,0 +1,87 @@
/**
* features/chat/api/chat.api.spec.ts
* 用途:验证聊天历史接口适配层不会在 hydrate 过程中丢失用户附件快照。
* 依赖:Vitest、mock 后的 shared/api/client 与 auth、chat.api。
* 逻辑:覆盖历史消息过滤 shipping-parse-review、恢复订单识别状态时仍保留 attachments。
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { listChatMessages } from './chat.api';
vi.mock('@/shared/utils/auth', () => ({
getAuthHeaders: vi.fn(() => ({ Authorization: 'Bearer test-token' })),
}));
vi.mock('@/shared/api/client', () => ({
requestJson: vi.fn(),
}));
import { requestJson } from '@/shared/api/client';
const requestJsonMock = vi.mocked(requestJson);
describe('chat.api history hydration', () => {
beforeEach(() => {
requestJsonMock.mockReset();
});
it('keeps user attachment snapshots while hydrating persisted assistant payload', async () => {
requestJsonMock.mockResolvedValue([
{
role: 'user',
content: '',
attachments: [{
id: 'attachment-only-1',
name: 'notice.eml',
type: 'document',
source: 'shipping_notice',
}],
},
{
role: 'assistant',
content: '中间态消息',
type: 'shipping-parse-review',
},
{
role: 'assistant',
content: '订单识别完成',
payload: {
shipping_order_extraction_state: {
status: 'completed',
phase: 'result_persisted',
message: '已创建 1 个任务',
created_count: 1,
opened_existing_count: 0,
pending_count: 0,
non_shipping_count: 0,
created_with_missing_fields_count: 0,
batch_no: 'SB-HISTORY-001',
terminal: true,
},
},
},
]);
const messages = await listChatMessages('conv-history-1', 24);
expect(requestJsonMock).toHaveBeenCalledWith(
'/api/chat/messages?conversation_id=conv-history-1&agent_id=24',
{ headers: { Authorization: 'Bearer test-token' } },
);
expect(messages).toHaveLength(2);
expect(messages[0]).toEqual({
role: 'user',
content: '',
attachments: [{
id: 'attachment-only-1',
name: 'notice.eml',
type: 'document',
source: 'shipping_notice',
}],
});
expect(messages[1]?.shipping_order_extraction_state).toMatchObject({
status: 'completed',
batch_no: 'SB-HISTORY-001',
terminal: true,
});
});
});
+25 -2
View File
@@ -1,6 +1,8 @@
/**
* features/chat/api/chat.api.ts
* 聊天域 HTTP API:会话、消息、源文件、附件上传。
* 用途:封装聊天会话、消息、源文件与附件上传 HTTP API,并恢复结构化历史消息状态。
* 依赖:shared/api/client、鉴权工具、chat.types。
* 逻辑:页面 composable 调用本模块;历史消息响应在返回前将 ChatLog.payload 中的领域状态投影到 ChatMessage。
*/
import { getAuthHeaders } from '@/shared/utils/auth';
import { requestJson } from '@/shared/api/client';
@@ -14,6 +16,7 @@ import type {
ChatSourceFileResult,
ChatUploadResult,
OaEmployeesResponse,
ShippingOrderExtractionState,
} from '../types/chat.types';
const auth = () => ({ headers: getAuthHeaders() });
@@ -28,10 +31,30 @@ export const deleteAllChatSessions = (agentId: number) =>
export const deleteChatSession = (conversationId: string, agentId: number) =>
requestJson(`/api/chat/sessions/${conversationId}?agent_id=${agentId}`, { method: 'DELETE', ...auth() });
/**
* 从 ChatLog.payload 恢复订单识别终态,保证刷新会话后与实时 SSE 消息形状一致。
* 核心逻辑:仅接受带合法 status 的对象;服务端仍是终态事实源,本函数不推导或改写计数。
* @param message - 历史接口返回的单条消息。
* @returns 原消息或已补充 shipping_order_extraction_state 的消息。
*/
export const hydrateShippingOrderExtractionState = (message: ChatMessage): ChatMessage => {
const payload = message.payload && typeof message.payload === 'object'
? message.payload as Record<string, unknown>
: {};
const state = payload.shipping_order_extraction_state;
if (!state || typeof state !== 'object') return message;
const status = String((state as Record<string, unknown>).status || '');
if (!['extracting', 'completed', 'partially_completed', 'failed'].includes(status)) return message;
message.shipping_order_extraction_state = state as ShippingOrderExtractionState;
return message;
};
export const listChatMessages = async (conversationId: string, agentId: number) => {
const messages = await requestJson<ChatMessage[]>(`/api/chat/messages?conversation_id=${conversationId}&agent_id=${agentId}`, auth());
return Array.isArray(messages)
? messages.filter((message) => message?.type !== 'shipping-parse-review')
? messages
.filter((message) => message?.type !== 'shipping-parse-review')
.map(hydrateShippingOrderExtractionState)
: messages;
};
@@ -20,6 +20,7 @@ import type {
ShippingTaskCreateResponse,
ShippingTaskDetail,
ShippingTaskSummary,
ShippingCustomerOption,
ShippingTaskUiState,
} from '../types/shipping.api.types';
@@ -154,6 +155,20 @@ export async function confirmWorkspaceShippingInventorySource(taskNo: string, so
return assertShippingOk(body);
}
/** 搜索可供业务用户确认的启用标准客户。 */
export const listWorkspaceShippingCustomerOptions = (query = '') => {
const params = new URLSearchParams({ limit: '200' });
if (query.trim()) params.set('q', query.trim());
return requestJson<ShippingCustomerOption[]>(`/api/workspace/shipping-customers?${params.toString()}`);
};
/** 为任务确认标准客户;确认成功后响应包含正式业务编号。 */
export const confirmWorkspaceShippingCustomer = (taskNo: string, customerId: number) =>
requestJson<ShippingTaskDetail>(
`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}/confirm-customer`,
jsonPost({ customer_id: customerId }),
);
/**
* 提交库存字段映射。
* @param taskNo - 发货任务编号