This commit is contained in:
2026-08-04 21:01:39 +08:00
parent 88bd939170
commit 5cb1ca307d
77 changed files with 9914 additions and 131 deletions
@@ -0,0 +1,112 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { http } from '@/shared/api/http'
import {
changePlatformLocationStatus,
createPlatformLocation,
deletePlatformLocation,
getPlatformLocation,
listPlatformLocations,
updatePlatformLocation,
type SavePlatformLocationRequest,
} from './platformLocations'
vi.mock('@/shared/api/http', () => ({
http: { get: vi.fn(), post: vi.fn(), put: vi.fn(), patch: vi.fn(), delete: vi.fn() },
unwrapApiResponse: <T>(response: { data: T }) => response.data,
}))
const payload: SavePlatformLocationRequest = {
warehouseId: '190000000000000101',
zoneCode: 'A',
zoneName: '常规区',
locationCode: 'A1-01',
locationName: 'A1 区 01 位',
locationType: 'SHELF',
maxWeightKg: 500,
status: 'AVAILABLE',
remark: null,
version: 3,
}
describe('platform location API', () => {
beforeEach(() => {
vi.mocked(http.get).mockReset()
vi.mocked(http.post).mockReset()
vi.mocked(http.put).mockReset()
vi.mocked(http.patch).mockReset()
vi.mocked(http.delete).mockReset()
})
it('uses the implemented paged list and detail contracts', async () => {
const page = { records: [], page: '2', size: '20', total: '21', pages: '2' }
const detail = { location: { id: '19001' }, warehouseCode: 'CN-YW-01', warehouseName: '义乌仓' }
vi.mocked(http.get)
.mockResolvedValueOnce({ code: '200', message: 'success', data: page })
.mockResolvedValueOnce({ code: '200', message: 'success', data: detail })
await expect(listPlatformLocations({
page: 2,
size: 20,
tenantId: '9223372036854775806',
warehouseId: '190000000000000101',
zoneCode: ' A ',
locationCode: ' A1 ',
locationType: 'SHELF',
status: 'AVAILABLE',
})).resolves.toEqual({ records: [], page: 2, size: 20, total: 21, pages: 2 })
await expect(getPlatformLocation('9223372036854775806', '190000000000000101', '19001')).resolves.toEqual(detail)
expect(http.get).toHaveBeenNthCalledWith(1, '/platform/v1/locations', {
params: {
tenantId: '9223372036854775806', page: 2, size: 20, warehouseId: '190000000000000101',
zoneCode: 'A', locationCode: 'A1', locationType: 'SHELF', status: 'AVAILABLE',
},
})
expect(http.get).toHaveBeenNthCalledWith(2, '/platform/v1/locations/19001', {
params: { tenantId: '9223372036854775806', warehouseId: '190000000000000101' },
})
})
it('omits empty customer, warehouse, and filter values from all-customer list requests', async () => {
const page = { records: [], page: 1, size: 20, total: 0, pages: 0 }
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: page })
await expect(listPlatformLocations({
page: 1,
size: 20,
tenantId: '',
warehouseId: '',
zoneCode: '',
locationCode: '',
locationType: '',
status: '',
})).resolves.toEqual(page)
expect(http.get).toHaveBeenCalledWith('/platform/v1/locations', {
params: { page: 1, size: 20 },
})
})
it('sends versioned create, update, status, and delete requests', async () => {
vi.mocked(http.post).mockResolvedValue({ code: '200', message: 'success', data: '19001' })
vi.mocked(http.put).mockResolvedValue({ code: '200', message: 'success', data: null })
vi.mocked(http.patch).mockResolvedValue({ code: '200', message: 'success', data: null })
vi.mocked(http.delete).mockResolvedValue({ code: '200', message: 'success', data: null })
await expect(createPlatformLocation(10001, payload)).resolves.toBe('19001')
await expect(updatePlatformLocation(10001, payload.warehouseId, '19001', payload)).resolves.toBeNull()
await expect(changePlatformLocationStatus(10001, payload.warehouseId, '19001', 'DISABLED', 3)).resolves.toBeNull()
await expect(deletePlatformLocation(10001, payload.warehouseId, '19001', 4)).resolves.toBeNull()
expect(http.post).toHaveBeenCalledWith('/platform/v1/locations', payload, { params: { tenantId: 10001 } })
expect(http.put).toHaveBeenCalledWith('/platform/v1/locations/19001', payload, {
params: { tenantId: 10001, warehouseId: payload.warehouseId },
})
expect(http.patch).toHaveBeenCalledWith('/platform/v1/locations/19001/status', { status: 'DISABLED', version: 3 }, {
params: { tenantId: 10001, warehouseId: payload.warehouseId },
})
expect(http.delete).toHaveBeenCalledWith('/platform/v1/locations/19001', {
params: { tenantId: 10001, warehouseId: payload.warehouseId, version: 4 },
})
})
})
@@ -0,0 +1,148 @@
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
export type PlatformLocationStatus = 'AVAILABLE' | 'RESERVED' | 'OCCUPIED' | 'DISABLED'
export type EditablePlatformLocationStatus = 'AVAILABLE' | 'DISABLED'
export type PlatformLocationType = 'SHELF' | 'PALLET' | 'FLOOR' | 'FLOW_RACK'
export interface PlatformLocationResource {
id: number | string
tenantId: number | string
warehouseId: number | string
warehouseCode?: string | null
warehouseName?: string | null
zoneCode: string
zoneName: string
locationCode: string
locationName: string | null
locationType: PlatformLocationType
maxWeightKg: number | string | null
sharedOverflow: boolean | null
status: PlatformLocationStatus
remark: string | null
version: number
createdAt?: string | null
updatedAt?: string | null
}
export interface PlatformLocationDetail {
location: PlatformLocationResource
warehouseCode: string
warehouseName: string
}
export interface PlatformLocationPage {
records: PlatformLocationResource[]
page: number
size: number
total: number
pages: number
}
export interface PlatformLocationQuery {
page?: number
size?: number
tenantId?: number | string
warehouseId?: number | string
zoneCode?: string
locationCode?: string
locationType?: PlatformLocationType | ''
status?: PlatformLocationStatus | ''
}
export interface SavePlatformLocationRequest {
warehouseId: number | string
zoneCode: string
zoneName: string
locationCode: string
locationName: string | null
locationType: PlatformLocationType
maxWeightKg: number | null
status: EditablePlatformLocationStatus
remark: string | null
version?: number
}
const basePath = '/platform/v1/locations'
export async function listPlatformLocations(query: PlatformLocationQuery) {
const zoneCode = query.zoneCode?.trim()
const locationCode = query.locationCode?.trim()
const response = await http.get<ApiEnvelope<PlatformLocationPage>, ApiEnvelope<PlatformLocationPage>>(basePath, {
params: {
page: query.page ?? 1,
size: query.size ?? 20,
...(query.tenantId ? { tenantId: query.tenantId } : {}),
...(query.warehouseId ? { warehouseId: query.warehouseId } : {}),
...(zoneCode ? { zoneCode } : {}),
...(locationCode ? { locationCode } : {}),
...(query.locationType ? { locationType: query.locationType } : {}),
...(query.status ? { status: query.status } : {}),
},
})
const result = unwrapApiResponse(response)
return {
...result,
page: Number(result.page),
size: Number(result.size),
total: Number(result.total),
pages: Number(result.pages),
}
}
export async function getPlatformLocation(
tenantId: number | string,
warehouseId: number | string,
id: number | string,
) {
const response = await http.get<ApiEnvelope<PlatformLocationDetail>, ApiEnvelope<PlatformLocationDetail>>(
`${basePath}/${id}`,
{ params: { tenantId, warehouseId } },
)
return unwrapApiResponse(response)
}
export async function createPlatformLocation(tenantId: number | string, payload: SavePlatformLocationRequest) {
const response = await http.post<ApiEnvelope<number | string>, ApiEnvelope<number | string>>(basePath, payload, {
params: { tenantId },
})
return unwrapApiResponse(response)
}
export async function updatePlatformLocation(
tenantId: number | string,
warehouseId: number | string,
id: number | string,
payload: SavePlatformLocationRequest,
) {
const response = await http.put<ApiEnvelope<null>, ApiEnvelope<null>>(`${basePath}/${id}`, payload, {
params: { tenantId, warehouseId },
})
return unwrapApiResponse(response)
}
export async function changePlatformLocationStatus(
tenantId: number | string,
warehouseId: number | string,
id: number | string,
status: EditablePlatformLocationStatus,
version: number,
) {
const response = await http.patch<ApiEnvelope<null>, ApiEnvelope<null>>(
`${basePath}/${id}/status`,
{ status, version },
{ params: { tenantId, warehouseId } },
)
return unwrapApiResponse(response)
}
export async function deletePlatformLocation(
tenantId: number | string,
warehouseId: number | string,
id: number | string,
version: number,
) {
const response = await http.delete<ApiEnvelope<null>, ApiEnvelope<null>>(`${basePath}/${id}`, {
params: { tenantId, warehouseId, version },
})
return unwrapApiResponse(response)
}