diff --git a/app/jobs/announcement/monthly_job.rb b/app/jobs/announcement/monthly_job.rb index 0afd48b7be..30bb0a2055 100644 --- a/app/jobs/announcement/monthly_job.rb +++ b/app/jobs/announcement/monthly_job.rb @@ -6,7 +6,7 @@ class MonthlyJob < ApplicationJob def perform Event.includes(:config).where(config: { generate_monthly_announcement: true }).find_each do |event| - event.announcements.monthly_for(Date.today.prev_month).find_each do |announcement| + event.announcements.approved_monthly_for(Date.today.prev_month).find_each do |announcement| Rails.error.handle do announcement.mark_published! end diff --git a/app/jobs/announcement/seven_day_warning_job.rb b/app/jobs/announcement/seven_day_warning_job.rb index fb37cc025e..2168ed94c8 100644 --- a/app/jobs/announcement/seven_day_warning_job.rb +++ b/app/jobs/announcement/seven_day_warning_job.rb @@ -6,7 +6,21 @@ class SevenDayWarningJob < ApplicationJob def perform Announcement.monthly_for(Date.today).where.not(aasm_state: :published).find_each do |announcement| - AnnouncementMailer.with(announcement:).seven_day_warning.deliver_later + # If a monthly announcement is still a template draft, then that means it has not been edited. + # So, if it has not been edited and has empty blocks (no data), the managers will be notified + # that the announcement is canceled. At the beginning of the month, these announcements will + # not be published unless edited since template drafts cannot transition to published, and monthly + # announcements that have block with data will be promoted to drafts in this job. + + if announcement.template_draft? && announcement.blocks.any?(&:empty?) + AnnouncementMailer.with(announcement:).canceled.deliver_now + else + if announcement.template_draft? + announcement.mark_draft! + end + + AnnouncementMailer.with(announcement:).seven_day_warning.deliver_later + end end end diff --git a/app/jobs/announcement/two_day_warning_job.rb b/app/jobs/announcement/two_day_warning_job.rb index 650b12bc3a..3b1aa9a662 100644 --- a/app/jobs/announcement/two_day_warning_job.rb +++ b/app/jobs/announcement/two_day_warning_job.rb @@ -5,7 +5,7 @@ class TwoDayWarningJob < ApplicationJob queue_as :low def perform - Announcement.monthly_for(Date.today).where(aasm_state: :template_draft).find_each do |announcement| + Announcement.approved_monthly_for(Date.today).where(aasm_state: :template_draft).find_each do |announcement| AnnouncementMailer.with(announcement:).two_day_warning.deliver_later end end diff --git a/app/mailers/announcement_mailer.rb b/app/mailers/announcement_mailer.rb index 7a365066dd..df9d065dea 100644 --- a/app/mailers/announcement_mailer.rb +++ b/app/mailers/announcement_mailer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class AnnouncementMailer < ApplicationMailer - before_action :set_warning_variables, only: [:seven_day_warning, :two_day_warning] + before_action :set_warning_variables, only: [:seven_day_warning, :two_day_warning, :canceled] def announcement_published @announcement = params[:announcement] @@ -18,6 +18,10 @@ def two_day_warning mail to: @emails, subject: "[#{@event.name}] Your scheduled monthly announcement will be delivered on #{@scheduled_for.strftime("%B #{@scheduled_for.day.ordinalize}")}" end + def canceled + mail to: @emails, subject: "[#{@event.name}] Your scheduled monthly announcement has been canceled" + end + def set_warning_variables @announcement = params[:announcement] @event = @announcement.event diff --git a/app/models/announcement.rb b/app/models/announcement.rb index e88eacc04d..a1e2079827 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -42,7 +42,7 @@ class Announcement < ApplicationRecord state :published event :mark_published do - transitions from: [:template_draft, :draft], to: :published + transitions from: :draft, to: :published after do Announcement::PublishedJob.perform_later(announcement: self) @@ -56,6 +56,7 @@ class Announcement < ApplicationRecord scope :monthly, -> { where(template_type: Announcement::Templates::Monthly.name) } scope :monthly_for, ->(date) { monthly.where("announcements.created_at BETWEEN ? AND ?", date.beginning_of_month, date.end_of_month) } + scope :approved_monthly_for, ->(date) { monthly_for(date).draft } validate :content_is_json scope :saved, -> { where.not(aasm_state: :template_draft).where.not(content: {}).and(where.not(template_type: Announcement::Templates::Monthly.name, published_at: nil).or(where(template_type: nil))) } diff --git a/app/views/announcement_mailer/canceled.html.erb b/app/views/announcement_mailer/canceled.html.erb new file mode 100644 index 0000000000..e335d3e6b5 --- /dev/null +++ b/app/views/announcement_mailer/canceled.html.erb @@ -0,0 +1,20 @@ +
+ Hey<%= " all" if @emails.size > 1 %>, +
+ +<% scheduled_for_string = @scheduled_for.strftime("%B #{@scheduled_for.day.ordinalize}") %> + ++ Your monthly announcement that was scheduled to send on <%= scheduled_for_string %> + has been canceled since blocks contained in this announcement have no data. +
+ ++ If you would like, you can keep this monthly announcement to be sent on <%= scheduled_for_string %> by <%= link_to "editing it on HCB", edit_announcement_url(@announcement) %>. +
+ +
+ Sincerely,
+
+ The HCB Team
+