55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
// 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).
|
|
*/
|
|
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");
|
|
|
|
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 res = await client
|
|
.setHeaders({ Authorization: `Bearer ${token}` })
|
|
.request(queries.QUERY_JOBS_FOR_CDK_EXPORT, { id: jobid });
|
|
|
|
return res?.jobs_by_pk || null;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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;
|
|
|
|
const vehicle = first?.Vehicle || first?.vehicle || first?.Veh || null;
|
|
const customer = first?.Customer || first?.customer || first?.Cust || null;
|
|
|
|
return {
|
|
vin,
|
|
vehicle,
|
|
customer
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
QueryJobData,
|
|
QueryDMSVehicleById,
|
|
getTransactionType
|
|
};
|