feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration Translations / Export Logs
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
// 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");
|
||||
@@ -10,11 +7,9 @@ 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 */
|
||||
/** Compact metadata 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"),
|
||||
@@ -32,8 +27,46 @@ function buildRRExportMeta({ result, extra = {} }) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Build a stringified JSON array for the `message` text column */
|
||||
function buildMessageJSONString({ error, classification, result, fallback }) {
|
||||
const msgs = [];
|
||||
|
||||
const clean = (v) => {
|
||||
if (v == null) return null;
|
||||
try {
|
||||
const s = String(v).replace(/\s+/g, " ").trim();
|
||||
return s.length ? s : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const push = (v) => {
|
||||
const s = clean(v);
|
||||
if (s && !msgs.includes(s)) msgs.push(s);
|
||||
};
|
||||
|
||||
// Friendly first
|
||||
push(classification?.friendlyMessage);
|
||||
push(classification?.title);
|
||||
|
||||
// Error text
|
||||
if (error instanceof Error) push(error.message);
|
||||
else if (typeof error === "string") push(error);
|
||||
else if (error?.message) push(error.message);
|
||||
|
||||
// RR status message
|
||||
push(result?.roStatus?.message ?? result?.roStatus?.Message);
|
||||
|
||||
// Fallback
|
||||
push(fallback || "RR export failed");
|
||||
|
||||
const arr = msgs.length ? msgs : ["RR export failed"];
|
||||
return JSON.stringify(arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Success path: mark job exported + insert success ExportsLog w/ metadata.
|
||||
* Success: mark job exported + (optionally) insert a success log.
|
||||
* Uses queries.MARK_JOB_EXPORTED (same shape as Fortellis/PBS).
|
||||
*/
|
||||
async function markRRExportSuccess({ socket, jobId, job, bodyshop, result, metaExtra = {} }) {
|
||||
@@ -62,7 +95,8 @@ async function markRRExportSuccess({ socket, jobId, job, bodyshop, result, metaE
|
||||
jobid: jobId,
|
||||
successful: true,
|
||||
useremail: socket?.user?.email || null,
|
||||
metadata: meta
|
||||
metadata: meta,
|
||||
message: buildMessageJSONString({ result, fallback: "RR export succeeded" })
|
||||
},
|
||||
bill: {
|
||||
exported: true,
|
||||
@@ -75,7 +109,6 @@ async function markRRExportSuccess({ socket, jobId, job, bodyshop, result, metaE
|
||||
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
|
||||
@@ -84,8 +117,8 @@ async function markRRExportSuccess({ socket, jobId, job, bodyshop, result, metaE
|
||||
}
|
||||
|
||||
/**
|
||||
* Failure path: insert failure ExportsLog (no job status flip).
|
||||
* Uses queries.INSERT_EXPORT_LOG (same shape as Fortellis/PBS).
|
||||
* Failure: insert failure ExportsLog with `message` as JSON **string** (text column).
|
||||
* Uses queries.INSERT_EXPORT_LOG($logs: [exportlog_insert_input!]!).
|
||||
*/
|
||||
async function insertRRFailedExportLog({ socket, jobId, job, bodyshop, error, classification, result }) {
|
||||
const endpoint = process.env.GRAPHQL_ENDPOINT;
|
||||
@@ -104,21 +137,27 @@ async function insertRRFailedExportLog({ socket, jobId, job, bodyshop, error, cl
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
}
|
||||
});
|
||||
const message = buildMessageJSONString({
|
||||
error,
|
||||
classification,
|
||||
result,
|
||||
fallback: "RR export failed"
|
||||
});
|
||||
|
||||
const entry = {
|
||||
bodyshopid: bodyshop?.id || job?.bodyshop?.id,
|
||||
jobid: jobId,
|
||||
successful: false,
|
||||
message, // stringified JSON array
|
||||
useremail: socket?.user?.email || null,
|
||||
metadata: meta
|
||||
};
|
||||
|
||||
try {
|
||||
// Your mutation expects $logs (array). Keep to that signature.
|
||||
await client.request(queries.INSERT_EXPORT_LOG, { logs: [entry] });
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user