import { randomUUID } from 'node:crypto' import { verifyVehicleCare } from './vehicle-care.mjs' const MODULE = 'fleet-vehicle-care' const BASE = '/api/platform/v1/last-leg/master-data/vehicles' const OPTIONS = { scenario: 'vehicle-care-lifecycle', deploymentBlocker: true } /** Fresh TEST fixtures only. No dispatch, inventory or financial writes. */ export async function run(context) { const proof = { status: 'CHECKING', care: {}, cleanup: 'PENDING' } context.createdResources.fleetVehicleCare = proof async function command(method, path, body, label) { const response = await context.request(method, path, body) const data = context.apiSuccess(MODULE, label, response, '', OPTIONS) if (response?.http !== 200 || String(response?.body?.code) !== '200' || data == null) throw new Error(label) return data } let failure try { const vehicle = await command('POST', BASE, { plateNumber: `VC-${randomUUID().slice(0, 12)}`, model: 'Vehicle care TEST', fuelConsumptionPer100Km: 10, mileageKm: 100, region: 'OVERSEAS', ownership: 'OWNED', status: 'IN_SERVICE', remark: context.marker, }, '建立本轮车辆运维专用档案') proof.vehicleId = String(vehicle.id) proof.plateNumber = vehicle.plateNumber await verifyVehicleCare(context, MODULE, vehicle, proof.care) proof.status = 'VERIFIED' } catch (error) { failure = error proof.status = 'FAILED' proof.error = error instanceof Error ? error.message : String(error) } finally { if (proof.vehicleId) { try { const path = `${BASE}/${encodeURIComponent(proof.vehicleId)}` const current = await command('GET', path, undefined, '结束前读取本轮车辆当前版本') const ended = current.status === 'INACTIVE' ? current : await command('PATCH', `${path}/status`, { recordVersion: current.recordVersion, status: 'INACTIVE', }, '停用本轮运维专用车辆并保留证据') if (ended.status !== 'INACTIVE') throw new Error('本轮运维车辆未停用') proof.cleanup = 'INACTIVE' } catch (error) { proof.cleanup = 'FAILED' proof.cleanupError = error instanceof Error ? error.message : String(error) proof.status = 'FAILED' failure ??= error } } } if (failure) throw failure }