133 lines
4.1 KiB
JavaScript
133 lines
4.1 KiB
JavaScript
// File: server/rr/rr-export-logs.js
|
|
// Mark job exported + insert export logs (success/failure) for Reynolds, mirroring Fortellis/PBS.
|
|
|
|
const { GraphQLClient } = require("graphql-request");
|
|
const queries = require("../graphql-client/queries");
|
|
const CreateRRLogEvent = require("./rr-logger-event");
|
|
|
|
/** Get bearer token from the socket (same approach used elsewhere) */
|
|
function getAuthToken(socket) {
|
|
return (socket?.data && socket.data.authToken) || (socket?.handshake?.auth && socket.handshake.auth.token) || null;
|
|
}
|
|
|
|
/** Build a compact ExportsLog metadata object for RR */
|
|
function buildRRExportMeta({ result, extra = {} }) {
|
|
// Avoid gigantic payloads; keep the useful bits
|
|
const roStatus = result?.roStatus || result?.data?.roStatus || null;
|
|
|
|
return {
|
|
provider: "rr",
|
|
success: Boolean(result?.success || roStatus?.status === "Success"),
|
|
customerNo: result?.customerNo,
|
|
svId: result?.svId,
|
|
roStatus: roStatus && {
|
|
status: roStatus.status ?? roStatus.Status,
|
|
statusCode: roStatus.statusCode ?? roStatus.StatusCode,
|
|
message: roStatus.message ?? roStatus.Message
|
|
},
|
|
statusBlocks: result?.statusBlocks || undefined,
|
|
xml: result?.xml,
|
|
parsed: result?.parsed,
|
|
...extra
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Success path: mark job exported + insert success ExportsLog w/ metadata.
|
|
* Uses queries.MARK_JOB_EXPORTED (same shape as Fortellis/PBS).
|
|
*/
|
|
async function markRRExportSuccess({ socket, jobId, job, bodyshop, result, metaExtra = {} }) {
|
|
const endpoint = process.env.GRAPHQL_ENDPOINT;
|
|
if (!endpoint) throw new Error("GRAPHQL_ENDPOINT not configured");
|
|
const token = getAuthToken(socket);
|
|
if (!token) throw new Error("Auth token missing on socket");
|
|
|
|
const client = new GraphQLClient(endpoint, {});
|
|
client.setHeaders({ Authorization: `Bearer ${token}` });
|
|
|
|
const exportedStatus =
|
|
job?.bodyshop?.md_ro_statuses?.default_exported || bodyshop?.md_ro_statuses?.default_exported || "Exported*";
|
|
|
|
const meta = buildRRExportMeta({ result, extra: metaExtra });
|
|
|
|
try {
|
|
await client.request(queries.MARK_JOB_EXPORTED, {
|
|
jobId,
|
|
job: {
|
|
status: exportedStatus,
|
|
date_exported: new Date()
|
|
},
|
|
log: {
|
|
bodyshopid: bodyshop?.id || job?.bodyshop?.id,
|
|
jobid: jobId,
|
|
successful: true,
|
|
useremail: socket?.user?.email || null,
|
|
metadata: meta
|
|
},
|
|
bill: {
|
|
exported: true,
|
|
exported_at: new Date()
|
|
}
|
|
});
|
|
|
|
CreateRRLogEvent(socket, "INFO", "RR export: job marked exported + success log inserted", {
|
|
jobId,
|
|
exportedStatus
|
|
});
|
|
} catch (e) {
|
|
// Non-fatal: export already succeeded; just surface the DB log failure
|
|
CreateRRLogEvent(socket, "ERROR", "RR export: failed to persist success markers/log", {
|
|
jobId,
|
|
error: e?.message
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Failure path: insert failure ExportsLog (no job status flip).
|
|
* Uses queries.INSERT_EXPORT_LOG (same shape as Fortellis/PBS).
|
|
*/
|
|
async function insertRRFailedExportLog({ socket, jobId, job, bodyshop, error, classification, result }) {
|
|
const endpoint = process.env.GRAPHQL_ENDPOINT;
|
|
if (!endpoint) throw new Error("GRAPHQL_ENDPOINT not configured");
|
|
const token = getAuthToken(socket);
|
|
if (!token) throw new Error("Auth token missing on socket");
|
|
|
|
const client = new GraphQLClient(endpoint, {});
|
|
client.setHeaders({ Authorization: `Bearer ${token}` });
|
|
|
|
const meta = buildRRExportMeta({
|
|
result,
|
|
extra: {
|
|
error: error?.message || String(error),
|
|
classification: classification || undefined
|
|
}
|
|
});
|
|
|
|
try {
|
|
await client.request(queries.INSERT_EXPORT_LOG, {
|
|
log: {
|
|
bodyshopid: bodyshop?.id || job?.bodyshop?.id,
|
|
jobid: jobId,
|
|
successful: false,
|
|
message: error?.message || String(error),
|
|
useremail: socket?.user?.email || null,
|
|
metadata: meta
|
|
}
|
|
});
|
|
|
|
CreateRRLogEvent(socket, "INFO", "RR export: failure log inserted", { jobId });
|
|
} catch (e) {
|
|
// Best-effort; don't throw
|
|
CreateRRLogEvent(socket, "ERROR", "RR export: failed to insert failure log", {
|
|
jobId,
|
|
error: e?.message
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
markRRExportSuccess,
|
|
insertRRFailedExportLog
|
|
};
|