From 8de7db60e6cfbfaef9dd5b2eaca6d6a9524a41f5 Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Tue, 11 Mar 2025 13:16:47 -0400 Subject: [PATCH] IO-3166-Global-Notifications-Part-2: checkpoint --- server/notifications/eventHandlers.js | 60 +++++++++++++++++++++++- server/notifications/scenarioBuilders.js | 9 ++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/server/notifications/eventHandlers.js b/server/notifications/eventHandlers.js index 45de5dc3e..315cfe705 100644 --- a/server/notifications/eventHandlers.js +++ b/server/notifications/eventHandlers.js @@ -50,13 +50,69 @@ const handleBillsChange = async (req, res) => /** * Handle documents change notifications. + * Processes both old and new job IDs if the document was moved between jobs. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ -const handleDocumentsChange = async (req, res) => - processNotificationEvent(req, res, "req.body.event.new.jobid", "Documents Change Notifications Event Handled."); +const handleDocumentsChange = async (req, res) => { + const { logger } = req; + const newJobId = req.body?.event?.data?.new?.jobid; + const oldJobId = req.body?.event?.data?.old?.jobid; + + // If jobid changed (document moved between jobs), we need to notify both jobs + if (oldJobId && newJobId && oldJobId !== newJobId) { + // Process notification for new job ID + scenarioParser(req, "req.body.event.new.jobid").catch((error) => { + logger.log("notifications-error", "error", "notifications", null, { + message: error?.message, + stack: error?.stack + }); + }); + + // Create a modified request for old job ID + const oldJobReq = { + body: { + ...req.body, + event: { + ...req.body.event, + data: { + new: { + ...req.body.event.data.old, + // Add a flag to indicate this document was moved away + _documentMoved: true, + _movedToJob: newJobId + }, + old: null + } + } + }, + logger, + sessionUtils: req.sessionUtils + }; + + // Process notification for old job ID using the modified request + scenarioParser(oldJobReq, "req.body.event.new.jobid").catch((error) => { + logger.log("notifications-error", "error", "notifications", null, { + message: error?.message, + stack: error?.stack + }); + }); + + return res.status(200).json({ message: "Documents Change Notifications Event Handled for both jobs." }); + } + + // Otherwise just process the new job ID + scenarioParser(req, "req.body.event.new.jobid").catch((error) => { + logger.log("notifications-error", "error", "notifications", null, { + message: error?.message, + stack: error?.stack + }); + }); + + return res.status(200).json({ message: "Documents Change Notifications Event Handled." }); +}; /** * Handle job lines change notifications. diff --git a/server/notifications/scenarioBuilders.js b/server/notifications/scenarioBuilders.js index 4c1083415..276cff882 100644 --- a/server/notifications/scenarioBuilders.js +++ b/server/notifications/scenarioBuilders.js @@ -261,10 +261,12 @@ const newMediaAddedReassignedBuilder = (data) => { // Determine the action let action; - if (data.isNew) { + if (data?.data?._documentMoved) { + action = "moved to another Job"; // Special case for document moved from this job + } else if (data.isNew) { action = "added"; // New media } else if (data.changedFields?.jobid && data.changedFields.jobid.old !== data.changedFields.jobid.new) { - action = "reassigned to Job"; + action = "moved to this Job"; } else { action = "updated"; } @@ -281,7 +283,8 @@ const newMediaAddedReassignedBuilder = (data) => { body, variables: { mediaType, - action + action, + movedToJob: data?.data?._movedToJob }, recipients: [] },