feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -1,67 +1,28 @@
|
||||
const { withClient } = require("./withClient");
|
||||
const { buildRRRepairOrderPayload } = require("./rr-job-helpers");
|
||||
const { buildClientAndOpts } = require("./rr-lookup");
|
||||
|
||||
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 });
|
||||
async function exportJobToRR(args) {
|
||||
const { bodyshop, job, selectedCustomer, advisorNo, existing } = args;
|
||||
|
||||
const customerNo = custRes?.data?.dmsRecKey || job.customer?.customerNo;
|
||||
if (!customerNo) throw new Error("Failed to resolve customerNo from RR response.");
|
||||
// Build client + opts (opts carries routing)
|
||||
const { client, opts } = buildClientAndOpts(bodyshop);
|
||||
|
||||
// 2) Ensure Service Vehicle (optional, if VIN present)
|
||||
if (job?.vehicle?.vin) {
|
||||
await client.insertServiceVehicle(
|
||||
{
|
||||
vin: job.vehicle.vin,
|
||||
vehicleServInfo: { customerNo }
|
||||
},
|
||||
{ routing }
|
||||
);
|
||||
}
|
||||
const payload = buildRRRepairOrderPayload({ job, selectedCustomer, advisorNo });
|
||||
|
||||
// 3) Create RO
|
||||
const roHeader = {
|
||||
customerNo,
|
||||
departmentType: "B",
|
||||
vin: job?.vehicle?.vin,
|
||||
outsdRoNo: job?.roExternal || job?.id,
|
||||
advisorNo: job?.advisorNo,
|
||||
mileageIn: job?.mileageIn
|
||||
};
|
||||
let rrRes;
|
||||
if (existing?.dmsRepairOrderId) {
|
||||
rrRes = await client.updateRepairOrder({ ...payload, dmsRepairOrderId: existing.dmsRepairOrderId }, opts);
|
||||
} else {
|
||||
rrRes = await client.createRepairOrder(payload, opts);
|
||||
}
|
||||
|
||||
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: {...}
|
||||
success: rrRes?.success === true,
|
||||
data: rrRes?.data || null,
|
||||
roStatus: rrRes?.data?.roStatus || null,
|
||||
statusBlocks: rrRes?.statusBlocks || [],
|
||||
xml: rrRes?.xml,
|
||||
parsed: rrRes?.parsed
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user