Skip to content

Commit

Permalink
unsubscribe soft deletes reminder
Browse files Browse the repository at this point in the history
  • Loading branch information
asmega committed Dec 6, 2024
1 parent ef0050b commit 93b8087
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/unsubscribe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create
if @form.reminder.nil?
head :bad_request
else
@form.reminder.destroy
@form.reminder.soft_delete!
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/forms/unsubscribe/confirmation_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ConfirmationForm
attribute :id, :string

def reminder
@reminder ||= Reminder.find_by(id:)
@reminder ||= Reminder.not_deleted.find_by(id:)
end

def obfuscasted_email
Expand Down
5 changes: 5 additions & 0 deletions app/models/reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Reminder < ApplicationRecord
scope :by_journey, ->(journey) { where(journey_class: journey.to_s) }
scope :inside_academic_year, -> { where(itt_academic_year: AcademicYear.current.to_s) }
scope :to_be_sent, -> { email_verified.not_yet_sent.inside_academic_year }
scope :not_deleted, -> { where(deleted_at: nil) }

def journey
journey_class.constantize
Expand All @@ -21,4 +22,8 @@ def itt_academic_year
read_attribute(:itt_academic_year)
)
end

def soft_delete!
update!(deleted_at: Time.now)
end
end
5 changes: 5 additions & 0 deletions db/migrate/20241206105631_add_deleted_at_to_reminders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDeletedAtToReminders < ActiveRecord::Migration[7.2]
def change
add_column :reminders, :deleted_at, :datetime
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_11_26_105650) do
ActiveRecord::Schema[7.2].define(version: 2024_12_06_105631) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -428,6 +428,7 @@
t.string "itt_academic_year", limit: 9
t.string "itt_subject"
t.text "journey_class", null: false
t.datetime "deleted_at"
t.index ["journey_class"], name: "index_reminders_on_journey_class"
end

Expand Down
4 changes: 4 additions & 0 deletions spec/factories/reminders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
trait :with_fe_reminder do
journey_class { Journeys::FurtherEducationPayments.to_s }
end

trait :soft_deleted do
deleted_at { 1.second.ago }
end
end
end
21 changes: 18 additions & 3 deletions spec/features/unsubscribe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
visit "/#{reminder.journey::ROUTING_NAME}/unsubscribe/reminders/#{reminder.id}"
expect(page).to have_content "Are you sure you wish to unsub"

expect {
click_button("Unsubscribe")
}.to change(Reminder, :count).by(-1)
click_button("Unsubscribe")

expect(reminder.reload.deleted_at).to be_present

expect(page).to have_content "Unsubscribe complete"
end
Expand All @@ -23,4 +23,19 @@
visit "/#{reminder.journey::ROUTING_NAME}/unsubscribe/reminders/idonotexist"
expect(page).to have_content "We can’t find your subscription"
end

context "when reminder already soft deleted" do
let(:reminder) do
create(
:reminder,
:soft_deleted,
journey_class: Journeys::FurtherEducationPayments.to_s
)
end

scenario "cannot find subscription" do
visit "/#{reminder.journey::ROUTING_NAME}/unsubscribe/reminders/#{reminder.id}"
expect(page).to have_content "We can’t find your subscription"
end
end
end
20 changes: 15 additions & 5 deletions spec/requests/unsubscribe_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "rails_helper"

RSpec.describe "unsubscribe", type: :request do
let!(:reminder) { create(:reminder, :with_fe_reminder) }
let(:reminder) { create(:reminder, :with_fe_reminder) }

describe "POST #create" do
context "happy path" do
it "unsubscribes from reminder via one click unsubscribe" do
expect {
post "/further-education-payments/unsubscribe/reminders", params: {id: reminder.id}
}.to change(Reminder, :count).by(-1)
it "sets deleted_at from reminder via one click unsubscribe" do
post "/further-education-payments/unsubscribe/reminders", params: {id: reminder.id}

expect(reminder.reload.deleted_at).to be_present

expect(response).to be_successful
end
Expand All @@ -21,5 +21,15 @@
expect(response).to be_bad_request
end
end

context "when already soft deleted" do
let(:reminder) { create(:reminder, :with_fe_reminder, :soft_deleted) }

it "returns 400 error" do
post "/further-education-payments/unsubscribe/reminders", params: {id: reminder.id}

expect(response).to be_bad_request
end
end
end
end

0 comments on commit 93b8087

Please sign in to comment.