Notify user of failed backorder completion

This commit is contained in:
Maikel Linke
2024-09-26 10:37:23 +10:00
parent 495634b60c
commit 989a6d57e0
7 changed files with 84 additions and 0 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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

View File

@@ -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}"

View File

@@ -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

View File

@@ -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

View File

@@ -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