Skip to content

Commit

Permalink
Resolves #4857 (#4872)
Browse files Browse the repository at this point in the history
* Calling reminder_email function from distribution_controller's create method

* Only schedule reminder email if box is checked

* Added specs for distribution reminder emails

* Lint fixes

* Updating existing distribution reminder email spec

---------

Co-authored-by: Gabe <[email protected]>
  • Loading branch information
gabeparra01 and Gabe authored Dec 23, 2024
1 parent 113ecae commit fc1e7b4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def create
@distribution = result.distribution

perform_inventory_check
schedule_reminder_email(result.distribution) if @distribution.reminder_email_enabled

respond_to do |format|
format.turbo_stream do
Expand Down Expand Up @@ -196,7 +197,7 @@ def update
if result.resend_notification? && @distribution.partner&.send_reminders
send_notification(current_organization.id, @distribution.id, subject: "Your Distribution Has Changed", distribution_changes: result.distribution_content.changes)
end
schedule_reminder_email(@distribution)
schedule_reminder_email(@distribution) if @distribution.reminder_email_enabled

perform_inventory_check
redirect_to @distribution, notice: "Distribution updated!"
Expand Down
97 changes: 97 additions & 0 deletions spec/controllers/distributions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
RSpec.describe DistributionsController, type: :controller do
include ActiveJob::TestHelper

let(:organization) { create(:organization) }
let(:user) { create(:user, organization: organization) }
let(:partner) { create(:partner, organization: organization) }

after(:each) do
clear_enqueued_jobs
end

context "While signed in" do
before do
sign_in(user)
Expand Down Expand Up @@ -220,6 +226,50 @@
expect(flash[:error]).to include("Sorry, we weren't able to save the distribution. \n Validation failed: Inventory Item 1's quantity needs to be at least 1")
end
end

context "when distribution reminder email is enabled" do
let(:params) do
{
organization_name: organization.id,
distribution: {
partner_id: partner.id,
issued_at: Date.tomorrow,
storage_location_id: first_storage_location.id,
line_items_attributes:
{
"0": { item_id: first_storage_location.items.first.id, quantity: 10 }
},
reminder_email_enabled: true
}
}
end
subject { post :create, params: params.merge(format: :turbo_stream) }

context "when partner has enabled send_reminders" do
before(:each) do
partner.send_reminders = true
end
it "should schedule the reminder email" do
subject
expect(enqueued_jobs[2]["arguments"][1]).to eq("reminder_email")
end

it "should not schedule a reminder for a date in the past" do
params[:distribution][:issued_at] = Date.yesterday
subject
expect(enqueued_jobs.size).to eq(2)
end
end

context "when partner has disabled send_reminders" do
let(:partner) { create(:partner, organization: organization, send_reminders: false) }

it "should not schedule an email reminder for a partner that disabled reminders" do
subject
expect(enqueued_jobs.size).to eq(1)
end
end
end
end

describe "PUT #update" do
Expand Down Expand Up @@ -353,6 +403,53 @@
expect(flash[:alert]).to be_nil
end
end

context "when distribution reminder email is enabled" do
let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 0) }
let(:storage_location) { create(:storage_location, organization: organization) }
let(:distribution) { create(:distribution, :with_items, item: item1, storage_location: storage_location, organization: organization, reminder_email_enabled: false, partner: partner) }
let(:params) do
{
organization_name: organization.id,
id: distribution.id,
distribution: {
storage_location_id: distribution.storage_location.id,
issued_at: Date.tomorrow,
line_items_attributes:
{
"0": { item_id: item1.id, quantity: 1 }
},
reminder_email_enabled: true
}
}
end
subject { put :update, params: params }

context "when partner has enabled send_reminders" do
before(:each) do
partner.send_reminders = true
end
it "should schedule the reminder email" do
subject
expect(enqueued_jobs[1]["arguments"][1]).to eq("reminder_email")
end

it "should not schedule a reminder for a date in the past" do
params[:distribution][:issued_at] = Date.yesterday
subject
expect(enqueued_jobs.size).to eq(1)
end
end

context "when partner has disabled send_reminders" do
let(:partner) { create(:partner, organization: organization, send_reminders: false) }

it "should not schedule an email reminder for a partner that disabled reminders" do
subject
expect(enqueued_jobs.size).to eq(1)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/system/distribution_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
end

context "With an existing distribution" do
let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", delivery_method: delivery_method, organization: user.organization) }
let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", delivery_method: delivery_method, organization: user.organization, reminder_email_enabled: true) }
let(:delivery_method) { "pick_up" }

before do
Expand Down

0 comments on commit fc1e7b4

Please sign in to comment.