diff --git a/hasura/metadata/tables.yaml b/hasura/metadata/tables.yaml index a52ea634f..9ba3be7e8 100644 --- a/hasura/metadata/tables.yaml +++ b/hasura/metadata/tables.yaml @@ -3698,6 +3698,7 @@ - deliverchecklist - depreciation_taxes - dms_allocation + - dms_id - driveable - employee_body - employee_csr @@ -3975,6 +3976,7 @@ - deliverchecklist - depreciation_taxes - dms_allocation + - dms_id - driveable - employee_body - employee_csr @@ -4264,6 +4266,7 @@ - deliverchecklist - depreciation_taxes - dms_allocation + - dms_id - driveable - employee_body - employee_csr diff --git a/hasura/migrations/1764704225560_alter_table_public_jobs_add_column_dms_id/down.sql b/hasura/migrations/1764704225560_alter_table_public_jobs_add_column_dms_id/down.sql new file mode 100644 index 000000000..7ea4450db --- /dev/null +++ b/hasura/migrations/1764704225560_alter_table_public_jobs_add_column_dms_id/down.sql @@ -0,0 +1,4 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- alter table "public"."jobs" add column "dms_id" text +-- null; diff --git a/hasura/migrations/1764704225560_alter_table_public_jobs_add_column_dms_id/up.sql b/hasura/migrations/1764704225560_alter_table_public_jobs_add_column_dms_id/up.sql new file mode 100644 index 000000000..d6eac4192 --- /dev/null +++ b/hasura/migrations/1764704225560_alter_table_public_jobs_add_column_dms_id/up.sql @@ -0,0 +1,2 @@ +alter table "public"."jobs" add column "dms_id" text + null; diff --git a/server/graphql-client/queries.js b/server/graphql-client/queries.js index 80a2871c8..a6c1704bc 100644 --- a/server/graphql-client/queries.js +++ b/server/graphql-client/queries.js @@ -2195,8 +2195,6 @@ mutation UPDATE_BILLS($billids: [uuid!]!, $bill: bills_set_input!, $logs: [expor } }`; - - exports.UPDATE_OLD_TRANSITION = `mutation UPDATE_OLD_TRANSITION($jobid: uuid!, $existingTransition: transitions_set_input!){ update_transitions(where:{jobid:{_eq:$jobid}, end:{_is_null:true }}, _set:$existingTransition){ @@ -3173,3 +3171,10 @@ mutation INSERT_MEDIA_ANALYTICS($mediaObject: media_analytics_insert_input!) { } } `; + +exports.SET_JOB_DMS_ID = `mutation SetJobDmsId($id: uuid!, $dms_id: String!) { + update_jobs_by_pk(pk_columns: { id: $id }, _set: { dms_id: $dms_id }) { + id + dms_id + } + }`; diff --git a/server/rr/rr-job-export.js b/server/rr/rr-job-export.js index 96c69e5c5..534ef61d3 100644 --- a/server/rr/rr-job-export.js +++ b/server/rr/rr-job-export.js @@ -4,6 +4,7 @@ const CreateRRLogEvent = require("./rr-logger-event"); const { extractRrResponsibilityCenters } = require("./rr-responsibility-centers"); const CdkCalculateAllocations = require("./rr-calculate-allocations").default; const { resolveRROpCodeFromBodyshop } = require("./rr-utils"); + /** * Derive RR status information from response object. * @param rrRes diff --git a/server/rr/rr-register-socket-events.js b/server/rr/rr-register-socket-events.js index 138e96f13..a039760f5 100644 --- a/server/rr/rr-register-socket-events.js +++ b/server/rr/rr-register-socket-events.js @@ -123,6 +123,47 @@ const getBodyshopForSocket = async ({ bodyshopId, socket }) => { return bodyshop; }; +/** + * GraphQL mutation to set job.dms_id + * @param socket + * @param jobId + * @param dmsId + * @returns {Promise} + */ +const setJobDmsIdForSocket = async ({ socket, jobId, dmsId }) => { + if (!jobId || !dmsId) { + CreateRRLogEvent(socket, "WARN", "setJobDmsIdForSocket called without jobId or dmsId", { + jobId, + dmsId + }); + return; + } + + try { + 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 auth token for setJobDmsIdForSocket"); + + const client = new GraphQLClient(endpoint, {}); + await client + .setHeaders({ Authorization: `Bearer ${token}` }) + .request(queries.SET_JOB_DMS_ID, { id: jobId, dms_id: String(dmsId) }); + + CreateRRLogEvent(socket, "INFO", "Linked job.dms_id to RR RO", { + jobId, + dmsId: String(dmsId) + }); + } catch (err) { + CreateRRLogEvent(socket, "ERROR", "Failed to set job.dms_id after RR create/update", { + jobId, + dmsId, + message: err?.message || String(err), + stack: err?.stack + }); + } +}; /** * Build advisors cache namespace and field * @param bodyshopId @@ -608,6 +649,22 @@ const registerRREvents = ({ socket, redisHelpers }) => { const outsdRoNo = data?.outsdRoNo ?? job?.ro_number ?? job?.id ?? null; + // ✅ Persist DMS RO number on the job (jobs.dms_id) + if (dmsRoNo) { + await setJobDmsIdForSocket({ socket, jobId: rid, dmsId: dmsRoNo }); + } else { + CreateRRLogEvent(socket, "WARN", "RR export succeeded but no DMS RO number was returned", { + jobId: rid, + resultPreview: { + roNo: result?.roNo, + data: { + dmsRoNo: data?.dmsRoNo, + outsdRoNo: data?.outsdRoNo + } + } + }); + } + await redisHelpers.setSessionTransactionData( socket.id, ns,