Skip to content

Commit

Permalink
Remove concept of welcome message
Browse files Browse the repository at this point in the history
  • Loading branch information
cdccollins committed Dec 6, 2024
1 parent 4f9ee95 commit 69fcb4b
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 139 deletions.
2 changes: 1 addition & 1 deletion app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def destroy
private

def content_params
params.require(:content).permit(:body, :link, :position, :welcome_message, :age_in_months)
params.require(:content).permit(:body, :link, :position, :age_in_months)
end
end
32 changes: 5 additions & 27 deletions app/jobs/send_welcome_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,12 @@ class SendWelcomeMessageJob < ApplicationJob
queue_as :default

def perform(user)
content = Content.find_by(age_in_months: user.child_age_in_months_today, welcome_message: true)

message = if content.present?
Message.build do |m|
m.token = m.send(:generate_token)
m.link = content.link
m.user = user
m.body = content.body.gsub("{{link}}", track_link_url(m.token))
end
else
Message.build do |m|
m.token = m.send(:generate_token)
m.user = user
m.body = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! You'll receive your first activity soon."
end
message = Message.build do |m|
m.token = m.send(:generate_token)
m.user = user
m.body = Content::WELCOME_MESSAGE
end

Twilio::Client.new.send_message(message) if save_user_and_message(user, message, content)
end

private

def save_user_and_message(user, message, content)
ActiveRecord::Base.transaction do
user.update(last_content_id: content.id) if content.present?
message.save
rescue ActiveRecord::RecordInvalid
false
end
Twilio::Client.new.send_message(message) if message.save
end
end
2 changes: 2 additions & 0 deletions app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Content < ApplicationRecord
has_many :messages, dependent: :nullify

validates_presence_of :body, :link, :age_in_months

WELCOME_MESSAGE = "Hi [Parent], welcome to our programme of weekly texts with fun activities for [Child]’s development. Congrats on starting this amazing journey with your little one!"
end
4 changes: 0 additions & 4 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ class Group < ApplicationRecord
has_many :contents, dependent: :destroy

validates :name, presence: true

def weekly_content
contents.where(welcome_message: false)
end
end
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def next_content
if had_any_content_before?
find_next_unseen_content
else
Content.where(age_in_months: child_age_in_months_today, welcome_message: false).min_by(&:position)
Content.where(age_in_months: child_age_in_months_today).min_by(&:position)
end
end

Expand All @@ -84,8 +84,8 @@ def find_next_unseen_content
content = Content.find_by(position: i)
# Last message in series
return nil if content.nil?
# Next message that's not a welcome message
return content if not_seen_content?(content) && !content.welcome_message?
# Next message
return content if not_seen_content?(content)
i += 1
end
end
Expand Down
4 changes: 0 additions & 4 deletions app/views/contents/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@
<%= form.input :link %>
</div>

<div class="mt-3">
<%= form.input :welcome_message, label: "This is the welcome message", input_html: { class: "form-checkbox" } %>
</div>

<%= form.submit (action_name == "edit" ? "Update" : "Create"), class: "btn btn-primary mt-5 bg-purple-600 text-white w-full" %>
<% end %>
3 changes: 0 additions & 3 deletions app/views/groups/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
<svg class="cursor-grab" width="20px" height="20px" stroke-width="1.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="#ccc"><path d="M3 5H21" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M3 12H21" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M3 19H21" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>
</td>
<td class="p-4">
<% if content.welcome_message? %>
<span class="text-sm text-gray-500">Welcome message</span><br>
<% end %>
<%= content.body %>
</td>
<td class="p-4">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveWelcomeMessageFromContent < ActiveRecord::Migration[8.0]
def change
remove_column :contents, :welcome_message, :boolean
end
end
7 changes: 4 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 2 additions & 43 deletions test/jobs/send_welcome_message_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,12 @@ class SendWelcomeMessageJobTest < ActiveSupport::TestCase

test "#perform sends message with default content" do
user = create(:user, child_birthday: 18.months.ago)
group = create(:group)
content = create(:content, group:, welcome_message: true, link: "https://example.com", body: "Hi, {{link}}")
create(:content, group:, welcome_message: false, link: "https://example.com")

Message.any_instance.stubs(:generate_token).returns("123")

stub_successful_twilio_call(content.body.gsub("{{link}}", track_link_url("123")), user)

SendWelcomeMessageJob.new.perform(user)

assert_equal 1, Message.count
assert_match(/m\/123/, Message.last.body)
assert_equal "https://example.com", Message.last.link
assert_equal user.reload.last_content_id, content.id
end

