From 495634b60ce6fd9031c5a2f65d5a066ca1f65934 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 25 Sep 2024 16:26:59 +1000 Subject: [PATCH] Send error notification to owner --- app/jobs/backorder_job.rb | 2 ++ app/mailers/backorder_mailer.rb | 14 +++++++++++++ .../backorder_mailer/backorder_failed.haml | 16 +++++++++++++++ config/locales/en.yml | 16 +++++++++++++++ spec/jobs/backorder_job_spec.rb | 6 ++++++ spec/mailers/backorder_mailer_spec.rb | 20 +++++++++++++++++++ .../previews/backorder_mailer_preview.rb | 12 +++++++++++ 7 files changed, 86 insertions(+) create mode 100644 app/mailers/backorder_mailer.rb create mode 100644 app/views/backorder_mailer/backorder_failed.haml create mode 100644 spec/mailers/backorder_mailer_spec.rb create mode 100644 spec/mailers/previews/backorder_mailer_preview.rb diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index ef91e35c8a..b48acda4a8 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -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) diff --git a/app/mailers/backorder_mailer.rb b/app/mailers/backorder_mailer.rb new file mode 100644 index 0000000000..d9f9e8d0c4 --- /dev/null +++ b/app/mailers/backorder_mailer.rb @@ -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 diff --git a/app/views/backorder_mailer/backorder_failed.haml b/app/views/backorder_mailer/backorder_failed.haml new file mode 100644 index 0000000000..1add17d2eb --- /dev/null +++ b/app/views/backorder_mailer/backorder_failed.haml @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index f1041c92bb..493053bfc3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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}" diff --git a/spec/jobs/backorder_job_spec.rb b/spec/jobs/backorder_job_spec.rb index 8850adc244..c8a329e092 100644 --- a/spec/jobs/backorder_job_spec.rb +++ b/spec/jobs/backorder_job_spec.rb @@ -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 diff --git a/spec/mailers/backorder_mailer_spec.rb b/spec/mailers/backorder_mailer_spec.rb new file mode 100644 index 0000000000..95aa377a63 --- /dev/null +++ b/spec/mailers/backorder_mailer_spec.rb @@ -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 diff --git a/spec/mailers/previews/backorder_mailer_preview.rb b/spec/mailers/previews/backorder_mailer_preview.rb new file mode 100644 index 0000000000..3741ff393d --- /dev/null +++ b/spec/mailers/previews/backorder_mailer_preview.rb @@ -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