feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration - Checkpoint

This commit is contained in:
Dave
2025-11-04 11:19:20 -05:00
parent 65e26ed5c9
commit 3d24d44274
23 changed files with 812 additions and 701 deletions

View File

@@ -1,54 +1,76 @@
// server/rr/rr-job-helpers.js
const { GraphQLClient } = require("graphql-request");
const queries = require("../graphql-client/queries");
const { combinedSearch } = require("./rr-lookup");
/** Namespace the job transaction data for RR */
const getTransactionType = (jobid) => `rr:${jobid}`;
/**
* QueryJobData — mirrors your Fortellis/CDK helpers; fetches job with all export details.
* Requires the caller's token (we read from the socket handshake).
* Query job + related entities.
* Supports { socket } (GraphQL) and/or { redisHelpers } (cache/fetch).
*/
async function QueryJobData({ socket, jobid }) {
const endpoint = process.env.GRAPHQL_ENDPOINT || process.env.HASURA_GRAPHQL_ENDPOINT || process.env.HASURA_URL;
if (!endpoint) throw new Error("GRAPHQL endpoint not configured");
async function QueryJobData(ctx = {}, jobId) {
if (!jobId) throw new Error("jobId required");
const client = new GraphQLClient(endpoint, {});
const token = (socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token);
if (!token) throw new Error("Missing auth token for QueryJobData");
const { redisHelpers, socket } = ctx;
const res = await client
.setHeaders({ Authorization: `Bearer ${token}` })
.request(queries.QUERY_JOBS_FOR_CDK_EXPORT, { id: jobid });
if (redisHelpers) {
if (typeof redisHelpers.getJobFromCache === "function") {
try {
const hit = await redisHelpers.getJobFromCache(jobId);
if (hit) return hit;
} catch {}
}
if (typeof redisHelpers.fetchJobById === "function") {
const full = await redisHelpers.fetchJobById(jobId);
if (full) return full;
}
}
return res?.jobs_by_pk || null;
if (socket) {
const endpoint = process.env.GRAPHQL_ENDPOINT;
if (!endpoint) throw new Error("GRAPHQL_ENDPOINT not configured");
const token = (socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token);
if (!token) throw new Error("Missing bearer token on socket for GraphQL fetch");
const client = new GraphQLClient(endpoint, {});
const resp = await client
.setHeaders({ Authorization: `Bearer ${token}` })
.request(queries.QUERY_JOBS_FOR_CDK_EXPORT, { id: jobId });
const job = resp?.jobs_by_pk;
if (job) return job;
throw new Error("QueryJobData: job not found via GraphQL");
}
throw new Error("QueryJobData: no available method to load job (need socket or redisHelpers)");
}
/**
* QueryDMSVehicleById — for RR we don't have a direct "vehicle by id" read,
* so we approximate with CombinedSearch by VIN and return the first hit.
* Build RR create/update RO payload.
* Prefers selectedVehicle.vin (if present) over job VIN.
*/
async function QueryDMSVehicleById({ bodyshopId, vin }) {
if (!vin) return null;
const res = await combinedSearch({ bodyshopId, kind: "vin", vin });
// RR lib returns { success, data: CombinedSearchBlock[] }
const blocks = res?.data || res || [];
const first = Array.isArray(blocks) && blocks.length ? blocks[0] : null;
if (!first) return null;
function buildRRRepairOrderPayload({ job, selectedCustomer, selectedVehicle, advisorNo }) {
const custNo =
selectedCustomer?.custNo ||
selectedCustomer?.customerNo ||
selectedCustomer?.CustNo ||
selectedCustomer?.CustomerNo;
const vehicle = first?.Vehicle || first?.vehicle || first?.Veh || null;
const customer = first?.Customer || first?.customer || first?.Cust || null;
if (!custNo) throw new Error("No RR customer selected (custNo missing)");
const vin = selectedVehicle?.vin || job?.vehicle?.vin || job?.v_vin || job?.vehicle_vin;
if (!vin) throw new Error("No VIN available (select or create a vehicle)");
return {
repairOrderNumber: String(job?.job_number || job?.id),
deptType: "B",
vin,
vehicle,
customer
custNo,
advNo: advisorNo || undefined
};
}
module.exports = {
QueryJobData,
QueryDMSVehicleById,
getTransactionType
buildRRRepairOrderPayload
};