feature/IO-3255-simplified-parts-management - Merge release / resolve conflicts
This commit is contained in:
@@ -81,7 +81,7 @@ const jobLifecycle = async (req, res) => {
|
||||
const finalTotal = Object.values(flatGroupedAllDurations).reduce((total, statusArr) => {
|
||||
return total + statusArr.reduce((acc, curr) => acc + curr.value, 0);
|
||||
}, 0);
|
||||
|
||||
|
||||
Object.keys(flatGroupedAllDurations).forEach((status) => {
|
||||
const value = flatGroupedAllDurations[status].reduce((acc, curr) => acc + curr.value, 0);
|
||||
const humanReadable = durationToHumanReadable(moment.duration(value));
|
||||
|
||||
@@ -50,7 +50,12 @@ const autoAddWatchers = async (req) => {
|
||||
try {
|
||||
// Fetch bodyshop data from Redis
|
||||
const bodyshopData = await getBodyshopFromRedis(shopId);
|
||||
const notificationFollowers = bodyshopData?.notification_followers || [];
|
||||
let notificationFollowers = bodyshopData?.notification_followers;
|
||||
|
||||
// Bail if notification_followers is missing or not an array
|
||||
if (!notificationFollowers || !Array.isArray(notificationFollowers)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute queries in parallel
|
||||
const [notificationData, existingWatchersData] = await Promise.all([
|
||||
|
||||
@@ -145,15 +145,70 @@ const handleNotesChange = async (req, res) =>
|
||||
const handlePaymentsChange = async (req, res) =>
|
||||
processNotificationEvent(req, res, "req.body.event.new.jobid", "Payments Changed Notification Event Handled.");
|
||||
|
||||
/**
|
||||
* Handle task socket emit.
|
||||
* @param req
|
||||
*/
|
||||
const handleTaskSocketEmit = (req) => {
|
||||
const {
|
||||
logger,
|
||||
ioRedis,
|
||||
ioHelpers: { getBodyshopRoom }
|
||||
} = req;
|
||||
const event = req.body.event;
|
||||
const op = event.op;
|
||||
let taskData;
|
||||
let type;
|
||||
let bodyshopId;
|
||||
|
||||
if (op === "INSERT") {
|
||||
taskData = event.data.new;
|
||||
if (taskData.deleted) {
|
||||
logger.log("tasks-event-insert-deleted", "warn", "notifications", null, { id: taskData.id });
|
||||
} else {
|
||||
type = "task-created";
|
||||
bodyshopId = taskData.bodyshopid;
|
||||
}
|
||||
} else if (op === "UPDATE") {
|
||||
const newData = event.data.new;
|
||||
const oldData = event.data.old;
|
||||
taskData = newData;
|
||||
bodyshopId = newData.bodyshopid;
|
||||
|
||||
if (newData.deleted && !oldData.deleted) {
|
||||
type = "task-deleted";
|
||||
taskData = { id: newData.id, assigned_to: newData.assigned_to };
|
||||
} else if (!newData.deleted && oldData.deleted) {
|
||||
type = "task-created";
|
||||
} else if (!newData.deleted) {
|
||||
type = "task-updated";
|
||||
}
|
||||
} else {
|
||||
logger.log("tasks-event-unknown-op", "warn", "notifications", null, { op });
|
||||
}
|
||||
|
||||
if (bodyshopId && ioRedis && type) {
|
||||
const room = getBodyshopRoom(bodyshopId);
|
||||
ioRedis.to(room).emit("bodyshop-message", { type, payload: taskData });
|
||||
logger.log("tasks-event-emitted", "info", "notifications", null, { type, bodyshopId });
|
||||
} else if (type) {
|
||||
logger.log("tasks-event-missing-data", "error", "notifications", null, { bodyshopId, hasIo: !!ioRedis, type });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle tasks change notifications.
|
||||
* Note: this also handles task center notifications.
|
||||
*
|
||||
* @param {Object} req - Express request object.
|
||||
* @param {Object} res - Express response object.
|
||||
* @returns {Promise<Object>} JSON response with a success message.
|
||||
*/
|
||||
const handleTasksChange = async (req, res) =>
|
||||
const handleTasksChange = async (req, res) => {
|
||||
// Handle Notification Event
|
||||
processNotificationEvent(req, res, "req.body.event.new.jobid", "Tasks Notifications Event Handled.");
|
||||
handleTaskSocketEmit(req);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle time tickets change notifications.
|
||||
|
||||
@@ -26,6 +26,7 @@ const redisSocketEvents = ({
|
||||
try {
|
||||
const user = await admin.auth().verifyIdToken(token);
|
||||
socket.user = user;
|
||||
socket.bodyshopId = bodyshopId;
|
||||
await addUserSocketMapping(user.email, socket.id, bodyshopId);
|
||||
next();
|
||||
} catch (error) {
|
||||
@@ -55,12 +56,8 @@ const redisSocketEvents = ({
|
||||
return;
|
||||
}
|
||||
socket.user = user;
|
||||
socket.bodyshopId = bodyshopId;
|
||||
await refreshUserSocketTTL(user.email, bodyshopId);
|
||||
createLogEvent(
|
||||
socket,
|
||||
"debug",
|
||||
`Token updated successfully for socket ID: ${socket.id} (bodyshop: ${bodyshopId})`
|
||||
);
|
||||
socket.emit("token-updated", { success: true });
|
||||
} catch (error) {
|
||||
if (error.code === "auth/id-token-expired") {
|
||||
@@ -82,7 +79,6 @@ const redisSocketEvents = ({
|
||||
try {
|
||||
const room = getBodyshopRoom(bodyshopUUID);
|
||||
socket.join(room);
|
||||
// createLogEvent(socket, "debug", `Client joined bodyshop room: ${room}`);
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "error", `Error joining room: ${error}`);
|
||||
}
|
||||
@@ -92,7 +88,6 @@ const redisSocketEvents = ({
|
||||
try {
|
||||
const room = getBodyshopRoom(bodyshopUUID);
|
||||
socket.leave(room);
|
||||
createLogEvent(socket, "debug", `Client left bodyshop room: ${room}`);
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "error", `Error joining room: ${error}`);
|
||||
}
|
||||
@@ -102,8 +97,6 @@ const redisSocketEvents = ({
|
||||
try {
|
||||
const room = getBodyshopRoom(bodyshopUUID);
|
||||
io.to(room).emit("bodyshop-message", message);
|
||||
// We do not need this as these can be debugged live
|
||||
// createLogEvent(socket, "debug", `Broadcast message to bodyshop ${room}`);
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "error", `Error getting room: ${error}`);
|
||||
}
|
||||
@@ -200,11 +193,6 @@ const redisSocketEvents = ({
|
||||
io.to(socketId).emit("sync-notification-read", { notificationId, timestamp });
|
||||
}
|
||||
});
|
||||
createLogEvent(
|
||||
socket,
|
||||
"debug",
|
||||
`Synced notification ${notificationId} read for ${userEmail} in bodyshop ${bodyshopId}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "error", `Error syncing notification read: ${error.message}`);
|
||||
@@ -223,7 +211,6 @@ const redisSocketEvents = ({
|
||||
io.to(socketId).emit("sync-all-notifications-read", { timestamp });
|
||||
}
|
||||
});
|
||||
createLogEvent(socket, "debug", `Synced all notifications read for ${email} in bodyshop ${bodyshopId}`);
|
||||
}
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "error", `Error syncing all notifications read: ${error.message}`);
|
||||
@@ -231,12 +218,34 @@ const redisSocketEvents = ({
|
||||
});
|
||||
};
|
||||
|
||||
// Task Events
|
||||
const registerTaskEvents = (socket) => {
|
||||
socket.on("task-created", (payload) => {
|
||||
if (!payload) return;
|
||||
const room = getBodyshopRoom(socket.bodyshopId);
|
||||
io.to(room).emit("bodyshop-message", { type: "task-created", payload });
|
||||
});
|
||||
|
||||
socket.on("task-updated", (payload) => {
|
||||
if (!payload) return;
|
||||
const room = getBodyshopRoom(socket.bodyshopId);
|
||||
io.to(room).emit("bodyshop-message", { type: "task-updated", payload });
|
||||
});
|
||||
|
||||
socket.on("task-deleted", (payload) => {
|
||||
if (!payload || !payload.id) return;
|
||||
const room = getBodyshopRoom(socket.bodyshopId);
|
||||
io.to(room).emit("bodyshop-message", { type: "task-deleted", payload });
|
||||
});
|
||||
};
|
||||
|
||||
// Call Handlers
|
||||
registerRoomAndBroadcastEvents(socket);
|
||||
registerUpdateEvents(socket);
|
||||
registerMessagingEvents(socket);
|
||||
registerDisconnectEvents(socket);
|
||||
registerSyncEvents(socket);
|
||||
registerTaskEvents(socket);
|
||||
};
|
||||
|
||||
// Associate Middleware and Handlers
|
||||
|
||||
Reference in New Issue
Block a user