const { withClient } = require("./withClient"); async function exportJobToRR({ bodyshopId, job, logger }) { return withClient(bodyshopId, logger, async (client, routing) => { // 1) Upsert Customer const custPayload = mapJobToCustomer(job); const custRes = job.customer?.nameRecId ? await client.updateCustomer(custPayload, { routing }) : await client.insertCustomer(custPayload, { routing }); const customerNo = custRes?.data?.dmsRecKey || job.customer?.customerNo; if (!customerNo) throw new Error("Failed to resolve customerNo from RR response."); // 2) Ensure Service Vehicle (optional, if VIN present) if (job?.vehicle?.vin) { await client.insertServiceVehicle( { vin: job.vehicle.vin, vehicleServInfo: { customerNo } }, { routing } ); } // 3) Create RO const roHeader = { customerNo, departmentType: "B", vin: job?.vehicle?.vin, outsdRoNo: job?.roExternal || job?.id, advisorNo: job?.advisorNo, mileageIn: job?.mileageIn }; const roBody = mapJobToRO(job); // extend if you want lines/tax/etc const roRes = await client.createRepairOrder({ ...roHeader, ...roBody }, { routing }); return roRes?.data; }); } function mapJobToCustomer(job) { const c = job?.customer || {}; return { nameRecId: c.nameRecId, firstName: c.firstName || c.given_name, lastName: c.lastName || c.family_name, phone: c.phone || c.mobile, email: c.email, address: { line1: c.address1, line2: c.address2, city: c.city, state: c.province || c.state, postalCode: c.postal || c.zip } }; } function mapJobToRO(job) { return { // rolabor: [...], // roparts: [...], // estimate: {...}, // tax: {...} }; } module.exports = { exportJobToRR };