test "#perform sends message with no link if there isn't appropriate content" do
user = create(:user, child_birthday: 25.months.ago)

Message.any_instance.stubs(:generate_token).returns("123")

message = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! You'll receive your first activity soon."

stub_successful_twilio_call(message, user)

SendWelcomeMessageJob.new.perform(user)

assert_equal 1, Message.count
assert_nil Message.last.link
assert_nil user.reload.last_content_id
end

test "#perform sends message with no link if there isn't a welcome message" do
user = create(:user, child_birthday: 18.months.ago)
group = create(:group)
create(:content, group:, welcome_message: false, link: "https://example.com")

Message.any_instance.stubs(:generate_token).returns("123")

message = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! You'll receive your first activity soon."

stub_successful_twilio_call(message, user)
stub_successful_twilio_call(Content::WELCOME_MESSAGE, user)

SendWelcomeMessageJob.new.perform(user)

assert_equal 1, Message.count
assert_nil Message.last.link
assert_nil user.reload.last_content_id
assert_match(Content::WELCOME_MESSAGE, Message.last.body)
end
end
7 changes: 0 additions & 7 deletions test/models/group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ def setup
end

test("name required") { assert_present(:name) }

test "#weekly_content" do
weekly_content = create(:content, group: @subject)
create(:content, group: @subject, welcome_message: true)

assert_equal [weekly_content], @subject.weekly_content
end
end
23 changes: 1 addition & 22 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class UserTest < ActiveSupport::TestCase

test "#next_content method returns next ranked content for age group" do
group = create(:group)
create(:content, group:, welcome_message: true)
content1 = create(:content, group:, position: 1)
content2 = create(:content, group:, position: 2)
create(:content, group:, position: 3)
Expand All @@ -149,17 +148,7 @@ class UserTest < ActiveSupport::TestCase
group = create(:group)
content = create(:content, group:, position: 1, age_in_months: 18)
create(:content, group:, position: 2, age_in_months: 18)
create(:content, group:, position: 3, age_in_months: 18)

assert_equal @subject.next_content, content
end

test "#next_content does not return the welcome message if user has not had content before" do
group = create(:group)
create(:content, group:, position: 1, welcome_message: true)
content = create(:content, group:, position: 2, age_in_months: 18)
create(:content, group:, position: 3, age_in_months: 18)
create(:content, group:, position: 4, age_in_months: 18)
create(:content, group:, position: 3, age_in_months: 19)

assert_equal @subject.next_content, content
end
Expand All @@ -176,16 +165,6 @@ class UserTest < ActiveSupport::TestCase
assert_equal @subject.next_content, content3
end

test "#next_content does not return welcome messages" do
group = create(:group)
content1 = create(:content, group:, position: 1)
create(:content, group:, position: 2, welcome_message: true)
content3 = create(:content, group:, position: 3)
@subject.update(last_content_id: content1.id)

assert_equal @subject.next_content, content3
end

test "#had_content_this_week? method returns true if user has had content" do
user = create(:user)
content = create(:content)
Expand Down
19 changes: 0 additions & 19 deletions test/system/contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,6 @@ class ContentsTest < ApplicationSystemTestCase
assert_text "New content"
end

test "creating new content with welcome message" do
sign_in
visit group_path(@group)

assert_text @group.name

click_on "Add new message"

fill_in "Body", with: "New content"
fill_in "Link", with: "www.example.com"
fill_in "Age in months", with: "18"
check "This is the welcome message"
click_on "Create"

within("tr", text: "New content") do
assert_text "Welcome message"
end
end

test "shows errors" do
create(:content, body: "Old Content", group: @group)

Expand Down
5 changes: 2 additions & 3 deletions test/system/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class UsersTest < ApplicationSystemTestCase
check "Would you like to be added to a Slack channel with other parents to discuss the programme?"
check "I accept the terms of service and privacy policy"

message = "Welcome to Tiny Happy People, a programme of weekly texts with fun activities! You'll receive your first activity soon."
stub_successful_twilio_call(message, User.new(phone_number: "+447444930200"))
stub_successful_twilio_call(Content::WELCOME_MESSAGE, User.new(phone_number: "+447444930200"))

within("#sign-up-form") do
click_on "Sign up"
Expand All @@ -35,7 +34,7 @@ class UsersTest < ApplicationSystemTestCase
assert_text "ABC123"
assert_text "Does have family support"
assert_text "On Slack"
assert_text "Welcome to Tiny Happy People, a programme of weekly texts with fun activities!"
assert_text Content::WELCOME_MESSAGE
end

test "form shows errors" do
Expand Down

0 comments on commit 69fcb4b

Please sign in to comment.