Skip to content

Commit 87bb1c5

Browse files
Luke-Oldenburggaryhtougithub-actions[bot]
authored
Auto-scheduled monthly announcement (#10975)
> [!NOTE] > These announcements do not show up on the announcements overview until they are published or updated. --------- Co-authored-by: Gary Tou <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 8450a3e commit 87bb1c5

25 files changed

+268
-16
lines changed

app/controllers/events_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,8 @@ def event_params
10231023
:id,
10241024
:anonymous_donations,
10251025
:cover_donation_fees,
1026-
:contact_email
1026+
:contact_email,
1027+
:generate_monthly_announcement
10271028
]
10281029
)
10291030

@@ -1071,7 +1072,8 @@ def user_event_params
10711072
:id,
10721073
:anonymous_donations,
10731074
:cover_donation_fees,
1074-
:contact_email
1075+
:contact_email,
1076+
:generate_monthly_announcement
10751077
]
10761078
)
10771079

app/jobs/announcement/monthly_job.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
class Announcement
4+
class MonthlyJob < ApplicationJob
5+
queue_as :default
6+
7+
def perform
8+
Announcement.monthly_for(Date.today.prev_month).find_each do |announcement|
9+
Rails.error.handle do
10+
announcement.publish!
11+
end
12+
end
13+
14+
Event.includes(:config).where(config: { generate_monthly_announcement: true }).find_each do |event|
15+
Announcement::Templates::Monthly.new(event:, author: User.system_user).create
16+
end
17+
end
18+
19+
end
20+
21+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class Announcement
4+
class SevenDayWarningJob < ApplicationJob
5+
queue_as :low
6+
7+
def perform
8+
Announcement.monthly_for(Date.today).where.not(aasm_state: :published).find_each do |announcement|
9+
AnnouncementMailer.with(announcement:).seven_day_warning.deliver_now
10+
end
11+
end
12+
13+
end
14+
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class Announcement
4+
class TwoDayWarningJob < ApplicationJob
5+
queue_as :low
6+
7+
def perform
8+
Announcement.monthly_for(Date.today).where(aasm_state: :template_draft).find_each do |announcement|
9+
AnnouncementMailer.with(announcement:).two_day_warning.deliver_now
10+
end
11+
end
12+
13+
end
14+
15+
end

app/mailers/announcement_mailer.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
# frozen_string_literal: true
22

33
class AnnouncementMailer < ApplicationMailer
4+
before_action :set_warning_variables, only: [:seven_day_warning, :two_day_warning]
5+
46
def announcement_published
57
@announcement = params[:announcement]
68
@event = @announcement.event
79

810
mail to: params[:email], subject: "#{@announcement.title} | #{@event.name}", from: hcb_email_with_name_of(@event)
911
end
1012

13+
def seven_day_warning
14+
mail to: @emails, subject: "[#{@event.name}] Your scheduled monthly announcement will be delivered on #{@scheduled_for.strftime("%b %-m")}"
15+
end
16+
17+
def two_day_warning
18+
mail to: @emails, subject: "[#{@event.name}] Your scheduled monthly announcement will be delivered on #{@scheduled_for.strftime("%b %-m")}"
19+
end
20+
21+
def set_warning_variables
22+
@announcement = params[:announcement]
23+
@event = @announcement.event
24+
25+
@emails = @event.managers.map(&:email_address_with_name)
26+
@emails << @event.config.contact_email if @event.config.contact_email.present?
27+
28+
@scheduled_for = Date.today.next_month.beginning_of_month
29+
end
30+
1131
end

app/models/announcement.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# published_at :datetime
1212
# rendered_email_html :text
1313
# rendered_html :text
14+
# template_type :string
1415
# title :string not null
1516
# created_at :datetime not null
1617
# updated_at :datetime not null
@@ -55,6 +56,9 @@ class Announcement < ApplicationRecord
5556
end
5657
end
5758

59+
scope :saved, -> { where.not(aasm_state: :template_draft) }
60+
scope :monthly, -> { where(template_type: Announcement::Templates::Monthly.name) }
61+
scope :monthly_for, ->(date) { monthly.where("announcements.created_at BETWEEN ? AND ?", date.beginning_of_month, date.end_of_month) }
5862
validate :content_is_json
5963

6064
scope :saved, -> { where.not(aasm_state: :template_draft).where.not(content: {}) }

app/models/announcement/templates/blank.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def json_content
2424
end
2525

2626
def create
27-
Announcement.create!(event: @event, title:, content: json_content, aasm_state: :template_draft, author: @author)
27+
Announcement.create!(event: @event, title:, content: json_content, aasm_state: :template_draft, author: @author, template_type: self.class.name)
2828
end
2929

3030
end

app/models/announcement/templates/donation_goal_reached.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def json_content
5151
end
5252

5353
def create
54-
Announcement.create!(event: @event, title:, content: json_content, aasm_state: :template_draft, author: @author)
54+
Announcement.create!(event: @event, title:, content: json_content, aasm_state: :template_draft, author: @author, template_type: self.class.name)
5555
end
5656

5757
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
class Announcement
4+
module Templates
5+
class Monthly
6+
include ApplicationHelper
7+
8+
def initialize(event:, author:)
9+
@event = event
10+
@author = author
11+
end
12+
13+
def title
14+
"#{Date.current.strftime("%B %Y")} Update"
15+
end
16+
17+
def json_content(block)
18+
{
19+
type: "doc",
20+
content: [
21+
{ type: "paragraph", content: [{ type: "text", text: "Hey all!" }] },
22+
{
23+
type: "paragraph",
24+
content: [
25+
{
26+
type: "text",
27+
text: "Thank you for your support and generosity! With this funding, we'll be able to better work towards our mission.",
28+
},
29+
],
30+
},
31+
{
32+
type: "paragraph",
33+
content: [
34+
{
35+
type: "text",
36+
text: "We'd like to thank all of the donors from the past month that contributed towards our organization:",
37+
},
38+
],
39+
},
40+
{ type: "donationSummary", attrs: { id: block.id } },
41+
{
42+
type: "paragraph",
43+
content: [
44+
{ type: "text", text: "Best," },
45+
{ type: "hardBreak" },
46+
{ type: "text", text: "The #{@event.name} team" },
47+
],
48+
},
49+
],
50+
}
51+
end
52+
53+
def create
54+
announcement = Announcement.create!(event: @event, title:, content: {}, aasm_state: :template_draft, author: @author, template_type: self.class.name)
55+
block = Announcement::Block::DonationSummary.create!(announcement:, parameters: { start_date: Date.current.beginning_of_month })
56+
announcement.update!(content: json_content(block))
57+
end
58+
59+
end
60+
61+
end
62+
63+
end

app/models/announcement/templates/new_donation_tier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def json_content
7575
end
7676

7777
def create
78-
Announcement.create!(event: @donation_tier.event, title:, content: json_content, aasm_state: :template_draft, author: @author)
78+
Announcement.create!(event: @donation_tier.event, title:, content: json_content, aasm_state: :template_draft, author: @author, template_type: self.class.name)
7979
end
8080

8181
end

0 commit comments

Comments
 (0)