const { isObject } = require("lodash"); const jobUpdated = async (req, res) => { const { ioRedis, logger, ioHelpers } = req; if (!req?.body?.event?.data?.new || !isObject(req?.body?.event?.data?.new)) { logger.log("job-update-error", "ERROR", req.user?.email, null, { message: `Malformed Job Update request sent from Hasura`, body: req?.body }); return res.json({ status: "error", message: `Malformed Job Update request sent from Hasura` }); } logger.log("job-update", "INFO", req.user?.email, null, { message: `Job updated event received from Hasura`, jobid: req?.body?.event?.data?.new?.id }); const updatedJob = req.body.event.data.new; const bodyshopID = updatedJob.shopid; // Emit the job-updated event only to the room corresponding to the bodyshop ioRedis.to(ioHelpers.getBodyshopRoom(bodyshopID)).emit("production-job-updated", updatedJob); return res.json({ message: "Job updated and event emitted" }); }; module.exports = jobUpdated;