feature/IO-3225-Notifications-1.5: Finish
This commit is contained in:
@@ -2958,3 +2958,30 @@ exports.INSERT_JOB_WATCHERS = `
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.GET_NOTIFICATION_ASSOCIATIONS_BY_IDS = `
|
||||
query GET_NOTIFICATION_ASSOCIATIONS_BY_IDS($associationIds: [uuid!]!, $shopid: uuid!) {
|
||||
associations(where: { id: { _in: $associationIds }, shopid: { _eq: $shopid }, active: { _eq: true } }) {
|
||||
id
|
||||
useremail
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.GET_EMPLOYEE_EMAILS = `
|
||||
query GET_EMPLOYEE_EMAILS($employeeIds: [uuid!]!, $shopid: uuid!) {
|
||||
employees(where: { id: { _in: $employeeIds }, shopid: { _eq: $shopid }, active: { _eq: true } }) {
|
||||
id
|
||||
user_email
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.GET_NOTIFICATION_ASSOCIATIONS_BY_EMAILS = `
|
||||
query GET_NOTIFICATION_ASSOCIATIONS_BY_EMAILS($emails: [String!]!, $shopid: uuid!) {
|
||||
associations(where: { useremail: { _in: $emails }, shopid: { _eq: $shopid }, active: { _eq: true } }) {
|
||||
id
|
||||
useremail
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -52,21 +52,23 @@ const autoAddWatchers = async (req) => {
|
||||
associationId: assoc.id
|
||||
})) || [];
|
||||
|
||||
// Get users from notification_followers (array of association IDs)
|
||||
// Get users from notification_followers (array of employee IDs)
|
||||
const notificationFollowers = autoAddData?.bodyshops_by_pk?.notification_followers || [];
|
||||
let followerEmails = [];
|
||||
if (notificationFollowers.length > 0) {
|
||||
// Fetch associations for notification_followers
|
||||
const followerAssociations = await gqlClient.request(queries.GET_NOTIFICATION_ASSOCIATIONS, {
|
||||
emails: [], // Filter by association IDs
|
||||
shopid: shopId
|
||||
});
|
||||
followerEmails = followerAssociations.associations
|
||||
.filter((assoc) => notificationFollowers.includes(assoc.id))
|
||||
.map((assoc) => ({
|
||||
email: assoc.useremail,
|
||||
associationId: assoc.id
|
||||
}));
|
||||
const validFollowers = notificationFollowers.filter((id) => id); // Remove null values
|
||||
if (validFollowers.length > 0) {
|
||||
const employeeData = await gqlClient.request(queries.GET_EMPLOYEE_EMAILS, {
|
||||
employeeIds: validFollowers,
|
||||
shopid: shopId
|
||||
});
|
||||
followerEmails = employeeData.employees
|
||||
.filter((e) => e.user_email)
|
||||
.map((e) => ({
|
||||
email: e.user_email,
|
||||
associationId: null
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// Combine and deduplicate emails (use email as the unique key)
|
||||
@@ -91,7 +93,6 @@ const autoAddWatchers = async (req) => {
|
||||
.filter((user) => !existingWatcherEmails.includes(user.email))
|
||||
.filter((user) => {
|
||||
if (FILTER_SELF_FROM_WATCHERS && hasuraUserRole === "user") {
|
||||
// Fetch user email for hasuraUserId to compare
|
||||
const userData = existingWatchersData?.job_watchers?.find((w) => w.user?.authid === hasuraUserId);
|
||||
return userData ? user.email !== userData.user_email : true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user