This commit is contained in:
2026-08-13 17:14:46 +08:00
parent b004edaf9e
commit e9564a21a2
5 changed files with 186 additions and 12 deletions
@@ -1,6 +1,7 @@
const MODULE = 'truck-appointments'
const OPTIONS = { scenario: 'truck-appointment-lifecycle', deploymentBlocker: true }
const BASE = '/api/platform/v1/truck-appointments'
const OCEAN_BOOKING_BASE = '/api/platform/v1/ocean-bookings'
function id(value) { return value == null ? '' : String(value) }
function codeOf(response) { return response?.body?.code == null ? 'missing' : String(response.body.code) }
@@ -36,10 +37,39 @@ async function uploadAttachment(context, appointmentId) {
} catch (error) { return { method: 'POST', path, http: 0, body: null, raw: String(error), requestId: null, traceId: null } }
}
function payload(context, suffix, version = 0) {
export function inspectOceanBookingIdentity(marker, resource, detail = null) {
if (!resource?.id) return { identity: null, reason: '本轮 oceanBooking.id 缺失' }
if (resource.marker !== marker) {
return { identity: null, reason: `oceanBooking marker 不属于本轮:expected=${marker}, actual=${resource.marker || 'missing'}` }
}
const booking = detail?.oceanBooking || null
if (booking && id(booking.id) !== id(resource.id)) {
return { identity: null, reason: `订舱详情资源归属不一致:expected=${id(resource.id)}, actual=${id(booking.id) || 'missing'}` }
}
const fields = ['customerId', 'warehouseId', 'customerName']
for (const field of fields) {
const published = id(resource[field])
const queried = id(booking?.[field])
if (published && queried && published !== queried) {
return { identity: null, reason: `订舱身份字段冲突:field=${field}, published=${published}, detail=${queried}` }
}
}
const identity = {
oceanBookingId: id(resource.id),
customerId: id(resource.customerId || booking?.customerId),
warehouseId: id(resource.warehouseId || booking?.warehouseId),
customerName: id(resource.customerName || booking?.customerName),
}
const missingFields = fields.filter((field) => !identity[field])
return missingFields.length
? { identity: null, reason: `本轮订舱身份字段缺失:${missingFields.join(',')}`, missingFields }
: { identity, reason: null, missingFields: [] }
}
export function truckAppointmentPayload(context, suffix, identity, version = 0) {
return {
outboundOrderId: null, oceanBookingId: context.createdResources?.oceanBooking?.id || null, containerBookingId: null, loadingPlanId: null,
warehouseId: '193000000000000001', customerId: '10001', customerName: `拖车验收客户-${suffix}`,
outboundOrderId: null, oceanBookingId: identity.oceanBookingId, containerBookingId: null, loadingPlanId: null,
warehouseId: identity.warehouseId, customerId: identity.customerId, customerName: identity.customerName,
carrierId: null, carrierName: `承运商-${suffix}`, fleetContact: '车队联系人', fleetPhone: '13800000004',
driverName: '司机验收', driverPhone: '13800000005', driverIdCard: null, vehicleNo: `粤B-${suffix.slice(-6)}`,
storeName: `门店-${suffix}`, storeContact: '门店联系人', storePhone: '13800000006', storeAddress: '深圳市南山区验收地址',
@@ -64,13 +94,35 @@ export async function run(context) {
const oceanBookings = await import(new URL('./ocean-bookings.mjs', import.meta.url))
await oceanBookings.run(context)
}
if (!context.createdResources?.oceanBooking?.id) {
const oceanBooking = context.createdResources?.oceanBooking
if (!oceanBooking?.id) {
context.addStep(MODULE, '创建拖车预约所需隔离订舱 fixture', null, false,
'本轮 ocean-bookings 未返回可关联的隔离订舱,未使用历史订舱', OPTIONS)
return
}
let identityResult = inspectOceanBookingIdentity(context.marker, oceanBooking)
if (!identityResult.identity && identityResult.missingFields?.length) {
const bookingPath = `${OCEAN_BOOKING_BASE}/${encodeURIComponent(id(oceanBooking.id))}`
const bookingResponse = await request(context, 'GET', bookingPath)
if (bookingResponse.http !== 200 || codeOf(bookingResponse) !== '200') {
context.addStep(MODULE, '读取本轮订舱身份快照', bookingResponse, false,
`path=${bookingPath}, HTTP=${bookingResponse.http}, code=${codeOf(bookingResponse)}, requestId=${bookingResponse.requestId || 'missing'}, traceId=${bookingResponse.traceId || 'missing'}`, OPTIONS)
return
}
identityResult = inspectOceanBookingIdentity(context.marker, oceanBooking, data(bookingResponse))
context.addStep(MODULE, '读取本轮订舱身份快照', bookingResponse, Boolean(identityResult.identity),
identityResult.identity
? `oceanBookingId=${identityResult.identity.oceanBookingId}, customerId=${identityResult.identity.customerId}, warehouseId=${identityResult.identity.warehouseId}`
: `${identityResult.reason}, path=${bookingPath}, requestId=${bookingResponse.requestId || 'missing'}, traceId=${bookingResponse.traceId || 'missing'}`, OPTIONS)
}
if (!identityResult.identity) {
context.addStep(MODULE, '创建拖车预约所需本轮订舱身份', null, false,
`${identityResult.reason};未使用历史订舱或硬编码客户/仓库`, OPTIONS)
return
}
const bookingIdentity = Object.freeze({ ...identityResult.identity })
const createResponse = await request(context, 'POST', BASE, payload(context, suffix)); const appointment = context.apiSuccess(MODULE, '创建隔离拖车预约', createResponse, '', OPTIONS)
const createResponse = await request(context, 'POST', BASE, truckAppointmentPayload(context, suffix, bookingIdentity)); const appointment = context.apiSuccess(MODULE, '创建隔离拖车预约', createResponse, '', OPTIONS)
const appointmentId = id(appointment?.id)
context.assert(MODULE, '拖车预约费用、司机车辆和地址字段完整', createResponse, Boolean(appointmentId) && appointment?.status === 'DRAFT' && appointment?.payableAmount != null && appointment?.receivableAmount != null, `id=${appointmentId || 'missing'}, status=${appointment?.status || 'missing'}`, OPTIONS)
if (!appointmentId) return
@@ -87,8 +139,8 @@ export async function run(context) {
if (attachmentId) { const removed = await request(context, 'DELETE', `${path}/attachments/${attachmentId}`); context.apiSuccess(MODULE, '删除本轮拖车预约附件引用', removed, `attachmentId=${attachmentId}`, OPTIONS) }
let version = Number(appointment.version)
const edit = await request(context, 'PUT', path, { ...payload(context, suffix, version), vehicleNo: `粤C-${suffix.slice(-6)}` }); const edited = context.apiSuccess(MODULE, '编辑拖车预约', edit, `version=${version}`, OPTIONS); version = Number(edited?.version)
const stale = await request(context, 'PUT', path, { ...payload(context, suffix, 0), vehicleNo: `STALE-${suffix}` })
const edit = await request(context, 'PUT', path, { ...truckAppointmentPayload(context, suffix, bookingIdentity, version), vehicleNo: `粤C-${suffix.slice(-6)}` }); const edited = context.apiSuccess(MODULE, '编辑拖车预约', edit, `version=${version}`, OPTIONS); version = Number(edited?.version)
const stale = await request(context, 'PUT', path, { ...truckAppointmentPayload(context, suffix, bookingIdentity, 0), vehicleNo: `STALE-${suffix}` })
context.addStep(MODULE, '拖车预约旧版本编辑返回冲突', stale, stale.http === 200 && ['A0409','409'].includes(codeOf(stale)), `HTTP=${stale.http}, code=${codeOf(stale)}`, OPTIONS)
const after = await request(context, 'GET', path); const afterData = data(after); context.apiSuccess(MODULE, '冲突后回查拖车预约', after, '', OPTIONS)
context.assert(MODULE, '拖车预约冲突未部分更新', after, afterData?.vehicleNo === edited?.vehicleNo, `vehicleNo=${afterData?.vehicleNo || 'missing'}`, OPTIONS); version = Number(afterData?.version)