Skip to content
2 changes: 1 addition & 1 deletion app/jobs/announcement/monthly_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion app/jobs/announcement/seven_day_warning_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/announcement/two_day_warning_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion app/mailers/announcement_mailer.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))) }
Expand Down
20 changes: 20 additions & 0 deletions app/views/announcement_mailer/canceled.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<p>
Hey<%= " all" if @emails.size > 1 %>,
</p>

<% scheduled_for_string = @scheduled_for.strftime("%B #{@scheduled_for.day.ordinalize}") %>

<p>
Your monthly announcement that was scheduled to send on <%= scheduled_for_string %>
has been canceled since blocks contained in this announcement have no data.
</p>

<p>
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) %>.
</p>

<p>
Sincerely,
<br><br>
The HCB Team
</p>
23 changes: 17 additions & 6 deletions app/views/events/announcement_overview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@
<% if @monthly_announcement.present? && organizer_signed_in?(as: :reader) %>
<% scheduled_for = Date.today.next_month.beginning_of_month %>
<% scheduled_for_string = scheduled_for.strftime("%B #{scheduled_for.day.ordinalize}") %>
<%= render "callout", type: "warning", title: "You have a scheduled monthly announcement that will be delivered on #{scheduled_for_string}", footer: :questions do %>
<ul>
<li>You can <%= link_to "view and edit this announcement", announcement_path(@monthly_announcement) %> any time before <%= scheduled_for_string %>.</li>
<li>If you would like to disable sending monthly announcements, you may do so in your <%= link_to "organization settings", edit_event_path(@event) %>.</li>
<li>This announcement will be sent out to everyone following you on <%= scheduled_for_string %>.</li>
</ul>

<% if @monthly_announcement.template_draft? && (DateTime.now > DateTime.now.end_of_month - 1.week) %>
<%= render "callout", type: "error", title: "Your scheduled monthly announcement is canceled", footer: :questions do %>
<ul>
<li>Since blocks contained in this monthly announcement have no data, the scheduled monthly announcement for <%= Date.today.strftime("%B") %> has been canceled.</li>
<li>If you would like to keep this monthly announcement, <%= link_to "edit it", announcement_path(@monthly_announcement) %> any time before <%= scheduled_for_string %>.</li>
<li>You can also disable sending monthly announcements in your <%= link_to "organization settings", edit_event_path(@event) %>.</li>
</ul>
<% end %>
<% elsif !@monthly_announcement.published? %>
<%= render "callout", type: "warning", title: "You have a scheduled monthly announcement that will be delivered on #{scheduled_for_string}", footer: :questions do %>
<ul>
<li>You can <%= link_to "view and edit this announcement", announcement_path(@monthly_announcement) %> any time before <%= scheduled_for_string %>.</li>
<li>If you would like to disable sending monthly announcements, you may do so in your <%= link_to "organization settings", edit_event_path(@event) %>.</li>
<li>This announcement will be sent out to everyone following you on <%= scheduled_for_string %>.</li>
</ul>
<% end %>
<% end %>
<% end %>

Expand Down
4 changes: 4 additions & 0 deletions spec/mailers/previews/announcement_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def two_day_warning
AnnouncementMailer.with(announcement: Announcement.monthly.last).two_day_warning
end

def canceled
AnnouncementMailer.with(announcement: Announcement.monthly.last).canceled
end

end