feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Expanded Logs / Formatting change
This commit is contained in:
@@ -1,29 +1,49 @@
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
const { GET_JOB_BY_PK } = require("../graphql-client/queries");
|
||||
|
||||
// ---------- Internals ----------
|
||||
/**
|
||||
* Remove all non-digit characters from a string.
|
||||
* @param s
|
||||
* @returns {string}
|
||||
*/
|
||||
const digitsOnly = (s) => String(s || "").replace(/\D/g, "");
|
||||
|
||||
function digitsOnly(s) {
|
||||
return String(s || "").replace(/\D/g, "");
|
||||
}
|
||||
/**
|
||||
* Pick job ID from various possible locations.
|
||||
* @param ctx
|
||||
* @param explicitId
|
||||
* @returns {*|null}
|
||||
*/
|
||||
const pickJobId = (ctx, explicitId) =>
|
||||
explicitId || ctx?.job?.id || ctx?.payload?.job?.id || ctx?.payload?.jobId || ctx?.jobId || null;
|
||||
|
||||
function pickJobId(ctx, explicitId) {
|
||||
return explicitId || ctx?.job?.id || ctx?.payload?.job?.id || ctx?.payload?.jobId || ctx?.jobId || null;
|
||||
}
|
||||
/**
|
||||
* Safely get VIN from job object.
|
||||
* @param job
|
||||
* @returns {*|string|null}
|
||||
*/
|
||||
const safeVin = (job) => (job?.v_vin && String(job.v_vin).trim()) || null;
|
||||
|
||||
function safeVin(job) {
|
||||
return (job?.v_vin && String(job.v_vin).trim()) || null;
|
||||
}
|
||||
|
||||
// Combined search helpers expect array-like blocks
|
||||
function blocksFromCombinedSearchResult(res) {
|
||||
/**
|
||||
* Extract blocks array from combined search result.
|
||||
* @param res
|
||||
* @returns {any[]|*[]}
|
||||
*/
|
||||
const blocksFromCombinedSearchResult = (res) => {
|
||||
const data = Array.isArray(res?.data) ? res.data : Array.isArray(res) ? res : [];
|
||||
return Array.isArray(data) ? data : [];
|
||||
}
|
||||
};
|
||||
|
||||
// ---------- Public API ----------
|
||||
|
||||
async function QueryJobData(ctx = {}, jobId) {
|
||||
/**
|
||||
* Query job data by ID from GraphQL API.
|
||||
* @param ctx
|
||||
* @param jobId
|
||||
* @returns {Promise<*>}
|
||||
* @constructor
|
||||
*/
|
||||
const QueryJobData = async (ctx = {}, jobId) => {
|
||||
if (ctx?.job) return ctx.job;
|
||||
if (ctx?.payload?.job) return ctx.payload.job;
|
||||
|
||||
@@ -39,18 +59,16 @@ async function QueryJobData(ctx = {}, jobId) {
|
||||
const msg = e?.response?.errors?.[0]?.message || e.message || "unknown";
|
||||
throw new Error(`QueryJobData failed: ${msg}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Build minimal RR RO payload (keys match RR client expectations).
|
||||
* 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
|
||||
* Build RR Repair Order payload from job and customer data.
|
||||
* @param job
|
||||
* @param selectedCustomer
|
||||
* @param advisorNo
|
||||
* @returns {{outsdRoNo: string, repairOrderNumber: string, departmentType: string, vin: string, customerNo: string, advisorNo: string, mileageIn: *|null}}
|
||||
*/
|
||||
function buildRRRepairOrderPayload({ job, selectedCustomer, advisorNo }) {
|
||||
const buildRRRepairOrderPayload = ({ job, selectedCustomer, advisorNo }) => {
|
||||
const customerNo = selectedCustomer?.customerNo
|
||||
? String(selectedCustomer.customerNo).trim()
|
||||
: selectedCustomer?.custNo
|
||||
@@ -94,9 +112,14 @@ function buildRRRepairOrderPayload({ job, selectedCustomer, advisorNo }) {
|
||||
// ---- Mileage In (new) ----
|
||||
mileageIn
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
function makeVehicleSearchPayloadFromJob(job) {
|
||||
/**
|
||||
* Make vehicle search payload from job data
|
||||
* @param job
|
||||
* @returns {{kind: string, license: string}|null|{kind: string, vin: *|string}}
|
||||
*/
|
||||
const makeVehicleSearchPayloadFromJob = (job) => {
|
||||
const vin = safeVin(job);
|
||||
if (vin) return { kind: "vin", vin };
|
||||
|
||||
@@ -104,9 +127,14 @@ function makeVehicleSearchPayloadFromJob(job) {
|
||||
if (plate) return { kind: "license", license: String(plate).trim() };
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
function makeCustomerSearchPayloadFromJob(job) {
|
||||
/**
|
||||
* Make customer search payload from job data
|
||||
* @param job
|
||||
* @returns {{kind: string, vin: *|string}|{kind: string, name: {name: string}}|{kind: string, phone: string}|null}
|
||||
*/
|
||||
const makeCustomerSearchPayloadFromJob = (job) => {
|
||||
const phone = job?.ownr_ph1;
|
||||
const d = digitsOnly(phone);
|
||||
if (d.length >= 7) return { kind: "phone", phone: d };
|
||||
@@ -120,9 +148,14 @@ function makeCustomerSearchPayloadFromJob(job) {
|
||||
if (vin) return { kind: "vin", vin };
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
function normalizeCustomerCandidates(res) {
|
||||
/**
|
||||
* Normalize customer candidates from combined search result.
|
||||
* @param res
|
||||
* @returns {*[]}
|
||||
*/
|
||||
const normalizeCustomerCandidates = (res) => {
|
||||
const blocks = blocksFromCombinedSearchResult(res);
|
||||
const out = [];
|
||||
for (const blk of blocks) {
|
||||
@@ -146,9 +179,14 @@ function normalizeCustomerCandidates(res) {
|
||||
seen.add(c.custNo);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function normalizeVehicleCandidates(res) {
|
||||
/**
|
||||
* Normalize vehicle candidates from combined search result.
|
||||
* @param res
|
||||
* @returns {*[]}
|
||||
*/
|
||||
const normalizeVehicleCandidates = (res) => {
|
||||
const blocks = blocksFromCombinedSearchResult(res);
|
||||
const out = [];
|
||||
for (const blk of blocks) {
|
||||
@@ -170,7 +208,7 @@ function normalizeVehicleCandidates(res) {
|
||||
seen.add(v.vin);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
QueryJobData,
|
||||
|
||||
Reference in New Issue
Block a user