Send error notification to owner

This commit is contained in:
Maikel Linke
2024-09-25 16:26:59 +10:00
parent 49fd1dc4a6
commit 495634b60c
7 changed files with 86 additions and 0 deletions

View File

@@ -42,6 +42,8 @@ class BackorderJob < ApplicationJob
payload.add_metadata(:order, order)
payload.add_metadata(:linked_variants, linked_variants)
end
BackorderMailer.backorder_failed(order, linked_variants).deliver_later
end
def place_backorder(order, linked_variants)

View File

@@ -0,0 +1,14 @@
# frozen_string_literal: true
class BackorderMailer < ApplicationMailer
include I18nHelper
def backorder_failed(order, linked_variants)
@order = order
@linked_variants = linked_variants
I18n.with_locale valid_locale(order.distributor.owner) do
mail(to: order.distributor.owner.email)
end
end
end

View File

@@ -0,0 +1,16 @@
%h1= t ".headline"
%p= t ".description"
%p= t ".hints"
%p= t ".order", number: @order.number
%table
%tr
%th= t ".stock"
%th= t ".product"
- @linked_variants.each do |variant|
%tr
%td= variant.on_hand
%td= variant.product_and_full_name

View File

@@ -360,6 +360,22 @@ en:
report_failed: |
This report failed. It may be too big to process.
We will look into it but please let us know if the problem persists.
backorder_mailer:
backorder_failed:
subject: "An automatic backorder failed"
headline: "Backordering failed"
description: |
We tried to place or update a backorder for out-of-stock items but
something went wrong. You may have negative stock and need to resolve
the issue to order more stock in.
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.
order: "Affected order: %{number}"
stock: "Stock"
product: "Product"
enterprise_mailer:
confirmation_instructions:
subject: "Please confirm the email address for %{enterprise}"

View File

@@ -54,6 +54,12 @@ RSpec.describe BackorderJob do
subject.perform(nil, [])
}.not_to raise_error
end
it "notifies owner of errors" do
expect {
subject.perform(order, [])
}.to enqueue_mail(BackorderMailer, :backorder_failed)
end
end
describe "#place_backorder" do

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BackorderMailer do
let(:order) { create(:completed_order_with_totals) }
let(:variants) { order.line_items.map(&:variant) }
describe "#backorder_failed" do
it "notifies the owner" do
order.distributor.owner.email = "jane@example.net"
BackorderMailer.backorder_failed(order, variants).deliver_now
mail = ActionMailer::Base.deliveries.first
expect(mail.to).to eq ["jane@example.net"]
expect(mail.subject).to eq "An automatic backorder failed"
end
end
end

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
class BackorderMailerPreview < ActionMailer::Preview
def backorder_failed
order = Spree::Order.complete.last || Spree::Order.last
BackorderMailer.backorder_failed(
order,
order.line_items.map(&:variant),
)
end
end