feature/IO-3225-Notifications-1.5: Final Refactoring / Optimization
This commit is contained in:
@@ -10,8 +10,7 @@ const { client: gqlClient } = require("../graphql-client/graphql-client");
|
||||
const { isEmpty } = require("lodash");
|
||||
const {
|
||||
GET_JOB_WATCHERS_MINIMAL,
|
||||
GET_AUTOADD_NOTIFICATION_USERS,
|
||||
GET_EMPLOYEE_EMAILS,
|
||||
GET_NOTIFICATION_WATCHERS,
|
||||
INSERT_JOB_WATCHERS
|
||||
} = require("../graphql-client/queries");
|
||||
|
||||
@@ -53,33 +52,30 @@ const autoAddWatchers = async (req) => {
|
||||
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 });
|
||||
// Execute queries in parallel
|
||||
const [notificationData, existingWatchersData] = await Promise.all([
|
||||
gqlClient.request(GET_NOTIFICATION_WATCHERS, {
|
||||
shopId,
|
||||
employeeIds: notificationFollowers.filter((id) => id)
|
||||
}),
|
||||
gqlClient.request(GET_JOB_WATCHERS_MINIMAL, { jobid: jobId })
|
||||
]);
|
||||
|
||||
// Get users with notifications_autoadd: true
|
||||
const autoAddUsers =
|
||||
autoAddData?.associations?.map((assoc) => ({
|
||||
notificationData?.associations?.map((assoc) => ({
|
||||
email: assoc.useremail,
|
||||
associationId: assoc.id
|
||||
})) || [];
|
||||
|
||||
// Get users from notification_followers (array of employee IDs)
|
||||
let followerEmails = [];
|
||||
if (notificationFollowers.length > 0) {
|
||||
const validFollowers = notificationFollowers.filter((id) => id); // Remove null values
|
||||
if (validFollowers.length > 0) {
|
||||
const employeeData = await gqlClient.request(GET_EMPLOYEE_EMAILS, {
|
||||
employeeIds: validFollowers,
|
||||
shopid: shopId
|
||||
});
|
||||
followerEmails = employeeData.employees
|
||||
.filter((e) => e.user_email)
|
||||
.map((e) => ({
|
||||
email: e.user_email,
|
||||
associationId: null
|
||||
}));
|
||||
}
|
||||
}
|
||||
// Get users from notification_followers
|
||||
const followerEmails =
|
||||
notificationData?.employees
|
||||
?.filter((e) => e.user_email)
|
||||
?.map((e) => ({
|
||||
email: e.user_email,
|
||||
associationId: null
|
||||
})) || [];
|
||||
|
||||
// Combine and deduplicate emails (use email as the unique key)
|
||||
const usersToAdd = [...autoAddUsers, ...followerEmails].reduce((acc, user) => {
|
||||
@@ -94,7 +90,6 @@ const autoAddWatchers = async (req) => {
|
||||
}
|
||||
|
||||
// Check existing watchers to avoid duplicates
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user