diff --git a/apps/backend/src/services/worker-platform/mappers.ts b/apps/backend/src/services/worker-platform/mappers.ts index a3c6f55f..48af63ba 100644 --- a/apps/backend/src/services/worker-platform/mappers.ts +++ b/apps/backend/src/services/worker-platform/mappers.ts @@ -20,7 +20,6 @@ import type { OrderItemRow, OrderRow } from '../../types/repository/rows.js' import { createHttpError } from '../../utils/http.js' import { safeParseJson } from '../admin/admin-query-utils.js' import { - normalizeStoredFileUrl, refreshStoredFileUrl, refreshUploadedFileUrls, } from '../file-storage/file-storage-service.js' @@ -41,6 +40,12 @@ import { normalizeRequirementFields, resolveRequirementFields, } from './work-order-requirement-fields.js' +import { + normalizeStringArray, + normalizeSubmittedFields, + normalizeStatuses, + normalizeUploadedFiles, +} from './worker-input-normalizers.js' export { normalizeRequirementFields, normalizeRequirementFieldsFromPayload, @@ -68,6 +73,12 @@ export { normalizePositiveInteger, normalizeSessionVersion, } from './worker-normalizers.js' +export { + normalizeStringArray, + normalizeSubmittedFields, + normalizeStatuses, + normalizeUploadedFiles, +} from './worker-input-normalizers.js' export { canWorkerAutoAcceptWholeOrder, resolveFreezeDepositAmount, @@ -1088,73 +1099,6 @@ export function matchesOptionalText(ruleValue: unknown, sourceValue: unknown) { ) } -export function normalizeSubmittedFields(value: unknown) { - const source = - value && typeof value === 'object' && !Array.isArray(value) ? (value as JsonObject) : {} - return Object.fromEntries( - Object.entries(source) - .filter(([key]) => !['orderNo', 'platformOrderId', 'fields', 'material'].includes(key)) - .map(([key, currentValue]) => [key, String(currentValue || '').trim()]), - ) -} - -export function normalizeStringArray(value: unknown): string[] { - if (!Array.isArray(value)) return [] - return value.map((item) => String(item || '').trim()).filter(Boolean) -} - -/** - * 将查询参数中的状态过滤解析为去重后的状态数组。 - * 支持:逗号分隔的字符串(如 "open,in_progress")、字符串数组、单个字符串。 - */ -export function normalizeStatuses(value: unknown): string[] { - const rawList: unknown[] = Array.isArray(value) - ? value - : typeof value === 'string' && value.trim() - ? value.split(',') - : value === undefined || value === null - ? [] - : [value] - return [...new Set(rawList.map((item) => String(item || '').trim()).filter(Boolean))] -} - -export function normalizeUploadedFiles(value: unknown) { - if (!Array.isArray(value)) return [] - return value - .map((item) => { - const source = - item && typeof item === 'object' && !Array.isArray(item) ? (item as JsonObject) : {} - const objectKey = String(source.objectKey || source.object_key || '').trim() - const url = String(source.url || '').trim() - if (!objectKey && !url) return null - return { - objectKey, - url: normalizeStoredFileUrl(url, objectKey), - thumbnailUrl: normalizeStoredFileUrl( - source.thumbnailUrl || source.thumbnail_url || url, - objectKey, - ), - mediumUrl: normalizeStoredFileUrl(source.mediumUrl || source.medium_url || url, objectKey), - filename: String(source.filename || '').trim(), - contentType: String(source.contentType || source.content_type || '').trim(), - size: normalizeInteger(source.size, 0), - } - }) - .filter( - ( - item, - ): item is { - objectKey: string - url: string - thumbnailUrl: string - mediumUrl: string - filename: string - contentType: string - size: number - } => Boolean(item), - ) -} - export function mapAcceptanceForResponse(value: unknown) { const acceptance = safeParseJson(value) const files = normalizeUploadedFiles(acceptance.files).map((file) => diff --git a/apps/backend/src/services/worker-platform/worker-input-normalizers.ts b/apps/backend/src/services/worker-platform/worker-input-normalizers.ts new file mode 100644 index 00000000..761b61b6 --- /dev/null +++ b/apps/backend/src/services/worker-platform/worker-input-normalizers.ts @@ -0,0 +1,67 @@ +import type { JsonObject } from '../../types/json.js' +import { normalizeStoredFileUrl } from '../file-storage/file-storage-service.js' +import { normalizeInteger } from './worker-normalizers.js' + +export function normalizeSubmittedFields(value: unknown) { + const source = + value && typeof value === 'object' && !Array.isArray(value) ? (value as JsonObject) : {} + return Object.fromEntries( + Object.entries(source) + .filter(([key]) => !['orderNo', 'platformOrderId', 'fields', 'material'].includes(key)) + .map(([key, currentValue]) => [key, String(currentValue || '').trim()]), + ) +} + +export function normalizeStringArray(value: unknown): string[] { + if (!Array.isArray(value)) return [] + return value.map((item) => String(item || '').trim()).filter(Boolean) +} + +/** 将查询参数中的状态过滤解析为去重后的状态数组。 */ +export function normalizeStatuses(value: unknown): string[] { + const rawList: unknown[] = Array.isArray(value) + ? value + : typeof value === 'string' && value.trim() + ? value.split(',') + : value === undefined || value === null + ? [] + : [value] + return [...new Set(rawList.map((item) => String(item || '').trim()).filter(Boolean))] +} + +export function normalizeUploadedFiles(value: unknown) { + if (!Array.isArray(value)) return [] + return value + .map((item) => { + const source = + item && typeof item === 'object' && !Array.isArray(item) ? (item as JsonObject) : {} + const objectKey = String(source.objectKey || source.object_key || '').trim() + const url = String(source.url || '').trim() + if (!objectKey && !url) return null + return { + objectKey, + url: normalizeStoredFileUrl(url, objectKey), + thumbnailUrl: normalizeStoredFileUrl( + source.thumbnailUrl || source.thumbnail_url || url, + objectKey, + ), + mediumUrl: normalizeStoredFileUrl(source.mediumUrl || source.medium_url || url, objectKey), + filename: String(source.filename || '').trim(), + contentType: String(source.contentType || source.content_type || '').trim(), + size: normalizeInteger(source.size, 0), + } + }) + .filter( + ( + item, + ): item is { + objectKey: string + url: string + thumbnailUrl: string + mediumUrl: string + filename: string + contentType: string + size: number + } => Boolean(item), + ) +}