Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DFC Orders] Backorder stock controlled products #12888

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions app/jobs/backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ class BackorderJob < ApplicationJob
sidekiq_options retry: 0

def self.check_stock(order)
variants_needing_stock = order.variants.select do |variant|
# TODO: scope variants to hub.
# We are only supporting producer stock at the moment.
variant.on_hand&.negative?
end

linked_variants = variants_needing_stock.select do |variant|
variant.semantic_links.present?
end
links = SemanticLink.where(variant_id: order.line_items.select(:variant_id))

perform_later(order, linked_variants) if linked_variants.present?
perform_later(order) if links.exists?
rescue StandardError => e
# Errors here shouldn't affect the checkout. So let's report them
# separately:
Expand All @@ -32,20 +24,25 @@ def self.check_stock(order)
end
end

def perform(order, linked_variants)
def perform(order)
OrderLocker.lock_order_and_variants(order) do
place_backorder(order, linked_variants)
place_backorder(order)
end
rescue StandardError
# If the backordering fails, we need to tell the shop owner because they
# need to organgise more stock.
BackorderMailer.backorder_failed(order, linked_variants).deliver_later
BackorderMailer.backorder_failed(order).deliver_later

raise
end

def place_backorder(order, linked_variants)
def place_backorder(order)
user = order.distributor.owner
linked_variants = order.variants.select do |variant|
# TODO: scope variants to hub.
# We are only supporting producer stock at the moment.
variant.on_hand&.negative? && variant.semantic_links.present?
end

# We are assuming that all variants are linked to the same wholesale
# shop and its catalog:
Expand Down
4 changes: 2 additions & 2 deletions app/mailers/backorder_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
class BackorderMailer < ApplicationMailer
include I18nHelper

def backorder_failed(order, linked_variants)
def backorder_failed(order)
@order = order
@linked_variants = linked_variants
@linked_variants = order.variants
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now lists all variants associated with an order, whether they're linked or not. Hmm, regardless we probably only want to know about variants that had errors. I'll wait to see where the PR goes..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we don't actually know what users want. We have no data on that. In the pilots, I think, all variants will be linked. Looking at errors could be good but also another source for errors. I think that we should wait until this actually happens and what people say about it.


I18n.with_locale valid_locale(order.distributor.owner) do
mail(to: order.distributor.owner.email)
Expand Down
7 changes: 4 additions & 3 deletions spec/jobs/backorder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

expect {
BackorderJob.check_stock(order)
}.to enqueue_job(BackorderJob).with(order, [variant])
}.to enqueue_job(BackorderJob).with(order)
end

it "reports errors" do
Expand All @@ -48,8 +48,9 @@

describe "#peform" do
it "notifies owner of errors" do
incorrect_order = create(:order)
expect {
subject.perform(order, [])
subject.perform(incorrect_order)
}.to enqueue_mail(BackorderMailer, :backorder_failed)
.and raise_error(NoMethodError)
end
Expand All @@ -70,7 +71,7 @@
)

expect {
subject.place_backorder(order, [variant])
subject.place_backorder(order)
}.to enqueue_job(CompleteBackorderJob).at(completion_time)

# We ordered a case of 12 cans: -3 + 12 = 9
Expand Down
3 changes: 1 addition & 2 deletions spec/mailers/backorder_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

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
BackorderMailer.backorder_failed(order).deliver_now

mail = ActionMailer::Base.deliveries.first
expect(mail.to).to eq ["jane@example.net"]
Expand Down