feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认
This commit is contained in:
1 parent
b595e157e9
commit
4fbaed2b4c
176 files changed
+14011
-994
No files matched your search
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* features/chat/utils/attachmentCard.utils.spec.ts
|
||||
* 用途:验证附件卡片名称、大小、状态与删除约束的纯函数映射。
|
||||
* 依赖:Vitest、attachmentCard.utils。
|
||||
* 逻辑:用普通附件、远程 URL 和发货通知覆盖用户可见的关键状态分支。
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { PendingAttachment, ShippingNoticePendingAttachment } from '../types/shipping.types';
|
||||
import {
|
||||
createChatMessageAttachmentSnapshots,
|
||||
formatAttachmentSize,
|
||||
getAttachmentCardStatus,
|
||||
getAttachmentExtension,
|
||||
getAttachmentName,
|
||||
getAttachmentSize,
|
||||
isAttachmentRemoveDisabled,
|
||||
} from './attachmentCard.utils';
|
||||
|
||||
/** 创建普通本地附件夹具。 */
|
||||
function localAttachment(status: 'waiting' | 'uploading' | 'uploaded' | 'failed' = 'waiting'): PendingAttachment {
|
||||
return {
|
||||
id: 'local-1',
|
||||
kind: 'local',
|
||||
file: new File(['pdf'], 'purchase-order.pdf', { type: 'application/pdf' }),
|
||||
type: 'document',
|
||||
uploadStatus: status,
|
||||
uploadProgress: status === 'uploaded' ? 100 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
/** 创建发货通知附件夹具。 */
|
||||
function shippingAttachment(
|
||||
patch: Partial<ShippingNoticePendingAttachment> = {},
|
||||
): ShippingNoticePendingAttachment {
|
||||
return {
|
||||
id: 'shipping-1',
|
||||
kind: 'shipping_notice',
|
||||
type: 'document',
|
||||
uploadStatus: 'uploaded',
|
||||
parseStatus: 'parsed',
|
||||
uploadProgress: 100,
|
||||
originalFilename: 'notice.eml',
|
||||
fileSize: 2048,
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
|
||||
describe('attachment card utils', () => {
|
||||
it('formats real file sizes without inventing remote URL sizes', () => {
|
||||
expect(formatAttachmentSize(0)).toBe('0 B');
|
||||
expect(formatAttachmentSize(144.46 * 1024)).toBe('144.46 KB');
|
||||
expect(formatAttachmentSize(2.31 * 1024 * 1024)).toBe('2.31 MB');
|
||||
expect(formatAttachmentSize(undefined)).toBe('');
|
||||
|
||||
const remote: PendingAttachment = {
|
||||
id: 'remote-1', kind: 'remote', url: 'https://example.com/files/report.xlsx', type: 'document',
|
||||
};
|
||||
expect(getAttachmentName(remote)).toBe('report.xlsx');
|
||||
expect(getAttachmentExtension(remote)).toBe('XLSX');
|
||||
expect(getAttachmentSize(remote)).toBe('');
|
||||
});
|
||||
|
||||
it('maps local upload lifecycle and disables removal only while uploading', () => {
|
||||
expect(getAttachmentCardStatus(localAttachment('waiting')).state).toBe('pending');
|
||||
expect(getAttachmentCardStatus(localAttachment('uploading')).state).toBe('uploading');
|
||||
expect(getAttachmentCardStatus(localAttachment('uploaded')).state).toBe('ready');
|
||||
expect(getAttachmentCardStatus(localAttachment('failed')).state).toBe('failed');
|
||||
expect(isAttachmentRemoveDisabled(localAttachment('uploading'))).toBe(true);
|
||||
expect(isAttachmentRemoveDisabled(localAttachment('failed'))).toBe(false);
|
||||
});
|
||||
|
||||
it('preserves shipping parsing, failure and duplicate attention states', () => {
|
||||
expect(getAttachmentCardStatus(shippingAttachment({ parseStatus: 'parsing' })).state).toBe('processing');
|
||||
expect(getAttachmentCardStatus(shippingAttachment({ parseStatus: 'failed' }))).toEqual({
|
||||
state: 'failed', labelKey: 'chat.attachment.parseFailed',
|
||||
});
|
||||
expect(getAttachmentCardStatus(shippingAttachment({
|
||||
duplicate: {
|
||||
status: 'possible_duplicate',
|
||||
blocking: true,
|
||||
fingerprintStrength: 'strong',
|
||||
matches: [],
|
||||
allowedActions: [],
|
||||
},
|
||||
})).state).toBe('attention');
|
||||
expect(getAttachmentCardStatus(shippingAttachment()).state).toBe('ready');
|
||||
});
|
||||
|
||||
it('shows the order extraction lifecycle separately from attachment parsing', () => {
|
||||
expect(getAttachmentCardStatus(shippingAttachment({ orderExtractionStatus: 'extracting' }))).toEqual({
|
||||
state: 'processing', labelKey: 'chat.shippingOrderExtractionProcessing',
|
||||
});
|
||||
expect(getAttachmentCardStatus(shippingAttachment({ orderExtractionStatus: 'partially_completed' }))).toEqual({
|
||||
state: 'attention', labelKey: 'chat.shippingOrderExtractionPartiallyCompleted',
|
||||
});
|
||||
expect(getAttachmentCardStatus(shippingAttachment({ orderExtractionStatus: 'completed' }))).toEqual({
|
||||
state: 'ready', labelKey: 'chat.shippingOrderExtractionCompleted',
|
||||
});
|
||||
expect(getAttachmentCardStatus(shippingAttachment({ orderExtractionStatus: 'failed' }))).toEqual({
|
||||
state: 'failed', labelKey: 'chat.shippingOrderExtractionFailed',
|
||||
});
|
||||
});
|
||||
|
||||
it('creates ordered JSON-safe snapshots for local, remote and shipping attachments', () => {
|
||||
const local = localAttachment('uploaded');
|
||||
if (local.kind === 'local') {
|
||||
local.uploadFileId = 'internal-upload-id';
|
||||
local.errorMessage = 'internal error';
|
||||
}
|
||||
const remote: PendingAttachment = {
|
||||
id: 'remote-secret',
|
||||
kind: 'remote',
|
||||
url: 'https://example.com/files/report.xlsx?signature=secret-token',
|
||||
type: 'document',
|
||||
};
|
||||
const shipping = shippingAttachment({
|
||||
assetNo: 'ASSET-INTERNAL',
|
||||
intakeNo: 'INTAKE-INTERNAL',
|
||||
originalFilename: 'notice.EML',
|
||||
fileSize: 229_877,
|
||||
});
|
||||
|
||||
const snapshots = createChatMessageAttachmentSnapshots([local, remote, shipping]);
|
||||
|
||||
expect(snapshots).toEqual([
|
||||
{
|
||||
id: 'local-1',
|
||||
name: 'purchase-order.pdf',
|
||||
size: 3,
|
||||
extension: 'pdf',
|
||||
type: 'document',
|
||||
source: 'local',
|
||||
},
|
||||
{
|
||||
id: 'remote-secret',
|
||||
name: 'report.xlsx',
|
||||
extension: 'xlsx',
|
||||
type: 'document',
|
||||
source: 'remote',
|
||||
},
|
||||
{
|
||||
id: 'shipping-1',
|
||||
name: 'notice.EML',
|
||||
size: 229_877,
|
||||
extension: 'eml',
|
||||
type: 'document',
|
||||
source: 'shipping_notice',
|
||||
},
|
||||
]);
|
||||
const serialized = JSON.stringify(snapshots);
|
||||
expect(serialized).not.toContain('secret-token');
|
||||
expect(serialized).not.toContain('internal-upload-id');
|
||||
expect(serialized).not.toContain('ASSET-INTERNAL');
|
||||
expect(serialized).not.toContain('INTAKE-INTERNAL');
|
||||
|
||||
if (local.kind === 'local') local.file = new File(['changed'], 'changed.pdf');
|
||||
shipping.originalFilename = 'changed.eml';
|
||||
expect(snapshots[0]?.name).toBe('purchase-order.pdf');
|
||||
expect(snapshots[2]?.name).toBe('notice.EML');
|
||||
});
|
||||
|
||||
it('uses a safe fallback for remote URLs without a filename and never invents size', () => {
|
||||
const snapshots = createChatMessageAttachmentSnapshots([{
|
||||
id: 'remote-root',
|
||||
kind: 'remote',
|
||||
url: 'https://example.com/?signature=hidden',
|
||||
type: 'custom',
|
||||
}]);
|
||||
|
||||
expect(snapshots).toEqual([{
|
||||
id: 'remote-root',
|
||||
name: 'remote-file',
|
||||
type: 'custom',
|
||||
source: 'remote',
|
||||
}]);
|
||||
});
|
||||
|
||||
it('strips path fragments and control characters from snapshot names', () => {
|
||||
const snapshots = createChatMessageAttachmentSnapshots([
|
||||
shippingAttachment({ originalFilename: 'C:\\mail\\notice\u0000.eml' }),
|
||||
]);
|
||||
|
||||
expect(snapshots[0]).toMatchObject({
|
||||
id: 'shipping-1',
|
||||
name: 'notice.eml',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* features/chat/utils/attachmentCard.utils.ts
|
||||
* 用途:统一计算聊天附件卡片的名称、大小、扩展名、状态、删除约束与已发送快照。
|
||||
* 依赖:chat.types 的消息附件契约、shipping.types 的待发送附件联合类型。
|
||||
* 逻辑:ChatInputBar 只负责渲染;发送层通过快照函数把可变待发送附件固化为安全展示数据。
|
||||
*/
|
||||
import type { ChatMessageAttachment } from '../types/chat.types';
|
||||
import type { AttachmentUiStatus, PendingAttachment } from '../types/shipping.types';
|
||||
|
||||
export interface AttachmentCardStatus {
|
||||
state: AttachmentUiStatus;
|
||||
labelKey: string;
|
||||
}
|
||||
|
||||
/** 获取附件卡片文件名;URL 优先展示路径末段。 */
|
||||
export function getAttachmentName(attachment: PendingAttachment): string {
|
||||
if (attachment.kind === 'local') return attachment.file.name;
|
||||
if (attachment.kind === 'shipping_notice') return attachment.originalFilename;
|
||||
try {
|
||||
const pathname = new URL(attachment.url).pathname;
|
||||
return decodeURIComponent(pathname.split('/').filter(Boolean).pop() || attachment.url);
|
||||
} catch {
|
||||
return attachment.url;
|
||||
}
|
||||
}
|
||||
|
||||
/** 将字节数格式化为紧凑的 B/KB/MB/GB 文本;未知大小返回空字符串。 */
|
||||
export function formatAttachmentSize(bytes?: number): string {
|
||||
if (!Number.isFinite(bytes) || bytes === undefined || bytes < 0) return '';
|
||||
if (bytes < 1024) return `${bytes} B`;
|
||||
const units = ['KB', 'MB', 'GB'];
|
||||
let value = bytes / 1024;
|
||||
let unitIndex = 0;
|
||||
while (value >= 1024 && unitIndex < units.length - 1) {
|
||||
value /= 1024;
|
||||
unitIndex += 1;
|
||||
}
|
||||
const formatted = value.toFixed(2).replace(/\.00$/, '').replace(/(\.\d)0$/, '$1');
|
||||
return `${formatted} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
/** 根据附件来源返回真实文件大小;远程 URL 不伪造大小。 */
|
||||
export function getAttachmentSize(attachment: PendingAttachment): string {
|
||||
if (attachment.kind === 'local') return formatAttachmentSize(attachment.file.size);
|
||||
if (attachment.kind === 'shipping_notice') return formatAttachmentSize(attachment.fileSize);
|
||||
return '';
|
||||
}
|
||||
|
||||
/** 根据文件名扩展名生成卡片类型徽标。 */
|
||||
export function getAttachmentExtension(attachment: PendingAttachment): string {
|
||||
const name = getAttachmentName(attachment);
|
||||
const extension = name.includes('.') ? name.split('.').pop()?.toUpperCase() : '';
|
||||
if (extension && extension.length <= 5) return extension;
|
||||
if (attachment.kind === 'remote') return 'URL';
|
||||
return attachment.type.toUpperCase().slice(0, 5);
|
||||
}
|
||||
|
||||
/** 将普通上传状态与发货通知上传/解析状态映射为统一卡片状态。 */
|
||||
export function getAttachmentCardStatus(attachment: PendingAttachment): AttachmentCardStatus {
|
||||
if (attachment.kind === 'remote') return { state: 'ready', labelKey: 'chat.attachment.ready' };
|
||||
if (attachment.kind === 'local') {
|
||||
if (attachment.uploadStatus === 'uploading') return { state: 'uploading', labelKey: 'chat.attachment.uploading' };
|
||||
if (attachment.uploadStatus === 'uploaded') return { state: 'ready', labelKey: 'chat.attachment.ready' };
|
||||
if (attachment.uploadStatus === 'failed') return { state: 'failed', labelKey: 'chat.attachment.uploadFailed' };
|
||||
return { state: 'pending', labelKey: 'chat.attachment.pending' };
|
||||
}
|
||||
if (attachment.uploadStatus === 'uploading') return { state: 'uploading', labelKey: 'chat.attachment.uploading' };
|
||||
if (attachment.uploadStatus === 'failed') return { state: 'failed', labelKey: 'chat.attachment.uploadFailed' };
|
||||
if (attachment.parseStatus === 'queued' || attachment.parseStatus === 'parsing') {
|
||||
return { state: 'processing', labelKey: 'chat.attachment.processing' };
|
||||
}
|
||||
if (attachment.parseStatus === 'failed') return { state: 'failed', labelKey: 'chat.attachment.parseFailed' };
|
||||
const extractionStatus = String(attachment.orderExtractionStatus || '').trim().toLowerCase();
|
||||
if (extractionStatus === 'extracting') {
|
||||
return { state: 'processing', labelKey: 'chat.shippingOrderExtractionProcessing' };
|
||||
}
|
||||
if (extractionStatus === 'partially_completed') {
|
||||
return { state: 'attention', labelKey: 'chat.shippingOrderExtractionPartiallyCompleted' };
|
||||
}
|
||||
if (extractionStatus === 'failed') {
|
||||
return { state: 'failed', labelKey: 'chat.shippingOrderExtractionFailed' };
|
||||
}
|
||||
if (extractionStatus === 'completed') {
|
||||
return { state: 'ready', labelKey: 'chat.shippingOrderExtractionCompleted' };
|
||||
}
|
||||
if (attachment.parseStatus === 'parsed' && !attachment.duplicateResolution?.action) {
|
||||
if (attachment.duplicate?.blocking) {
|
||||
return { state: 'attention', labelKey: 'chat.shippingNoticeDuplicateBlockingBadge' };
|
||||
}
|
||||
if (attachment.duplicate?.status === 'possible_duplicate') {
|
||||
return { state: 'attention', labelKey: 'chat.shippingNoticeDuplicatePossibleBadge' };
|
||||
}
|
||||
}
|
||||
if (attachment.parseStatus === 'parsed' || attachment.parseStatus === 'consumed') {
|
||||
return { state: 'ready', labelKey: 'chat.attachment.ready' };
|
||||
}
|
||||
return { state: 'pending', labelKey: 'chat.attachment.pending' };
|
||||
}
|
||||
|
||||
/** 上传中的普通附件没有取消接口,因此禁止删除直至请求结束。 */
|
||||
export function isAttachmentRemoveDisabled(attachment: PendingAttachment): boolean {
|
||||
return attachment.kind === 'local' && attachment.uploadStatus === 'uploading';
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取可持久化的安全文件名,避免远程 URL 查询参数或临时签名进入聊天记录。
|
||||
* @param attachment - 当前待发送附件
|
||||
* @returns 最长 255 字符的文件名;远程 URL 无有效末段时返回 remote-file
|
||||
*/
|
||||
function getSnapshotAttachmentName(attachment: PendingAttachment): string {
|
||||
let name = '';
|
||||
if (attachment.kind === 'local') {
|
||||
name = attachment.file.name;
|
||||
} else if (attachment.kind === 'shipping_notice') {
|
||||
name = attachment.originalFilename;
|
||||
} else {
|
||||
try {
|
||||
const pathname = new URL(attachment.url).pathname;
|
||||
name = decodeURIComponent(pathname.split('/').filter(Boolean).pop() || '');
|
||||
} catch {
|
||||
name = '';
|
||||
}
|
||||
if (!name) name = 'remote-file';
|
||||
}
|
||||
const basename = String(name || 'attachment')
|
||||
.split(/[\\/]/)
|
||||
.filter(Boolean)
|
||||
.pop() || 'attachment';
|
||||
return basename.replace(/[\u0000-\u001f\u007f]/g, '').trim().slice(0, 255) || 'attachment';
|
||||
}
|
||||
|
||||
/**
|
||||
* 鐢熸垚鍙寔涔呭寲鐨勯檮浠跺揩鐓?ID锛屼紭鍏堝鐢ㄥ緟鍙戦€侀檮浠剁殑鏈湴鏍囪瘑锛岄伩鍏嶅紩鍏?File 鎴栧唴閮ㄨ祫浜ф寚閽堛€?
|
||||
*/
|
||||
function getSnapshotAttachmentId(attachment: PendingAttachment, index: number): string {
|
||||
const candidate = String(attachment.id || '')
|
||||
.replace(/[\u0000-\u001f\u007f]/g, '')
|
||||
.trim()
|
||||
.slice(0, 128);
|
||||
return candidate || `attachment-${index + 1}`;
|
||||
}
|
||||
|
||||
/** 从安全文件名提取小写扩展名;无明确扩展名时不制造类型后缀。 */
|
||||
function getSnapshotAttachmentExtension(name: string): string | undefined {
|
||||
const dotIndex = name.lastIndexOf('.');
|
||||
if (dotIndex <= 0 || dotIndex === name.length - 1) return undefined;
|
||||
const extension = name.slice(dotIndex + 1).trim().toLowerCase();
|
||||
return extension && extension.length <= 16 ? extension : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把待发送附件固化为已发送消息快照。
|
||||
* 核心逻辑:创建全新纯对象并仅复制展示白名单;远程附件不保存 URL 或伪造大小。
|
||||
* @param attachments - 按用户选择顺序排列的待发送附件
|
||||
* @returns 可直接 JSON 序列化、顺序不变的消息附件快照
|
||||
*/
|
||||
export function createChatMessageAttachmentSnapshots(
|
||||
attachments: readonly PendingAttachment[],
|
||||
): ChatMessageAttachment[] {
|
||||
return attachments.map((attachment, index) => {
|
||||
const name = getSnapshotAttachmentName(attachment);
|
||||
const size = attachment.kind === 'local'
|
||||
? attachment.file.size
|
||||
: attachment.kind === 'shipping_notice'
|
||||
? attachment.fileSize
|
||||
: undefined;
|
||||
const extension = getSnapshotAttachmentExtension(name);
|
||||
return {
|
||||
id: getSnapshotAttachmentId(attachment, index),
|
||||
name,
|
||||
type: attachment.type,
|
||||
source: attachment.kind,
|
||||
...(extension ? { extension } : {}),
|
||||
...(Number.isFinite(size) && size !== undefined && size >= 0
|
||||
? { size: Math.round(size) }
|
||||
: {}),
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user