107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
|
|
|
|
export type CoLoadGroupStatus = 'ACTIVE' | 'LOCKED' | 'CONSUMED' | 'CANCELLED'
|
|
|
|
export interface CoLoadGroupMember {
|
|
tenantId: string
|
|
orderId: string
|
|
inboundId: string
|
|
fullPackageCount: number
|
|
}
|
|
|
|
export interface CoLoadGroup {
|
|
groupId: string
|
|
groupNo: string
|
|
status: CoLoadGroupStatus
|
|
warehouseId: string
|
|
channelCode: string
|
|
lockedPlanId: string | null
|
|
version: number
|
|
members: CoLoadGroupMember[]
|
|
fullPackageCount: number
|
|
createdAt: string
|
|
updatedAt: string
|
|
}
|
|
|
|
export interface CoLoadBusinessMember {
|
|
inboundCode: string
|
|
}
|
|
|
|
export interface CoLoadBusinessSummary {
|
|
status: CoLoadGroupStatus
|
|
memberCount: number
|
|
members: CoLoadBusinessMember[]
|
|
}
|
|
|
|
type RawId = string | number
|
|
type RawCoLoadGroupMember = Omit<CoLoadGroupMember, 'tenantId' | 'orderId' | 'inboundId'> & {
|
|
tenantId: RawId
|
|
orderId: RawId
|
|
inboundId: RawId
|
|
}
|
|
type RawCoLoadGroup = Omit<CoLoadGroup, 'groupId' | 'warehouseId' | 'lockedPlanId' | 'members'> & {
|
|
groupId: RawId
|
|
warehouseId: RawId
|
|
lockedPlanId: RawId | null
|
|
members: RawCoLoadGroupMember[]
|
|
}
|
|
type RawCoLoadBusinessSummary = Omit<CoLoadBusinessSummary, 'memberCount' | 'members'> & {
|
|
memberCount: number | string
|
|
members: Array<{ inboundCode?: string | null }>
|
|
}
|
|
|
|
const basePath = '/platform/v1/first-leg/coload-groups'
|
|
const optionalId = (value: RawId | null) => value == null ? null : String(value)
|
|
const operationId = (prefix: string) => `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
|
|
|
function normalizeGroup(raw: RawCoLoadGroup): CoLoadGroup {
|
|
return {
|
|
...raw,
|
|
groupId: String(raw.groupId),
|
|
warehouseId: String(raw.warehouseId),
|
|
lockedPlanId: optionalId(raw.lockedPlanId),
|
|
version: Number(raw.version),
|
|
fullPackageCount: Number(raw.fullPackageCount),
|
|
members: (raw.members || []).map((member) => ({
|
|
...member,
|
|
tenantId: String(member.tenantId),
|
|
orderId: String(member.orderId),
|
|
inboundId: String(member.inboundId),
|
|
fullPackageCount: Number(member.fullPackageCount),
|
|
})),
|
|
}
|
|
}
|
|
|
|
export async function createPlatformCoLoadGroup(orderIds: string[]) {
|
|
const response = await http.post<ApiEnvelope<RawCoLoadGroup>, ApiEnvelope<RawCoLoadGroup>>(
|
|
basePath,
|
|
{ orderIds },
|
|
{ headers: { 'Idempotency-Key': operationId('admin-coload-create') } },
|
|
)
|
|
return normalizeGroup(unwrapApiResponse(response))
|
|
}
|
|
|
|
export async function cancelPlatformCoLoadGroup(inboundCode: string, reason?: string | null) {
|
|
const response = await http.post<ApiEnvelope<RawCoLoadGroup>, ApiEnvelope<RawCoLoadGroup>>(
|
|
`${basePath}/by-inbound-code/${encodeURIComponent(inboundCode)}/cancel`,
|
|
{ reason: reason?.trim() || null },
|
|
{ headers: { 'Idempotency-Key': operationId('admin-coload-cancel') } },
|
|
)
|
|
return normalizeGroup(unwrapApiResponse(response))
|
|
}
|
|
|
|
export async function getPlatformCoLoadGroupByInboundCode(inboundCode: string) {
|
|
const response = await http.get<ApiEnvelope<RawCoLoadBusinessSummary>, ApiEnvelope<RawCoLoadBusinessSummary>>(
|
|
`${basePath}/by-inbound-code/${encodeURIComponent(inboundCode)}`,
|
|
)
|
|
const raw = unwrapApiResponse(response)
|
|
const members = (raw.members || []).map((member) => ({
|
|
inboundCode: typeof member.inboundCode === 'string' ? member.inboundCode.trim() : '',
|
|
}))
|
|
return {
|
|
status: raw.status,
|
|
memberCount: Number(raw.memberCount),
|
|
members,
|
|
} satisfies CoLoadBusinessSummary
|
|
}
|