diff --git a/hasura/metadata/tables.yaml b/hasura/metadata/tables.yaml index c9b1922ca..68dab7c39 100644 --- a/hasura/metadata/tables.yaml +++ b/hasura/metadata/tables.yaml @@ -683,6 +683,31 @@ - exported: _eq: false event_triggers: + - name: notifications_bills + definition: + enable_manual: false + insert: + columns: '*' + retry_conf: + interval_sec: 10 + num_retries: 0 + timeout_sec: 60 + webhook_from_env: HASURA_API_URL + headers: + - name: event-secret + value_from_env: EVENT_SECRET + request_transform: + body: + action: transform + template: |- + { + "success": true + } + method: POST + query_params: {} + template_engine: Kriti + url: '{{$base_url}}/job/events/handleBillsChange' + version: 2 - name: os_bills definition: delete: @@ -2813,7 +2838,7 @@ object_relationships: - name: job using: - foreign_key_constraint_on: job_id + foreign_key_constraint_on: jobid - name: user using: foreign_key_constraint_on: user_email @@ -2832,7 +2857,7 @@ - user_email - created_at - id - - job_id + - jobid comment: "" select_permissions: - role: user @@ -2841,7 +2866,7 @@ - user_email - created_at - id - - job_id + - jobid filter: user: _and: @@ -2858,7 +2883,7 @@ - user_email - created_at - id - - job_id + - jobid filter: user: _and: @@ -4402,6 +4427,58 @@ template_engine: Kriti url: '{{$base_url}}/record-handler/arms' version: 2 + - name: notifications_jobs + definition: + enable_manual: false + insert: + columns: '*' + update: + columns: + - queued_for_parts + - employee_prep + - clm_total + - towin + - employee_body + - converted + - scheduled_in + - scheduled_completion + - scheduled_delivery + - actual_delivery + - actual_completion + - alt_transport + - date_exported + - status + - employee_csr + - actual_in + - deliverchecklist + - comment + - job_totals + - employee_refinish + - inproduction + - production_vars + - intakechecklist + - cieca_ttl + - date_invoiced + retry_conf: + interval_sec: 10 + num_retries: 0 + timeout_sec: 60 + webhook_from_env: HASURA_API_URL + headers: + - name: event-secret + value_from_env: EVENT_SECRET + request_transform: + body: + action: transform + template: |- + { + "success": true + } + method: POST + query_params: {} + template_engine: Kriti + url: '{{$base_url}}/job/events/handleJobsChange' + version: 2 - name: os_jobs definition: delete: @@ -4751,46 +4828,21 @@ object_relationships: - name: association using: - foreign_key_constraint_on: association_id + foreign_key_constraint_on: associationid - name: job using: - foreign_key_constraint_on: job_id - insert_permissions: - - role: user - permission: - check: - association: - _and: - - active: - _eq: true - - user: - authid: - _eq: X-Hasura-User-Id - columns: - - association_id - - created_at - - fcm_data - - fcm_message - - fcm_title - - id - - job_id - - meta - - read - - ui_translation_meta - - ui_translation_string - - updated_at - comment: "" + foreign_key_constraint_on: jobid select_permissions: - role: user permission: columns: - - association_id + - associationid - created_at - fcm_data - fcm_message - fcm_title - id - - job_id + - jobid - meta - read - ui_translation_meta @@ -4809,18 +4861,8 @@ - role: user permission: columns: - - association_id - - created_at - - fcm_data - - fcm_message - - fcm_title - - id - - job_id - meta - read - - ui_translation_meta - - ui_translation_string - - updated_at filter: association: _and: @@ -5071,6 +5113,32 @@ - active: _eq: true check: null + event_triggers: + - name: notifications_parts_dispatch + definition: + enable_manual: false + insert: + columns: '*' + retry_conf: + interval_sec: 10 + num_retries: 0 + timeout_sec: 60 + webhook_from_env: HASURA_API_URL + headers: + - name: event-secret + value_from_env: EVENT_SECRET + request_transform: + body: + action: transform + template: |- + { + "success": true + } + method: POST + query_params: {} + template_engine: Kriti + url: '{{$base_url}}/job/events/handlePartsDispatchChange' + version: 2 - table: name: parts_dispatch_lines schema: public @@ -6049,6 +6117,8 @@ update: columns: - assigned_to + - completed + - description retry_conf: interval_sec: 10 num_retries: 3 @@ -6203,8 +6273,6 @@ enable_manual: false insert: columns: '*' - update: - columns: '*' retry_conf: interval_sec: 10 num_retries: 0 @@ -6214,10 +6282,16 @@ - name: event-secret value_from_env: EVENT_SECRET request_transform: + body: + action: transform + template: |- + { + "success": true + } method: POST query_params: {} template_engine: Kriti - url: '{{$base_url}}/events/handleTimeTicketsChange' + url: '{{$base_url}}/job/events/handleTimeTicketsChange' version: 2 - table: name: transitions diff --git a/hasura/migrations/1736532531529_notification_settings_to_null/down.sql b/hasura/migrations/1736532531529_notification_settings_to_null/down.sql new file mode 100644 index 000000000..9b79fc484 --- /dev/null +++ b/hasura/migrations/1736532531529_notification_settings_to_null/down.sql @@ -0,0 +1,3 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- update associations set notification_settings = '{}'; diff --git a/hasura/migrations/1736532531529_notification_settings_to_null/up.sql b/hasura/migrations/1736532531529_notification_settings_to_null/up.sql new file mode 100644 index 000000000..0badc8e0d --- /dev/null +++ b/hasura/migrations/1736532531529_notification_settings_to_null/up.sql @@ -0,0 +1 @@ +update associations set notification_settings = '{}'; diff --git a/hasura/migrations/1736532558976_alter_table_public_associations_alter_column_notification_settings/down.sql b/hasura/migrations/1736532558976_alter_table_public_associations_alter_column_notification_settings/down.sql new file mode 100644 index 000000000..a57d44af7 --- /dev/null +++ b/hasura/migrations/1736532558976_alter_table_public_associations_alter_column_notification_settings/down.sql @@ -0,0 +1 @@ +alter table "public"."associations" alter column "notification_settings" drop not null; diff --git a/hasura/migrations/1736532558976_alter_table_public_associations_alter_column_notification_settings/up.sql b/hasura/migrations/1736532558976_alter_table_public_associations_alter_column_notification_settings/up.sql new file mode 100644 index 000000000..ff8b9e509 --- /dev/null +++ b/hasura/migrations/1736532558976_alter_table_public_associations_alter_column_notification_settings/up.sql @@ -0,0 +1 @@ +alter table "public"."associations" alter column "notification_settings" set not null; diff --git a/hasura/migrations/1736532890968_notifications_json_empty/down.sql b/hasura/migrations/1736532890968_notifications_json_empty/down.sql new file mode 100644 index 000000000..8996c9f3e --- /dev/null +++ b/hasura/migrations/1736532890968_notifications_json_empty/down.sql @@ -0,0 +1,5 @@ +-- Could not auto-generate a down migration. +-- Please write an appropriate down migration for the SQL below: +-- update notifications set meta = '{}'; +-- update notifications set fcm_data = '{}'; +-- update notifications set ui_translation_meta = '{}'; diff --git a/hasura/migrations/1736532890968_notifications_json_empty/up.sql b/hasura/migrations/1736532890968_notifications_json_empty/up.sql new file mode 100644 index 000000000..3300e7e9e --- /dev/null +++ b/hasura/migrations/1736532890968_notifications_json_empty/up.sql @@ -0,0 +1,3 @@ +update notifications set meta = '{}'; +update notifications set fcm_data = '{}'; +update notifications set ui_translation_meta = '{}'; diff --git a/hasura/migrations/1736532914109_alter_table_public_notifications_alter_column_meta/down.sql b/hasura/migrations/1736532914109_alter_table_public_notifications_alter_column_meta/down.sql new file mode 100644 index 000000000..86844a82c --- /dev/null +++ b/hasura/migrations/1736532914109_alter_table_public_notifications_alter_column_meta/down.sql @@ -0,0 +1,2 @@ +alter table "public"."notifications" alter column "meta" drop not null; +ALTER TABLE "public"."notifications" ALTER COLUMN "meta" drop default; diff --git a/hasura/migrations/1736532914109_alter_table_public_notifications_alter_column_meta/up.sql b/hasura/migrations/1736532914109_alter_table_public_notifications_alter_column_meta/up.sql new file mode 100644 index 000000000..05e6a5b23 --- /dev/null +++ b/hasura/migrations/1736532914109_alter_table_public_notifications_alter_column_meta/up.sql @@ -0,0 +1,2 @@ +alter table "public"."notifications" alter column "meta" set default jsonb_build_object(); +alter table "public"."notifications" alter column "meta" set not null; diff --git a/hasura/migrations/1736532933220_alter_table_public_notifications_alter_column_fcm_data/down.sql b/hasura/migrations/1736532933220_alter_table_public_notifications_alter_column_fcm_data/down.sql new file mode 100644 index 000000000..f33b7161e --- /dev/null +++ b/hasura/migrations/1736532933220_alter_table_public_notifications_alter_column_fcm_data/down.sql @@ -0,0 +1,2 @@ +alter table "public"."notifications" alter column "fcm_data" drop not null; +ALTER TABLE "public"."notifications" ALTER COLUMN "fcm_data" drop default; diff --git a/hasura/migrations/1736532933220_alter_table_public_notifications_alter_column_fcm_data/up.sql b/hasura/migrations/1736532933220_alter_table_public_notifications_alter_column_fcm_data/up.sql new file mode 100644 index 000000000..5c0d753d9 --- /dev/null +++ b/hasura/migrations/1736532933220_alter_table_public_notifications_alter_column_fcm_data/up.sql @@ -0,0 +1,2 @@ +alter table "public"."notifications" alter column "fcm_data" set default jsonb_build_object(); +alter table "public"."notifications" alter column "fcm_data" set not null; diff --git a/hasura/migrations/1736532952372_alter_table_public_notifications_alter_column_ui_translation_meta/down.sql b/hasura/migrations/1736532952372_alter_table_public_notifications_alter_column_ui_translation_meta/down.sql new file mode 100644 index 000000000..d4a7ca3ef --- /dev/null +++ b/hasura/migrations/1736532952372_alter_table_public_notifications_alter_column_ui_translation_meta/down.sql @@ -0,0 +1,2 @@ +alter table "public"."notifications" alter column "ui_translation_meta" drop not null; +ALTER TABLE "public"."notifications" ALTER COLUMN "ui_translation_meta" drop default; diff --git a/hasura/migrations/1736532952372_alter_table_public_notifications_alter_column_ui_translation_meta/up.sql b/hasura/migrations/1736532952372_alter_table_public_notifications_alter_column_ui_translation_meta/up.sql new file mode 100644 index 000000000..221860724 --- /dev/null +++ b/hasura/migrations/1736532952372_alter_table_public_notifications_alter_column_ui_translation_meta/up.sql @@ -0,0 +1,2 @@ +alter table "public"."notifications" alter column "ui_translation_meta" set default jsonb_build_object(); +alter table "public"."notifications" alter column "ui_translation_meta" set not null; diff --git a/hasura/migrations/1736781880394_alter_table_public_notifications_alter_column_job_id/down.sql b/hasura/migrations/1736781880394_alter_table_public_notifications_alter_column_job_id/down.sql new file mode 100644 index 000000000..701336411 --- /dev/null +++ b/hasura/migrations/1736781880394_alter_table_public_notifications_alter_column_job_id/down.sql @@ -0,0 +1 @@ +alter table "public"."notifications" rename column "jobid" to "job_id"; diff --git a/hasura/migrations/1736781880394_alter_table_public_notifications_alter_column_job_id/up.sql b/hasura/migrations/1736781880394_alter_table_public_notifications_alter_column_job_id/up.sql new file mode 100644 index 000000000..ee6f4ba02 --- /dev/null +++ b/hasura/migrations/1736781880394_alter_table_public_notifications_alter_column_job_id/up.sql @@ -0,0 +1 @@ +alter table "public"."notifications" rename column "job_id" to "jobid"; diff --git a/hasura/migrations/1736781913160_alter_table_public_notifications_alter_column_association_id/down.sql b/hasura/migrations/1736781913160_alter_table_public_notifications_alter_column_association_id/down.sql new file mode 100644 index 000000000..ddf7bf683 --- /dev/null +++ b/hasura/migrations/1736781913160_alter_table_public_notifications_alter_column_association_id/down.sql @@ -0,0 +1 @@ +alter table "public"."notifications" rename column "associationid" to "association_id"; diff --git a/hasura/migrations/1736781913160_alter_table_public_notifications_alter_column_association_id/up.sql b/hasura/migrations/1736781913160_alter_table_public_notifications_alter_column_association_id/up.sql new file mode 100644 index 000000000..5238af59a --- /dev/null +++ b/hasura/migrations/1736781913160_alter_table_public_notifications_alter_column_association_id/up.sql @@ -0,0 +1 @@ +alter table "public"."notifications" rename column "association_id" to "associationid"; diff --git a/hasura/migrations/1736781949826_alter_table_public_job_watchers_alter_column_job_id/down.sql b/hasura/migrations/1736781949826_alter_table_public_job_watchers_alter_column_job_id/down.sql new file mode 100644 index 000000000..ff9c47c35 --- /dev/null +++ b/hasura/migrations/1736781949826_alter_table_public_job_watchers_alter_column_job_id/down.sql @@ -0,0 +1 @@ +alter table "public"."job_watchers" rename column "jobid" to "job_id"; diff --git a/hasura/migrations/1736781949826_alter_table_public_job_watchers_alter_column_job_id/up.sql b/hasura/migrations/1736781949826_alter_table_public_job_watchers_alter_column_job_id/up.sql new file mode 100644 index 000000000..af25f283a --- /dev/null +++ b/hasura/migrations/1736781949826_alter_table_public_job_watchers_alter_column_job_id/up.sql @@ -0,0 +1 @@ +alter table "public"."job_watchers" rename column "job_id" to "jobid"; diff --git a/server/email/tasksEmails.js b/server/email/tasksEmails.js index 6d787811a..7a05bcb05 100644 --- a/server/email/tasksEmails.js +++ b/server/email/tasksEmails.js @@ -142,27 +142,38 @@ const sendMail = (type, to, subject, html, taskIds, successCallback, requestInst * @returns {Promise<*>} */ const taskAssignedEmail = async (req, res) => { - // We have no event Data, bail + // 1. Check if we have new task data in the event body. if (!req?.body?.event?.data?.new) { return res.status(400).json({ message: "No data in the event body" }); } - const { new: newTask } = req.body.event.data; + const { new: newTask, old: oldTask } = req.body.event.data; - // This is not a new task, but a reassignment. - const dirty = req.body.event.data?.old && req.body.event.data?.old?.assigned_to; + // TODO: THIS IS HERE BECAUSE THE HANDLER NOW DOES 3 FIELDS, WILL NEED TO BE BUILT ON FOR NOTIFICATIONS + if (oldTask && oldTask.assigned_to === newTask.assigned_to) { + return res.status(200).json({ success: true, message: "assigned_to not changed" }); + } - //Query to get the employee assigned currently. + // 3. If we made it here, assigned_to changed (or oldTask is missing), + // so we continue with the rest of the logic. + + // Query to get the task with bodyshop data, assigned employee data, etc. const { tasks_by_pk } = await client.request(queries.QUERY_TASK_BY_ID, { id: newTask.id }); + // Format date/time with the correct timezone const dateLine = moment().tz(tasks_by_pk.bodyshop.timezone).format("M/DD/YYYY @ hh:mm a"); + // This determines if it was re-assigned (old task exists and had an assigned_to). + const dirty = oldTask && oldTask.assigned_to; + sendMail( "assigned", tasks_by_pk.assigned_to_employee.user_email, - `A ${formatPriority(newTask.priority)} priority task has been ${dirty ? "reassigned to" : "created for"} you - ${newTask.title}`, + `A ${formatPriority(newTask.priority)} priority task has been ${ + dirty ? "reassigned" : "created" + } for you - ${newTask.title}`, generateEmailTemplate( generateTemplateArgs( newTask.title, @@ -181,8 +192,8 @@ const taskAssignedEmail = async (req, res) => { tasks_by_pk.bodyshop.convenient_company ); - // We return success regardless because we don't want to block the event trigger. - res.status(200).json({ success: true }); + // Return success so we don't block the event trigger. + return res.status(200).json({ success: true }); }; /** diff --git a/server/notifications/eventHandlers/handeJobChange.js b/server/notifications/eventHandlers/handeJobChange.js deleted file mode 100644 index db38d5e87..000000000 --- a/server/notifications/eventHandlers/handeJobChange.js +++ /dev/null @@ -1,5 +0,0 @@ -const handleJobChange = (req, res) => { - return res.status(200).json({ message: "Job change handled." }); -}; - -module.exports = handleJobChange; diff --git a/server/notifications/eventHandlers/handeJobsChange.js b/server/notifications/eventHandlers/handeJobsChange.js new file mode 100644 index 000000000..9449f23c4 --- /dev/null +++ b/server/notifications/eventHandlers/handeJobsChange.js @@ -0,0 +1,5 @@ +const handleJobsChange = (req, res) => { + return res.status(200).json({ message: "Jobs change handled." }); +}; + +module.exports = handleJobsChange; diff --git a/server/notifications/eventHandlers/handleBillChange.js b/server/notifications/eventHandlers/handleBillChange.js deleted file mode 100644 index fcabafe85..000000000 --- a/server/notifications/eventHandlers/handleBillChange.js +++ /dev/null @@ -1,5 +0,0 @@ -const handleBillChange = (req, res) => { - return res.status(200).json({ message: "Bill change handled." }); -}; - -module.exports = handleBillChange; diff --git a/server/notifications/eventHandlers/handleBillsChange.js b/server/notifications/eventHandlers/handleBillsChange.js new file mode 100644 index 000000000..d444848f2 --- /dev/null +++ b/server/notifications/eventHandlers/handleBillsChange.js @@ -0,0 +1,5 @@ +const handleBillsChange = (req, res) => { + return res.status(200).json({ message: "Bills change handled." }); +}; + +module.exports = handleBillsChange; diff --git a/server/routes/notificationsRoutes.js b/server/routes/notificationsRoutes.js index a459b9a53..64228382b 100644 --- a/server/routes/notificationsRoutes.js +++ b/server/routes/notificationsRoutes.js @@ -2,11 +2,13 @@ const express = require("express"); const validateFirebaseIdTokenMiddleware = require("../middleware/validateFirebaseIdTokenMiddleware"); const { subscribe, unsubscribe, sendNotification } = require("../firebase/firebase-handler"); const eventAuthorizationMiddleware = require("../middleware/eventAuthorizationMIddleware"); -const handleJobChange = require("../notifications/eventHandlers/handeJobChange"); -const handleBillChange = require("../notifications/eventHandlers/handleBillChange"); + const handlePartsOrderChange = require("../notifications/eventHandlers/handlePartsOrderChange"); const handlePartsDispatchChange = require("../notifications/eventHandlers/handlePartsDispatchChange"); const handleTasksChange = require("../notifications/eventHandlers/handleTasksChange"); +const handleTimeTicketsChange = require("../notifications/eventHandlers/handleTimeTicketsChange"); +const handleJobsChange = require("../notifications/eventHandlers/handeJobsChange"); +const handleBillsChange = require("../notifications/eventHandlers/handleBillsChange"); const router = express.Router(); @@ -16,11 +18,11 @@ router.post("/unsubscribe", validateFirebaseIdTokenMiddleware, unsubscribe); router.post("/sendtestnotification", validateFirebaseIdTokenMiddleware, sendNotification); // Hasura Entry points for creating notifications -router.post("/events/handleJobChange", eventAuthorizationMiddleware, handleJobChange); -router.post("/events/handleBillChange", eventAuthorizationMiddleware, handleBillChange); +router.post("/events/handleJobsChange", eventAuthorizationMiddleware, handleJobsChange); +router.post("/events/handleBillsChange", eventAuthorizationMiddleware, handleBillsChange); router.post("/events/handlePartsOrderChange", eventAuthorizationMiddleware, handlePartsOrderChange); router.post("/events/handlePartsDispatchChange", eventAuthorizationMiddleware, handlePartsDispatchChange); router.post("/events/handleTasksChange", eventAuthorizationMiddleware, handleTasksChange); -router.post("/events/handleTimeTicketsChange", eventAuthorizationMiddleware, handleTasksChange); +router.post("/events/handleTimeTicketsChange", eventAuthorizationMiddleware, handleTimeTicketsChange); module.exports = router;