/** * @fileoverview Notification event handlers. * This module exports functions to handle various notification events. * Each handler optionally calls the scenarioParser and logs errors if they occur, * then returns a JSON response with a success message. */ const scenarioParser = require("./scenarioParser"); /** * Processes a notification event by invoking the scenario parser. * The scenarioParser is intentionally not awaited so that the response is sent immediately. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @param {string} parserPath - The key path to be passed to scenarioParser. * @param {string} successMessage - The message to return on success. * @returns {Promise} A promise that resolves to an Express JSON response. */ async function processNotificationEvent(req, res, parserPath, successMessage) { const { logger } = req; // Call scenarioParser but don't await it; log any error that occurs. scenarioParser(req, parserPath).catch((error) => { logger.log("notifications-error", "error", "notifications", null, { message: error?.message, stack: error?.stack }); }); return res.status(200).json({ message: successMessage }); } /** * Handle job change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handleJobsChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.id", "Job Notifications Event Handled."); /** * Handle bills change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handleBillsChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.jobid", "Bills Changed Notification Event Handled."); /** * Handle documents change notifications. * * @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."); /** * Handle job lines change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handleJobLinesChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.jobid", "JobLines Change Notifications Event Handled."); /** * Handle notes change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handleNotesChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.jobid", "Notes Changed Notification Event Handled."); /** * Handle parts dispatch change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Object} JSON response with a success message. */ const handlePartsDispatchChange = (req, res) => res.status(200).json({ message: "Parts Dispatch change handled." }); /** * Handle parts order change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Object} JSON response with a success message. */ const handlePartsOrderChange = (req, res) => res.status(200).json({ message: "Parts Order change handled." }); /** * Handle payments change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handlePaymentsChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.jobid", "Payments Changed Notification Event Handled."); /** * Handle tasks change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handleTasksChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.jobid", "Tasks Notifications Event Handled."); /** * Handle time tickets change notifications. * * @param {Object} req - Express request object. * @param {Object} res - Express response object. * @returns {Promise} JSON response with a success message. */ const handleTimeTicketsChange = async (req, res) => processNotificationEvent(req, res, "req.body.event.new.jobid", "Time Tickets Changed Notification Event Handled."); module.exports = { handleJobsChange, handleBillsChange, handleDocumentsChange, handleJobLinesChange, handleNotesChange, handlePartsDispatchChange, handlePartsOrderChange, handlePaymentsChange, handleTasksChange, handleTimeTicketsChange };