Skip to content

Commit

Permalink
Send error notification to owner
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Sep 25, 2024
1 parent ef4c2d1 commit 16ff220
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/jobs/backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def perform(order, linked_variants)
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)
Expand Down
14 changes: 14 additions & 0 deletions app/mailers/backorder_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions app/views/backorder_mailer/backorder_failed.haml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
6 changes: 6 additions & 0 deletions spec/jobs/backorder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
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
Expand Down
20 changes: 20 additions & 0 deletions spec/mailers/backorder_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions spec/mailers/previews/backorder_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 16ff220

Please sign in to comment.