From dd2a4a4047f15031dc7b745e5d93e2654ced5a32 Mon Sep 17 00:00:00 2001 From: Giga Chubinidze Date: Thu, 2 Nov 2023 07:07:35 +0400 Subject: [PATCH] Implement Task Due Date Reminder notification [SCI-9562] --- app/controllers/my_modules_controller.rb | 4 ++++ app/jobs/my_modules/due_date_reminder_job.rb | 12 +++++++++++ app/models/my_module.rb | 4 ++++ .../task_due_date_notification.rb | 20 +++++++++++++++++++ config/initializers/scheduler.rb | 4 ++++ ...add_task_due_date_reminder_notification.rb | 5 +++++ 6 files changed, 49 insertions(+) create mode 100644 app/jobs/my_modules/due_date_reminder_job.rb create mode 100644 app/notifications/task_due_date_notification.rb create mode 100644 db/migrate/20231029231440_add_task_due_date_reminder_notification.rb diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index 7182efbd2..4f2bb363a 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -187,6 +187,10 @@ class MyModulesController < ApplicationController log_activity(:rename_task) if name_changed log_start_date_change_activity(start_date_changes) if start_date_changes.present? log_due_date_change_activity(due_date_changes) if due_date_changes.present? + + if due_date_changes + @my_module.update(notification_sent: false) + end end end if saved diff --git a/app/jobs/my_modules/due_date_reminder_job.rb b/app/jobs/my_modules/due_date_reminder_job.rb new file mode 100644 index 000000000..63cf94880 --- /dev/null +++ b/app/jobs/my_modules/due_date_reminder_job.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class DueDateReminderJob < ApplicationJob + def perform + my_modules = MyModule.approaching_due_dates + + my_modules.each do |task| + TaskDueDateNotification.send_notifications({ my_module_id: task.id }) + task.update(notification_sent: true) + end + end +end diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 95537bfff..1826a2c4f 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -139,6 +139,10 @@ class MyModule < ApplicationRecord joins(experiment: :project).where(experiment: { projects: { team: teams } }) end + def self.approaching_due_dates + where(notification_sent: false).select { |task| task.is_one_day_prior? } + end + def parent experiment end diff --git a/app/notifications/task_due_date_notification.rb b/app/notifications/task_due_date_notification.rb new file mode 100644 index 000000000..0084d0fe2 --- /dev/null +++ b/app/notifications/task_due_date_notification.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class TaskDueDateNotification < BaseNotification + def message + I18n.t( + 'notifications.notification.my_module_due_date_reminder_html', + my_module_name: subject.name + ) + end + + def self.subtype + :my_module_due_date_reminder + end + + def title; end + + def subject + MyModule.find(params[:my_module_id]) + end +end diff --git a/config/initializers/scheduler.rb b/config/initializers/scheduler.rb index 52c0f946d..49510c45c 100644 --- a/config/initializers/scheduler.rb +++ b/config/initializers/scheduler.rb @@ -21,3 +21,7 @@ if ENV['ENABLE_FLUICS_SYNC'] == 'true' LabelPrinters::Fluics::SyncService.new.sync_templates! if LabelPrinter.fluics.any? end end + +scheduler.every '1h' do + DueDateReminderJob.perform_now +end diff --git a/db/migrate/20231029231440_add_task_due_date_reminder_notification.rb b/db/migrate/20231029231440_add_task_due_date_reminder_notification.rb new file mode 100644 index 000000000..c3a5bf1f1 --- /dev/null +++ b/db/migrate/20231029231440_add_task_due_date_reminder_notification.rb @@ -0,0 +1,5 @@ +class AddTaskDueDateReminderNotification < ActiveRecord::Migration[7.0] + def change + add_column :my_modules, :notification_sent, :boolean, default: false + end +end