IO-3166-Global-Notifications-Part-2: checkpoint

This commit is contained in:
Dave Richer
2025-03-11 13:16:47 -04:00
parent d6df5af1a4
commit 8de7db60e6
2 changed files with 64 additions and 5 deletions

View File

@@ -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<Object>} 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.

View File

@@ -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: []
},