feature/IO-3225-Notifications-1.5: checkpoint

This commit is contained in:
Dave Richer
2025-05-06 13:38:42 -04:00
parent 1dd28af752
commit 020db91105
10 changed files with 79 additions and 39 deletions

View File

@@ -2765,6 +2765,17 @@ query GET_JOB_WATCHERS($jobid: uuid!) {
}
`;
exports.GET_JOB_WATCHERS_MINIMAL = `
query GET_JOB_WATCHERS_MINIMAL($jobid: uuid!) {
job_watchers(where: { jobid: { _eq: $jobid } }) {
user_email
user {
authid
}
}
}
`;
exports.GET_NOTIFICATION_ASSOCIATIONS = `
query GET_NOTIFICATION_ASSOCIATIONS($emails: [String!]!, $shopid: uuid!) {
associations(where: {
@@ -2802,6 +2813,7 @@ exports.GET_BODYSHOP_BY_ID = `
imexshopid
intellipay_config
state
notification_followers
}
}
`;
@@ -2931,10 +2943,6 @@ exports.INSERT_NEW_DOCUMENT = `
exports.GET_AUTOADD_NOTIFICATION_USERS = `
query GET_AUTOADD_NOTIFICATION_USERS($shopId: uuid!) {
bodyshops_by_pk(id: $shopId) {
id
notification_followers
}
associations(where: {
_and: [
{ shopid: { _eq: $shopId } },

View File

@@ -7,8 +7,13 @@
*/
const { client: gqlClient } = require("../graphql-client/graphql-client");
const queries = require("../graphql-client/queries");
const { isEmpty } = require("lodash");
const {
GET_JOB_WATCHERS_MINIMAL,
GET_AUTOADD_NOTIFICATION_USERS,
GET_EMPLOYEE_EMAILS,
INSERT_JOB_WATCHERS
} = require("../graphql-client/queries");
// If true, the user who commits the action will NOT receive notifications; if false, they will.
const FILTER_SELF_FROM_WATCHERS = process.env?.FILTER_SELF_FROM_WATCHERS !== "false";
@@ -22,11 +27,13 @@ const FILTER_SELF_FROM_WATCHERS = process.env?.FILTER_SELF_FROM_WATCHERS !== "fa
*/
const autoAddWatchers = async (req) => {
const { event, trigger } = req.body;
const { logger } = req;
const {
logger,
sessionUtils: { getBodyshopFromRedis }
} = req;
// Validate that this is an INSERT event
// Validate that this is an INSERT event, bail
if (trigger?.name !== "notifications_jobs_autoadd" || event.op !== "INSERT" || event.data.old) {
logger.log("Invalid event for auto-add watchers, skipping", "info", "notifications");
return;
}
@@ -42,8 +49,12 @@ const autoAddWatchers = async (req) => {
const hasuraUserId = event?.session_variables?.["x-hasura-user-id"];
try {
// Fetch auto-add users and notification followers
const autoAddData = await gqlClient.request(queries.GET_AUTOADD_NOTIFICATION_USERS, { shopId });
// Fetch bodyshop data from Redis
const bodyshopData = await getBodyshopFromRedis(shopId);
const notificationFollowers = bodyshopData?.notification_followers || [];
// Fetch auto-add users from associations
const autoAddData = await gqlClient.request(GET_AUTOADD_NOTIFICATION_USERS, { shopId });
// Get users with notifications_autoadd: true
const autoAddUsers =
@@ -53,12 +64,11 @@ const autoAddWatchers = async (req) => {
})) || [];
// Get users from notification_followers (array of employee IDs)
const notificationFollowers = autoAddData?.bodyshops_by_pk?.notification_followers || [];
let followerEmails = [];
if (notificationFollowers.length > 0) {
const validFollowers = notificationFollowers.filter((id) => id); // Remove null values
if (validFollowers.length > 0) {
const employeeData = await gqlClient.request(queries.GET_EMPLOYEE_EMAILS, {
const employeeData = await gqlClient.request(GET_EMPLOYEE_EMAILS, {
employeeIds: validFollowers,
shopid: shopId
});
@@ -80,12 +90,11 @@ const autoAddWatchers = async (req) => {
}, []);
if (isEmpty(usersToAdd)) {
logger.log(`No users to auto-add for jobId "${jobId}" (RO: ${roNumber})`, "info", "notifications");
return;
}
// Check existing watchers to avoid duplicates
const existingWatchersData = await gqlClient.request(queries.GET_JOB_WATCHERS, { jobid: jobId });
const existingWatchersData = await gqlClient.request(GET_JOB_WATCHERS_MINIMAL, { jobid: jobId });
const existingWatcherEmails = existingWatchersData?.job_watchers?.map((w) => w.user_email) || [];
// Filter out already existing watchers and optionally the user who created the job
@@ -104,23 +113,11 @@ const autoAddWatchers = async (req) => {
}));
if (isEmpty(newWatchers)) {
logger.log(
`No new watchers to add after filtering for jobId "${jobId}" (RO: ${roNumber})`,
"info",
"notifications"
);
return;
}
// Insert new watchers
await gqlClient.request(queries.INSERT_JOB_WATCHERS, { watchers: newWatchers });
logger.log(
`Added ${newWatchers.length} auto-add watchers for jobId "${jobId}" (RO: ${roNumber})`,
"info",
"notifications",
null,
{ addedEmails: newWatchers.map((w) => w.user_email) }
);
await gqlClient.request(INSERT_JOB_WATCHERS, { watchers: newWatchers });
} catch (error) {
logger.log("Error adding auto-add watchers", "error", "notifications", null, {
message: error?.message,