feature/IO-3225-Notifications-1.5: Final Refactoring / Optimization
This commit is contained in:
@@ -9,6 +9,7 @@ query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID($mssid: String!, $phone: String!) {
|
|||||||
}
|
}
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
// Unused
|
||||||
exports.GET_JOB_BY_RO_NUMBER = `
|
exports.GET_JOB_BY_RO_NUMBER = `
|
||||||
query GET_JOB_BY_RO_NUMBER($ro_number: String!) {
|
query GET_JOB_BY_RO_NUMBER($ro_number: String!) {
|
||||||
jobs(where:{ro_number:{_eq:$ro_number}}) {
|
jobs(where:{ro_number:{_eq:$ro_number}}) {
|
||||||
@@ -1773,6 +1774,7 @@ exports.QUERY_JOB_COSTING_DETAILS_MULTI = ` query QUERY_JOB_COSTING_DETAILS_MULT
|
|||||||
}
|
}
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
// Exists in Commented out Query
|
||||||
exports.INSERT_IOEVENT = ` mutation INSERT_IOEVENT($event: ioevents_insert_input!) {
|
exports.INSERT_IOEVENT = ` mutation INSERT_IOEVENT($event: ioevents_insert_input!) {
|
||||||
insert_ioevents_one(object: $event) {
|
insert_ioevents_one(object: $event) {
|
||||||
id
|
id
|
||||||
@@ -2765,17 +2767,6 @@ 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 = `
|
exports.GET_NOTIFICATION_ASSOCIATIONS = `
|
||||||
query GET_NOTIFICATION_ASSOCIATIONS($emails: [String!]!, $shopid: uuid!) {
|
query GET_NOTIFICATION_ASSOCIATIONS($emails: [String!]!, $shopid: uuid!) {
|
||||||
associations(where: {
|
associations(where: {
|
||||||
@@ -2941,8 +2932,16 @@ exports.INSERT_NEW_DOCUMENT = `
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports.GET_AUTOADD_NOTIFICATION_USERS = `
|
exports.INSERT_JOB_WATCHERS = `
|
||||||
query GET_AUTOADD_NOTIFICATION_USERS($shopId: uuid!) {
|
mutation INSERT_JOB_WATCHERS($watchers: [job_watchers_insert_input!]!) {
|
||||||
|
insert_job_watchers(objects: $watchers, on_conflict: { constraint: job_watchers_pkey, update_columns: [] }) {
|
||||||
|
affected_rows
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports.GET_NOTIFICATION_WATCHERS = `
|
||||||
|
query GET_NOTIFICATION_WATCHERS($shopId: uuid!, $employeeIds: [uuid!]!) {
|
||||||
associations(where: {
|
associations(where: {
|
||||||
_and: [
|
_and: [
|
||||||
{ shopid: { _eq: $shopId } },
|
{ shopid: { _eq: $shopId } },
|
||||||
@@ -2953,43 +2952,19 @@ exports.GET_AUTOADD_NOTIFICATION_USERS = `
|
|||||||
id
|
id
|
||||||
useremail
|
useremail
|
||||||
}
|
}
|
||||||
}
|
employees(where: { id: { _in: $employeeIds }, shopid: { _eq: $shopId }, active: { _eq: true } }) {
|
||||||
`;
|
|
||||||
|
|
||||||
exports.INSERT_JOB_WATCHERS = `
|
|
||||||
mutation INSERT_JOB_WATCHERS($watchers: [job_watchers_insert_input!]!) {
|
|
||||||
insert_job_watchers(objects: $watchers) {
|
|
||||||
affected_rows
|
|
||||||
returning {
|
|
||||||
user_email
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
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
|
user_email
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports.GET_NOTIFICATION_ASSOCIATIONS_BY_EMAILS = `
|
exports.GET_JOB_WATCHERS_MINIMAL = `
|
||||||
query GET_NOTIFICATION_ASSOCIATIONS_BY_EMAILS($emails: [String!]!, $shopid: uuid!) {
|
query GET_JOB_WATCHERS_MINIMAL($jobid: uuid!) {
|
||||||
associations(where: { useremail: { _in: $emails }, shopid: { _eq: $shopid }, active: { _eq: true } }) {
|
job_watchers(where: { jobid: { _eq: $jobid } }) {
|
||||||
id
|
user_email
|
||||||
useremail
|
user {
|
||||||
|
authid
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ const { client: gqlClient } = require("../graphql-client/graphql-client");
|
|||||||
const { isEmpty } = require("lodash");
|
const { isEmpty } = require("lodash");
|
||||||
const {
|
const {
|
||||||
GET_JOB_WATCHERS_MINIMAL,
|
GET_JOB_WATCHERS_MINIMAL,
|
||||||
GET_AUTOADD_NOTIFICATION_USERS,
|
GET_NOTIFICATION_WATCHERS,
|
||||||
GET_EMPLOYEE_EMAILS,
|
|
||||||
INSERT_JOB_WATCHERS
|
INSERT_JOB_WATCHERS
|
||||||
} = require("../graphql-client/queries");
|
} = require("../graphql-client/queries");
|
||||||
|
|
||||||
@@ -53,33 +52,30 @@ const autoAddWatchers = async (req) => {
|
|||||||
const bodyshopData = await getBodyshopFromRedis(shopId);
|
const bodyshopData = await getBodyshopFromRedis(shopId);
|
||||||
const notificationFollowers = bodyshopData?.notification_followers || [];
|
const notificationFollowers = bodyshopData?.notification_followers || [];
|
||||||
|
|
||||||
// Fetch auto-add users from associations
|
// Execute queries in parallel
|
||||||
const autoAddData = await gqlClient.request(GET_AUTOADD_NOTIFICATION_USERS, { shopId });
|
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
|
// Get users with notifications_autoadd: true
|
||||||
const autoAddUsers =
|
const autoAddUsers =
|
||||||
autoAddData?.associations?.map((assoc) => ({
|
notificationData?.associations?.map((assoc) => ({
|
||||||
email: assoc.useremail,
|
email: assoc.useremail,
|
||||||
associationId: assoc.id
|
associationId: assoc.id
|
||||||
})) || [];
|
})) || [];
|
||||||
|
|
||||||
// Get users from notification_followers (array of employee IDs)
|
// Get users from notification_followers
|
||||||
let followerEmails = [];
|
const followerEmails =
|
||||||
if (notificationFollowers.length > 0) {
|
notificationData?.employees
|
||||||
const validFollowers = notificationFollowers.filter((id) => id); // Remove null values
|
?.filter((e) => e.user_email)
|
||||||
if (validFollowers.length > 0) {
|
?.map((e) => ({
|
||||||
const employeeData = await gqlClient.request(GET_EMPLOYEE_EMAILS, {
|
email: e.user_email,
|
||||||
employeeIds: validFollowers,
|
associationId: null
|
||||||
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)
|
// Combine and deduplicate emails (use email as the unique key)
|
||||||
const usersToAdd = [...autoAddUsers, ...followerEmails].reduce((acc, user) => {
|
const usersToAdd = [...autoAddUsers, ...followerEmails].reduce((acc, user) => {
|
||||||
@@ -94,7 +90,6 @@ const autoAddWatchers = async (req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check existing watchers to avoid duplicates
|
// 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) || [];
|
const existingWatcherEmails = existingWatchersData?.job_watchers?.map((w) => w.user_email) || [];
|
||||||
|
|
||||||
// Filter out already existing watchers and optionally the user who created the job
|
// Filter out already existing watchers and optionally the user who created the job
|
||||||
|
|||||||
Reference in New Issue
Block a user