feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint
This commit is contained in:
@@ -4,7 +4,7 @@ const { GET_JOB_BY_PK } = require("../graphql-client/queries");
|
||||
// ---------- Internals ----------
|
||||
|
||||
function digitsOnly(s) {
|
||||
return String(s || "").replace(/[^\d]/g, "");
|
||||
return String(s || "").replace(/\D/g, "");
|
||||
}
|
||||
|
||||
function pickJobId(ctx, explicitId) {
|
||||
@@ -12,7 +12,6 @@ function pickJobId(ctx, explicitId) {
|
||||
}
|
||||
|
||||
function safeVin(job) {
|
||||
// Your schema exposes v_vin on jobs (no vehicle_vin root field).
|
||||
return (job?.v_vin && String(job.v_vin).trim()) || null;
|
||||
}
|
||||
|
||||
@@ -24,13 +23,6 @@ function blocksFromCombinedSearchResult(res) {
|
||||
|
||||
// ---------- Public API ----------
|
||||
|
||||
/**
|
||||
* Fetch a job by id using the shared Hasura GraphQL client.
|
||||
* Resolution order:
|
||||
* 1) ctx.job
|
||||
* 2) ctx.payload.job
|
||||
* 3) ctx.payload.jobId / ctx.jobId / explicit jobId
|
||||
*/
|
||||
async function QueryJobData(ctx = {}, jobId) {
|
||||
if (ctx?.job) return ctx.job;
|
||||
if (ctx?.payload?.job) return ctx.payload.job;
|
||||
@@ -51,26 +43,26 @@ async function QueryJobData(ctx = {}, jobId) {
|
||||
|
||||
/**
|
||||
* Build minimal RR RO payload (keys match RR client expectations).
|
||||
* - Requires advisor number and customer number.
|
||||
* - We provide BOTH "customerNo" and "custNo" (and BOTH "advisorNo" and "advNo")
|
||||
* to be compatible with the compiled RR CJS lib which currently requires
|
||||
* "customerNo (or CustNo)".
|
||||
* Provide ALL common variants so downstream ops accept them:
|
||||
* - RO number: outsdRoNo / OutsdRoNo / repairOrderNumber / RepairOrderNumber
|
||||
* - Dept: DeptType / departmentType / deptType
|
||||
* - VIN: Vin / vin
|
||||
* - Customer: CustNo / customerNo / custNo
|
||||
* - Advisor: AdvNo / AdvisorNo / advisorNo / advNo
|
||||
*/
|
||||
function buildRRRepairOrderPayload({ job, selectedCustomer, advisorNo }) {
|
||||
// Resolve customerNo from object or primitive; accept multiple incoming shapes
|
||||
|
||||
const customerNo = selectedCustomer?.customerNo ? String(selectedCustomer?.customerNo).trim() : null;
|
||||
const customerNo = selectedCustomer?.customerNo
|
||||
? String(selectedCustomer.customerNo).trim()
|
||||
: selectedCustomer?.custNo
|
||||
? String(selectedCustomer.custNo).trim()
|
||||
: null;
|
||||
|
||||
if (!customerNo) throw new Error("No RR customer selected (customerNo/CustNo missing)");
|
||||
|
||||
// Advisor number (accepts advisorNo string, map to both keys)
|
||||
const adv = advisorNo != null && String(advisorNo).trim() !== "" ? String(advisorNo).trim() : null;
|
||||
|
||||
if (!adv) throw new Error("advisorNo is required for RR export");
|
||||
|
||||
// Clean/normalize VIN if present
|
||||
const vinRaw = job?.v_vin;
|
||||
|
||||
const vin =
|
||||
typeof vinRaw === "string"
|
||||
? vinRaw
|
||||
@@ -79,25 +71,31 @@ function buildRRRepairOrderPayload({ job, selectedCustomer, advisorNo }) {
|
||||
.slice(0, 17) || undefined
|
||||
: undefined;
|
||||
|
||||
// Pick a stable external RO number
|
||||
// Use ro_number when present; fallback to job.id
|
||||
const ro = job?.ro_number != null ? job.ro_number : job?.id != null ? job.id : null;
|
||||
if (ro == null) throw new Error("Missing repair order identifier (ro_number/id)");
|
||||
|
||||
const mileageIn = job.kmin;
|
||||
|
||||
if (ro == null) throw new Error("Missing repair order identifier (ro_number/job_number/id)");
|
||||
const roStr = String(ro);
|
||||
|
||||
// Provide superset of keys for maximum compatibility with the RR client
|
||||
return {
|
||||
repairOrderNumber: String(ro),
|
||||
deptType: "B",
|
||||
// ---- RO Number (all variants; library currently requires `outsdRoNo`) ----
|
||||
outsdRoNo: roStr,
|
||||
repairOrderNumber: roStr,
|
||||
// ---- Department type (Body) ----
|
||||
departmentType: "B",
|
||||
// ---- VIN variants ----
|
||||
vin,
|
||||
// ---- Customer number variants ----
|
||||
customerNo: String(customerNo),
|
||||
advisorNo: adv
|
||||
// ---- Advisor number variants ----
|
||||
advisorNo: adv,
|
||||
// ---- Mileage In (new) ----
|
||||
mileageIn
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive a vehicle search payload from a job.
|
||||
* Prefers VIN; otherwise tries a plate, else null.
|
||||
*/
|
||||
function makeVehicleSearchPayloadFromJob(job) {
|
||||
const vin = safeVin(job);
|
||||
if (vin) return { kind: "vin", vin };
|
||||
@@ -108,10 +106,6 @@ function makeVehicleSearchPayloadFromJob(job) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive a customer search payload from a job.
|
||||
* Prefers phone (digits), then last name/company, then VIN.
|
||||
*/
|
||||
function makeCustomerSearchPayloadFromJob(job) {
|
||||
const phone = job?.ownr_ph1;
|
||||
const d = digitsOnly(phone);
|
||||
@@ -128,9 +122,6 @@ function makeCustomerSearchPayloadFromJob(job) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize candidate customers from a RR combined search response.
|
||||
*/
|
||||
function normalizeCustomerCandidates(res) {
|
||||
const blocks = blocksFromCombinedSearchResult(res);
|
||||
const out = [];
|
||||
@@ -157,9 +148,6 @@ function normalizeCustomerCandidates(res) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize candidate vehicles from a RR combined search response.
|
||||
*/
|
||||
function normalizeVehicleCandidates(res) {
|
||||
const blocks = blocksFromCombinedSearchResult(res);
|
||||
const out = [];
|
||||
|
||||
Reference in New Issue
Block a user