feature/IO-3096-GlobalNotifications - Checkpoint - In production, a user can not trigger their own scenario notification.
This commit is contained in:
@@ -2712,6 +2712,7 @@ query GET_JOB_WATCHERS($jobid: uuid!) {
|
||||
nodes {
|
||||
user_email
|
||||
user {
|
||||
authid
|
||||
employee {
|
||||
id
|
||||
first_name
|
||||
|
||||
@@ -15,6 +15,9 @@ const { getMatchingScenarios } = require("./scenarioMapperr");
|
||||
const { dispatchEmailsToQueue } = require("./queues/emailQueue");
|
||||
const { dispatchAppsToQueue } = require("./queues/appQueue");
|
||||
|
||||
// If true, the user who commits the action will NOT receive notifications; if false, they will.
|
||||
const FILTER_SELF_FROM_WATCHERS = (() => process.env.NODE_ENV === "production")();
|
||||
|
||||
/**
|
||||
* Parses an event and determines matching scenarios for notifications.
|
||||
* Queries job watchers and notification settings before triggering scenario builders.
|
||||
@@ -28,6 +31,14 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
const { event, trigger, table } = req.body;
|
||||
const { logger } = req;
|
||||
|
||||
// Validate we know what user commited the action that fired the parser
|
||||
const hasuraUserId = event?.session_variables?.["x-hasura-user-id"];
|
||||
|
||||
// Bail if we don't know
|
||||
if (!hasuraUserId) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate that required fields are present in the request body
|
||||
if (!event?.data || !trigger || !table) {
|
||||
throw new Error("Missing required request fields: event data, trigger, or table.");
|
||||
@@ -48,13 +59,18 @@ const scenarioParser = async (req, jobIdField) => {
|
||||
});
|
||||
|
||||
// Transform watcher data into a simplified format with email and employee details
|
||||
const jobWatchers = watcherData?.job_watchers_aggregate?.nodes?.map((watcher) => ({
|
||||
let jobWatchers = watcherData?.job_watchers_aggregate?.nodes?.map((watcher) => ({
|
||||
email: watcher.user_email,
|
||||
firstName: watcher?.user?.employee?.first_name,
|
||||
lastName: watcher?.user?.employee?.last_name,
|
||||
employeeId: watcher?.user?.employee?.id
|
||||
employeeId: watcher?.user?.employee?.id,
|
||||
authId: watcher?.user?.authid
|
||||
}));
|
||||
|
||||
if (FILTER_SELF_FROM_WATCHERS) {
|
||||
jobWatchers = jobWatchers.filter((watcher) => watcher.authId !== hasuraUserId);
|
||||
}
|
||||
|
||||
// Exit early if no job watchers are found for this job
|
||||
if (isEmpty(jobWatchers)) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user