158 lines
13 KiB
JavaScript
158 lines
13 KiB
JavaScript
function id(value) { return value == null ? '' : String(value) }
|
|||
|
|
function pageOk(value) { return Array.isArray(value?.records) && Number.isFinite(Number(value?.total)) }
|
||
|
|
function xlsxOk(response) {
|
||
|
|
return response.http === 200 && response.bytes?.length > 4
|
||
|
|
&& response.bytes[0] === 0x50 && response.bytes[1] === 0x4b
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function run(context) {
|
||
|
|
const module = 'master-data'
|
||
|
|
const suffix = context.marker.replace(/\D/g, '').slice(-12)
|
||
|
|
|
||
|
|
const warehouseListResponse = await context.request('GET', '/api/platform/v1/warehouse-admin?page=1&size=20')
|
||
|
|
const warehousePage = context.apiSuccess(module, '查询平台仓库列表', warehouseListResponse)
|
||
|
|
context.assert(module, '仓库列表分页结构正确', warehouseListResponse, pageOk(warehousePage), `total=${warehousePage?.total ?? 'n/a'}`)
|
||
|
|
const warehousePayload = {
|
||
|
|
tenantId: 0, warehouseCode: `AW${suffix}`, warehouseName: `Admin验收仓-${suffix}`,
|
||
|
|
warehouseType: 'DOMESTIC', countryCode: 'CN', timezone: 'Asia/Shanghai',
|
||
|
|
contactName: '仓库验收员', contactPhone: '13800000001', contactEmail: `warehouse-${suffix}@example.com`,
|
||
|
|
status: 'ACTIVE', remark: context.marker,
|
||
|
|
address: {
|
||
|
|
addressType: 'WAREHOUSE', countryCode: 'CN', stateProvince: '广东省', city: '深圳市',
|
||
|
|
district: '南山区', postalCode: '518000', addressLine1: 'Admin API 验收仓地址',
|
||
|
|
addressLine2: null, latitude: null, longitude: null,
|
||
|
|
},
|
||
|
|
capabilities: [{ capabilityCode: 'RECEIVING', capabilityName: '收货', enabled: true, cutoffTime: '16:00:00', description: context.marker }],
|
||
|
|
}
|
||
|
|
const createWarehouseResponse = await context.request('POST', '/api/platform/v1/warehouse-admin', warehousePayload)
|
||
|
|
const warehouseId = id(context.apiSuccess(module, '创建平台公共仓库', createWarehouseResponse))
|
||
|
|
context.assert(module, '仓库创建返回字符串兼容 ID', createWarehouseResponse, Boolean(warehouseId), `warehouseId=${warehouseId || 'missing'}`)
|
||
|
|
if (!warehouseId) throw new Error('平台仓库创建失败,库位和托盘场景无法继续')
|
||
|
|
context.context.warehouseId = warehouseId
|
||
|
|
context.createdResources.warehouse = { id: warehouseId, code: warehousePayload.warehouseCode, finalStatus: 'INACTIVE' }
|
||
|
|
const warehousePath = `/api/platform/v1/warehouse-admin/${warehouseId}?tenantId=0`
|
||
|
|
const warehouseDetailResponse = await context.request('GET', warehousePath)
|
||
|
|
const warehouseDetail = context.apiSuccess(module, '查询平台仓库详情', warehouseDetailResponse)
|
||
|
|
context.assert(module, '仓库详情、地址和能力正确', warehouseDetailResponse,
|
||
|
|
id(warehouseDetail?.warehouse?.id) === warehouseId && warehouseDetail?.addresses?.length === 1 && warehouseDetail?.capabilities?.length === 1,
|
||
|
|
`addresses=${warehouseDetail?.addresses?.length ?? 'n/a'}, capabilities=${warehouseDetail?.capabilities?.length ?? 'n/a'}`)
|
||
|
|
const editedWarehouse = { ...warehousePayload, warehouseName: `${warehousePayload.warehouseName}-已编辑`, remark: `${context.marker}-EDITED` }
|
||
|
|
const updateWarehouseResponse = await context.request('PUT', warehousePath, editedWarehouse)
|
||
|
|
context.apiSuccess(module, '编辑平台仓库', updateWarehouseResponse)
|
||
|
|
const warehouseAfterEditResponse = await context.request('GET', warehousePath)
|
||
|
|
const warehouseAfterEdit = context.apiSuccess(module, '回查编辑后的平台仓库', warehouseAfterEditResponse)
|
||
|
|
context.assert(module, '仓库编辑已持久化', warehouseAfterEditResponse,
|
||
|
|
warehouseAfterEdit?.warehouse?.warehouseName === editedWarehouse.warehouseName && warehouseAfterEdit?.warehouse?.remark === editedWarehouse.remark,
|
||
|
|
`warehouseId=${warehouseId}`)
|
||
|
|
|
||
|
|
const locationPayload = {
|
||
|
|
warehouseId, zoneCode: `Z${suffix.slice(-5)}`, zoneName: 'Admin验收区',
|
||
|
|
locationCode: `L${suffix}`, locationName: 'Admin验收库位', locationType: 'SHELF',
|
||
|
|
maxWeightKg: 1000, status: 'AVAILABLE', remark: context.marker,
|
||
|
|
}
|
||
|
|
const createLocationResponse = await context.request('POST', '/api/platform/v1/locations?tenantId=0', locationPayload)
|
||
|
|
const locationId = id(context.apiSuccess(module, '创建仓库库位', createLocationResponse))
|
||
|
|
if (!locationId) throw new Error('库位创建失败')
|
||
|
|
context.createdResources.location = { id: locationId, warehouseId, deleted: false }
|
||
|
|
const locationPath = `/api/platform/v1/locations/${locationId}?tenantId=0&warehouseId=${warehouseId}`
|
||
|
|
const locationDetailResponse = await context.request('GET', locationPath)
|
||
|
|
const locationDetail = context.apiSuccess(module, '查询库位详情', locationDetailResponse)
|
||
|
|
context.assert(module, '库位详情关联正确仓库', locationDetailResponse,
|
||
|
|
id(locationDetail?.location?.id) === locationId && id(locationDetail?.location?.warehouseId) === warehouseId,
|
||
|
|
`locationId=${locationId}`)
|
||
|
|
const locationVersion = Number(locationDetail?.location?.version)
|
||
|
|
const updateLocationResponse = await context.request('PUT', locationPath, { ...locationPayload, locationName: 'Admin验收库位-已编辑', maxWeightKg: 1200, version: locationVersion })
|
||
|
|
context.apiSuccess(module, '编辑库位', updateLocationResponse)
|
||
|
|
const locationAfterEditResponse = await context.request('GET', locationPath)
|
||
|
|
const locationAfterEdit = context.apiSuccess(module, '回查编辑后的库位', locationAfterEditResponse)
|
||
|
|
context.assert(module, '库位编辑与版本递增正确', locationAfterEditResponse,
|
||
|
|
locationAfterEdit?.location?.locationName === 'Admin验收库位-已编辑' && Number(locationAfterEdit?.location?.version) === locationVersion + 1,
|
||
|
|
`version=${locationAfterEdit?.location?.version ?? 'missing'}`)
|
||
|
|
const disabledLocationVersion = Number(locationAfterEdit?.location?.version)
|
||
|
|
const disableLocationResponse = await context.request('PATCH', `/api/platform/v1/locations/${locationId}/status?tenantId=0&warehouseId=${warehouseId}`, { status: 'DISABLED', version: disabledLocationVersion })
|
||
|
|
context.apiSuccess(module, '停用库位', disableLocationResponse)
|
||
|
|
const deleteLocationResponse = await context.request('DELETE', `/api/platform/v1/locations/${locationId}?tenantId=0&warehouseId=${warehouseId}&version=${disabledLocationVersion + 1}`)
|
||
|
|
const deletedLocation = context.apiSuccess(module, '删除未占用测试库位', deleteLocationResponse)
|
||
|
|
if (deletedLocation === null && String(deleteLocationResponse.body?.code) === '200') context.createdResources.location.deleted = true
|
||
|
|
|
||
|
|
const palletPayload = { warehouseId, palletType: 'STANDARD', capacityCbm: 1.5, weightKg: 25, color: 'BLUE', remark: context.marker }
|
||
|
|
const createPalletResponse = await context.request('POST', '/api/platform/v1/pallets?tenantId=0', palletPayload)
|
||
|
|
const palletId = id(context.apiSuccess(module, '创建仓库托盘', createPalletResponse))
|
||
|
|
if (!palletId) throw new Error('托盘创建失败')
|
||
|
|
context.createdResources.pallet = { id: palletId, warehouseId, deleted: false }
|
||
|
|
const palletPath = `/api/platform/v1/pallets/${palletId}?tenantId=0&warehouseId=${warehouseId}`
|
||
|
|
const palletDetailResponse = await context.request('GET', palletPath)
|
||
|
|
const pallet = context.apiSuccess(module, '查询托盘详情', palletDetailResponse)
|
||
|
|
context.assert(module, '托盘详情关联正确仓库', palletDetailResponse,
|
||
|
|
id(pallet?.id) === palletId && id(pallet?.warehouseId) === warehouseId, `palletId=${palletId}`)
|
||
|
|
const updatePalletResponse = await context.request('PUT', palletPath, { ...palletPayload, capacityCbm: 1.75, weightKg: 26, remark: `${context.marker}-EDITED` })
|
||
|
|
context.apiSuccess(module, '编辑托盘', updatePalletResponse)
|
||
|
|
const palletAfterEditResponse = await context.request('GET', palletPath)
|
||
|
|
const palletAfterEdit = context.apiSuccess(module, '回查编辑后的托盘', palletAfterEditResponse)
|
||
|
|
context.assert(module, '托盘编辑已持久化', palletAfterEditResponse,
|
||
|
|
Number(palletAfterEdit?.capacityCbm) === 1.75 && palletAfterEdit?.remark === `${context.marker}-EDITED`, `palletId=${palletId}`)
|
||
|
|
const deletePalletResponse = await context.request('DELETE', palletPath)
|
||
|
|
context.apiSuccess(module, '删除未使用测试托盘', deletePalletResponse)
|
||
|
|
if (String(deletePalletResponse.body?.code) === '200') context.createdResources.pallet.deleted = true
|
||
|
|
|
||
|
|
const expressPayload = {
|
||
|
|
companyCode: `EX${suffix}`, companyName: `Admin验收快递-${suffix}`, servicePhone: '4000000000',
|
||
|
|
website: 'https://example.com', trackingUrlTemplate: 'https://example.com/{trackingNo}', status: 'ACTIVE', remark: context.marker,
|
||
|
|
}
|
||
|
|
const createExpressResponse = await context.request('POST', '/api/platform/v1/express-companies?tenantId=0', expressPayload)
|
||
|
|
const expressId = id(context.apiSuccess(module, '创建快递公司', createExpressResponse))
|
||
|
|
if (!expressId) throw new Error('快递公司创建失败')
|
||
|
|
context.createdResources.expressCompany = { id: expressId, code: expressPayload.companyCode, finalStatus: 'INACTIVE' }
|
||
|
|
const expressPath = `/api/platform/v1/express-companies/${expressId}?tenantId=0`
|
||
|
|
const expressDetailResponse = await context.request('GET', expressPath)
|
||
|
|
const express = context.apiSuccess(module, '查询快递公司详情', expressDetailResponse)
|
||
|
|
context.assert(module, '快递公司详情正确', expressDetailResponse, id(express?.id) === expressId && express?.companyCode === expressPayload.companyCode, `expressId=${expressId}`)
|
||
|
|
const updateExpressResponse = await context.request('PUT', expressPath, { ...expressPayload, companyName: `${expressPayload.companyName}-已编辑`, remark: `${context.marker}-EDITED` })
|
||
|
|
context.apiSuccess(module, '编辑快递公司', updateExpressResponse)
|
||
|
|
const disableExpressResponse = await context.request('PATCH', `/api/platform/v1/express-companies/${expressId}/status?tenantId=0`, { status: 'INACTIVE' })
|
||
|
|
context.apiSuccess(module, '停用快递公司', disableExpressResponse)
|
||
|
|
const expressListResponse = await context.request('GET', `/api/platform/v1/express-companies?page=1&size=20&keyword=${expressPayload.companyCode}`)
|
||
|
|
const expressPage = context.apiSuccess(module, '筛选快递公司列表', expressListResponse)
|
||
|
|
context.assert(module, '快递公司列表回显停用状态', expressListResponse,
|
||
|
|
expressPage?.records?.some((item) => id(item.id) === expressId && item.status === 'INACTIVE'), `records=${expressPage?.records?.length ?? 'n/a'}`)
|
||
|
|
|
||
|
|
const partnerPayload = {
|
||
|
|
partnerCode: `PT${suffix}`, partnerName: `Admin验收往来单位-${suffix}`, partnerType: 'SUPPLIER', types: ['SUPPLIER'],
|
||
|
|
contactName: '往来单位验收员', contactPhone: '13800000002', contactEmail: `partner-${suffix}@example.com`,
|
||
|
|
countryCode: 'CN', province: '广东省', city: '深圳市', district: '南山区', addressLine: 'Admin API 往来单位地址',
|
||
|
|
postalCode: '518000', status: 'ACTIVE', remark: context.marker,
|
||
|
|
}
|
||
|
|
const createPartnerResponse = await context.request('POST', '/api/platform/v1/partners?tenantId=0', partnerPayload)
|
||
|
|
const partnerId = id(context.apiSuccess(module, '创建往来单位', createPartnerResponse))
|
||
|
|
if (!partnerId) throw new Error('往来单位创建失败')
|
||
|
|
context.createdResources.partner = { id: partnerId, code: partnerPayload.partnerCode, finalStatus: 'INACTIVE' }
|
||
|
|
const partnerPath = `/api/platform/v1/partners/${partnerId}?tenantId=0`
|
||
|
|
const partnerDetailResponse = await context.request('GET', partnerPath)
|
||
|
|
const partner = context.apiSuccess(module, '查询往来单位详情', partnerDetailResponse)
|
||
|
|
context.assert(module, '往来单位详情正确', partnerDetailResponse,
|
||
|
|
id(partner?.id) === partnerId && partner?.types?.includes('SUPPLIER'), `partnerId=${partnerId}`)
|
||
|
|
const updatePartnerResponse = await context.request('PUT', partnerPath, { ...partnerPayload, partnerName: `${partnerPayload.partnerName}-已编辑`, types: ['SUPPLIER', 'CARRIER'], remark: `${context.marker}-EDITED` })
|
||
|
|
context.apiSuccess(module, '编辑往来单位', updatePartnerResponse)
|
||
|
|
const disablePartnerResponse = await context.request('PATCH', `/api/platform/v1/partners/${partnerId}/status?tenantId=0`, { status: 'INACTIVE' })
|
||
|
|
context.apiSuccess(module, '停用往来单位', disablePartnerResponse)
|
||
|
|
const partnerListResponse = await context.request('GET', `/api/platform/v1/partners?page=1&size=20&keyword=${partnerPayload.partnerCode}`)
|
||
|
|
const partnerPage = context.apiSuccess(module, '筛选往来单位列表', partnerListResponse)
|
||
|
|
context.assert(module, '往来单位列表回显编辑与停用状态', partnerListResponse,
|
||
|
|
partnerPage?.records?.some((item) => id(item.id) === partnerId && item.status === 'INACTIVE' && item.types?.includes('CARRIER')),
|
||
|
|
`records=${partnerPage?.records?.length ?? 'n/a'}`)
|
||
|
|
|
||
|
|
const templates = [
|
||
|
|
['仓库', '/api/platform/v1/warehouse-admin/import-template'],
|
||
|
|
['快递公司', '/api/platform/v1/express-companies/import-template'],
|
||
|
|
['往来单位', '/api/platform/v1/partners/import-template'],
|
||
|
|
]
|
||
|
|
for (const [name, path] of templates) {
|
||
|
|
const response = await context.requestBinary('GET', path)
|
||
|
|
context.addStep(module, `下载${name}导入模板`, response, xlsxOk(response),
|
||
|
|
`bytes=${response.bytes?.length ?? 0}, contentType=${response.contentType || 'missing'}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
const disableWarehouseResponse = await context.request('PATCH', `/api/platform/v1/warehouse-admin/${warehouseId}/status?tenantId=0&status=INACTIVE`)
|
||
|
|
context.apiSuccess(module, '停用测试仓库', disableWarehouseResponse)
|
||
|
|
}
|