From 69fcb4b053c3ca8b9c2b3e3772a2e7d013bdb377 Mon Sep 17 00:00:00 2001 From: Celia Collins Date: Fri, 6 Dec 2024 11:23:11 +0000 Subject: [PATCH] Remove concept of welcome message --- app/controllers/contents_controller.rb | 2 +- app/jobs/send_welcome_message_job.rb | 32 +++---------- app/models/content.rb | 2 + app/models/group.rb | 4 -- app/models/user.rb | 6 +-- app/views/contents/_form.html.erb | 4 -- app/views/groups/show.html.erb | 3 -- ...738_remove_welcome_message_from_content.rb | 5 +++ db/schema.rb | 7 +-- test/jobs/send_welcome_message_job_test.rb | 45 +------------------ test/models/group_test.rb | 7 --- test/models/user_test.rb | 23 +--------- test/system/contents_test.rb | 19 -------- test/system/users_test.rb | 5 +-- 14 files changed, 25 insertions(+), 139 deletions(-) create mode 100644 db/migrate/20241206110738_remove_welcome_message_from_content.rb diff --git a/app/controllers/contents_controller.rb b/app/controllers/contents_controller.rb index 19a71a2..a6f3192 100644 --- a/app/controllers/contents_controller.rb +++ b/app/controllers/contents_controller.rb @@ -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 diff --git a/app/jobs/send_welcome_message_job.rb b/app/jobs/send_welcome_message_job.rb index 47ec713..a16b2cb 100644 --- a/app/jobs/send_welcome_message_job.rb +++ b/app/jobs/send_welcome_message_job.rb @@ -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 diff --git a/app/models/content.rb b/app/models/content.rb index 78641d8..7d15c6f 100644 --- a/app/models/content.rb +++ b/app/models/content.rb @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index 447cdbf..de73a3d 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 0ed433e..f135e00 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ -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 diff --git a/app/views/contents/_form.html.erb b/app/views/contents/_form.html.erb index ea14088..2635221 100644 --- a/app/views/contents/_form.html.erb +++ b/app/views/contents/_form.html.erb @@ -11,9 +11,5 @@ <%= form.input :link %> -
- <%= form.input :welcome_message, label: "This is the welcome message", input_html: { class: "form-checkbox" } %> -
- <%= form.submit (action_name == "edit" ? "Update" : "Create"), class: "btn btn-primary mt-5 bg-purple-600 text-white w-full" %> <% end %> diff --git a/app/views/groups/show.html.erb b/app/views/groups/show.html.erb index 52dedd5..84b5b38 100644 --- a/app/views/groups/show.html.erb +++ b/app/views/groups/show.html.erb @@ -25,9 +25,6 @@ - <% if content.welcome_message? %> - Welcome message
- <% end %> <%= content.body %> diff --git a/db/migrate/20241206110738_remove_welcome_message_from_content.rb b/db/migrate/20241206110738_remove_welcome_message_from_content.rb new file mode 100644 index 0000000..c36615b --- /dev/null +++ b/db/migrate/20241206110738_remove_welcome_message_from_content.rb @@ -0,0 +1,5 @@ +class RemoveWelcomeMessageFromContent < ActiveRecord::Migration[8.0] + def change + remove_column :contents, :welcome_message, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index be58a11..1575ca1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_12_05_140053) do +ActiveRecord::Schema[8.0].define(version: 2024_12_06_110738) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -96,7 +96,6 @@ t.string "link" t.bigint "group_id" t.integer "position", null: false - t.boolean "welcome_message", default: false t.integer "age_in_months", null: false t.index ["group_id", "position"], name: "index_contents_on_group_id_and_position", unique: true t.index ["group_id"], name: "index_contents_on_group_id" @@ -136,13 +135,14 @@ t.bigint "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "content_id" t.string "message_sid" t.string "status" t.datetime "sent_at" t.string "token", null: false t.string "link" t.datetime "clicked_at" - t.bigint "content_id" + t.index ["content_id"], name: "index_messages_on_content_id" t.index ["token"], name: "index_messages_on_token", unique: true end @@ -174,5 +174,6 @@ add_foreign_key "clicks", "pages" add_foreign_key "interests", "users" + add_foreign_key "messages", "contents" add_foreign_key "users", "contents", column: "last_content_id" end diff --git a/test/jobs/send_welcome_message_job_test.rb b/test/jobs/send_welcome_message_job_test.rb index 9b39983..1e21e2e 100644 --- a/test/jobs/send_welcome_message_job_test.rb +++ b/test/jobs/send_welcome_message_job_test.rb @@ -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 diff --git a/test/models/group_test.rb b/test/models/group_test.rb index 7c40bdb..00c2f25 100644 --- a/test/models/group_test.rb +++ b/test/models/group_test.rb @@ -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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 7eacb5d..45ac687 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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) @@ -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 @@ -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) diff --git a/test/system/contents_test.rb b/test/system/contents_test.rb index 5966db7..e07e751 100644 --- a/test/system/contents_test.rb +++ b/test/system/contents_test.rb @@ -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) diff --git a/test/system/users_test.rb b/test/system/users_test.rb index 29e06b4..85cbce0 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -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" @@ -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