diff --git a/app/jobs/complete_backorder_job.rb b/app/jobs/complete_backorder_job.rb index e4ffd836d1..3c9f771045 100644 --- a/app/jobs/complete_backorder_job.rb +++ b/app/jobs/complete_backorder_job.rb @@ -23,6 +23,15 @@ class CompleteBackorderJob < ApplicationJob adjust_quantities(user, order, urls, variants) FdcBackorderer.new(user, urls).complete_order(order) + rescue StandardError => e + Bugsnag.notify(e) do |payload| + payload.add_metadata(:user, user) + payload.add_metadata(:distributor, distributor) + payload.add_metadata(:order_cycle, order_cycle) + payload.add_metadata(:order_id, order_id) + end + + BackorderMailer.backorder_incomplete(user, distributor, order_cycle, order_id).deliver_later end # Check if we have enough stock to reduce the backorder. diff --git a/app/mailers/backorder_mailer.rb b/app/mailers/backorder_mailer.rb index d9f9e8d0c4..a3ecb34be7 100644 --- a/app/mailers/backorder_mailer.rb +++ b/app/mailers/backorder_mailer.rb @@ -11,4 +11,14 @@ class BackorderMailer < ApplicationMailer mail(to: order.distributor.owner.email) end end + + def backorder_incomplete(user, distributor, order_cycle, order_id) + @distributor = distributor + @order_cycle = order_cycle + @order_id = order_id + + I18n.with_locale valid_locale(user) do + mail(to: user.email) + end + end end diff --git a/app/views/backorder_mailer/backorder_incomplete.haml b/app/views/backorder_mailer/backorder_incomplete.haml new file mode 100644 index 0000000000..a67acde5ac --- /dev/null +++ b/app/views/backorder_mailer/backorder_incomplete.haml @@ -0,0 +1,9 @@ +%h1= t ".headline" + +%p= t ".description" + +%p= t ".hints" + +%p= t ".affected", enterprise: @distributor.name, order_cycle: @order_cycle.name + +%pre= @order_id diff --git a/config/locales/en.yml b/config/locales/en.yml index 493053bfc3..d208b21926 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -376,6 +376,20 @@ en: order: "Affected order: %{number}" stock: "Stock" product: "Product" + backorder_incomplete: + subject: "An automatic backorder failed to complete" + headline: "Your backorder is still a draft" + description: | + We tried to complete a backorder for out-of-stock items but + something went wrong. The backorder quantities may be too high if + you had cancellations. And your backorder won't be fulfilled while + it's in draft state. + hints: | + You may need to go to the OIDC settings and reconnect your account. + Also check that your supplier's catalog hasn't changed and is still + supplying all products you need. And please get in touch with us if + you have any questions. + affected: "%{enterprise}: %{order_cycle}" enterprise_mailer: confirmation_instructions: subject: "Please confirm the email address for %{enterprise}" diff --git a/spec/jobs/complete_backorder_job_spec.rb b/spec/jobs/complete_backorder_job_spec.rb index 8c268e42e6..73eaa520ff 100644 --- a/spec/jobs/complete_backorder_job_spec.rb +++ b/spec/jobs/complete_backorder_job_spec.rb @@ -88,5 +88,20 @@ RSpec.describe CompleteBackorderJob do variant.on_hand }.from(49).to(13) # minus 3 backordered slabs (3 * 12 = 36) end + + it "reports errors" do + expect(Bugsnag).to receive(:notify).and_call_original + + expect { + subject.perform(user, distributor, order_cycle, "https://nil") + }.not_to raise_error + + # Combined example for performance + expect(Bugsnag).to receive(:notify).and_call_original + + expect { + subject.perform(user, distributor, order_cycle, "https://nil") + }.to enqueue_mail(BackorderMailer, :backorder_incomplete) + end end end diff --git a/spec/mailers/backorder_mailer_spec.rb b/spec/mailers/backorder_mailer_spec.rb index 95aa377a63..1747632298 100644 --- a/spec/mailers/backorder_mailer_spec.rb +++ b/spec/mailers/backorder_mailer_spec.rb @@ -17,4 +17,19 @@ RSpec.describe BackorderMailer do expect(mail.subject).to eq "An automatic backorder failed" end end + + describe "#backorder_incomplete" do + let(:user) { build(:user, email: "jane@example.net") } + let(:distributor) { build(:enterprise) } + let(:order_cycle) { build(:order_cycle) } + let(:order_id) { "https://null" } + + it "notifies the owner" do + BackorderMailer.backorder_incomplete(user, distributor, order_cycle, order_id).deliver_now + + mail = ActionMailer::Base.deliveries.first + expect(mail.to).to eq ["jane@example.net"] + expect(mail.subject).to eq "An automatic backorder failed to complete" + end + end end diff --git a/spec/mailers/previews/backorder_mailer_preview.rb b/spec/mailers/previews/backorder_mailer_preview.rb index 3741ff393d..4aea240555 100644 --- a/spec/mailers/previews/backorder_mailer_preview.rb +++ b/spec/mailers/previews/backorder_mailer_preview.rb @@ -9,4 +9,16 @@ class BackorderMailerPreview < ActionMailer::Preview order.line_items.map(&:variant), ) end + + def backorder_incomplete + order = Spree::Order.complete.last || Spree::Order.last + order_cycle = order.order_cycle + distributor = order.distributor + user = distributor.owner + order_id = "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/Orders/1177603473714" + + BackorderMailer.backorder_incomplete( + user, distributor, order_cycle, order_id + ) + end end