Skip to content

Commit

Permalink
Revert back to reminder state inference model
Browse files Browse the repository at this point in the history
(1) Notified - a `notification_id` exists
(2) Scheduled - a `job_id` exists

Reverts fdf09b9
  • Loading branch information
akabiru committed Nov 25, 2024
1 parent 89bd340 commit b9a5fd0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 15 deletions.
3 changes: 2 additions & 1 deletion app/models/reminder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ class Reminder < ApplicationRecord
belongs_to :creator, class_name: "User"
belongs_to :notification, optional: true

enum :status, { pending: 0, scheduled: 1, notified: 2, done: 9 }
def notified? = notification_id.present?
def scheduled? = job_id.present?
end
2 changes: 1 addition & 1 deletion app/services/reminders/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CreateService < ::BaseServices::Create
def after_perform(service_call)
reminder = service_call.result
job = Reminders::ScheduleReminderJob.schedule(reminder)
reminder.update_columns(status: :scheduled, job_id: job.job_id)
reminder.update_columns(job_id: job.job_id)

service_call
end
Expand Down
2 changes: 1 addition & 1 deletion app/workers/reminders/schedule_reminder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def perform(reminder)
create_notification_service = create_notification_from_reminder(reminder)

create_notification_service.on_success do |service_result|
reminder.update_columns(status: :notified, notification_id: service_result.result.id)
reminder.update_columns(notification_id: service_result.result.id)
end

create_notification_service.on_failure do |service_result|
Expand Down
1 change: 0 additions & 1 deletion db/migrate/20241119131205_create_reminders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def change
t.references :creator, null: false, foreign_key: { to_table: :users }
t.references :notification, foreign_key: true
t.datetime :remind_at, null: false
t.integer :status, default: 0, null: false
t.string :job_id
t.string :note

Expand Down
1 change: 0 additions & 1 deletion spec/factories/reminders_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

trait :notified do
job_id { SecureRandom.uuid }
status { :notified }
notification factory: :notification
end
end
Expand Down
34 changes: 27 additions & 7 deletions spec/models/reminder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,35 @@
it { is_expected.to belong_to(:notification).optional }
end

describe "Enums" do
it do
expect(subject).to define_enum_for(:status)
.with_values({ pending: 0, scheduled: 1, notified: 2, done: 9 })
.backed_by_column_of_type(:integer)
describe "DB Indexes" do
it { is_expected.to have_db_index(:notification_id).unique(true) }
end

describe "#notified?" do
it "returns true if notification_id is present" do
reminder = build_stubbed(:reminder, :notified)

expect(reminder).to be_notified
end

it "returns false if notification_id is not present" do
reminder = build(:reminder, notification_id: nil)

expect(reminder).not_to be_notified
end
end

describe "DB Indexes" do
it { is_expected.to have_db_index(:notification_id).unique(true) }
describe "#scheduled?" do
it "returns true if job_id is present" do
reminder = build_stubbed(:reminder, :scheduled)

expect(reminder).to be_scheduled
end

it "returns false if job_id is not present" do
reminder = build(:reminder, job_id: nil)

expect(reminder).not_to be_scheduled
end
end
end
2 changes: 1 addition & 1 deletion spec/services/reminders/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
subject

expect(Reminders::ScheduleReminderJob).to have_received(:schedule).with(model_instance)
expect(model_instance).to have_received(:update_columns).with(status: :scheduled, job_id: 1)
expect(model_instance).to have_received(:update_columns).with(job_id: 1)
end
end
end
4 changes: 2 additions & 2 deletions spec/workers/reminders/schedule_reminder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

it "creates a notification from the reminder" do
notification_svc = nil
expect { notification_svc = subject }.to change(Notification, :count).by(1) & change(reminder, :status).to("notified")
expect { notification_svc = subject }.to change(Notification, :count).by(1)

notification = notification_svc.result

Expand All @@ -61,7 +61,7 @@

aggregate_failures "marks the reminder as notified" do
expect(reminder.reload).to be_notified
expect(reminder.notification_id).to eq(notification.id)
expect(reminder.notification).to eq(notification)
end
end

Expand Down

0 comments on commit b9a5fd0

Please sign in to comment.