-
-
Notifications
You must be signed in to change notification settings - Fork 719
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
Changes from all commits
11f3bbc
74c80c9
09de223
f8bd0a1
4b20996
61aa02b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
class StockSyncJob < ApplicationJob | ||
# No retry but stay as failed job: | ||
sidekiq_options retry: 0 | ||
|
||
# We synchronise stock of stock-controlled variants linked to a remote | ||
# product. These variants are rare though and we check first before we | ||
# enqueue a new job. That should save some time loading the order with | ||
# all the stock data to make this decision. | ||
def self.sync_linked_catalogs(order) | ||
stock_controlled_variants = order.variants.reject(&:on_demand) | ||
links = SemanticLink.where(variant_id: stock_controlled_variants.map(&:id)) | ||
semantic_ids = links.pluck(:semantic_id) | ||
|
||
return if semantic_ids.empty? | ||
|
||
user = order.distributor.owner | ||
reference_id = semantic_ids.first # Assuming one catalog for now. | ||
perform_later(user, reference_id) | ||
rescue StandardError => e | ||
# Errors here shouldn't affect the shopping. So let's report them | ||
# separately: | ||
Bugsnag.notify(e) do |payload| | ||
payload.add_metadata(:order, order) | ||
end | ||
end | ||
|
||
def perform(user, semantic_id) | ||
urls = FdcUrlBuilder.new(semantic_id) | ||
json_catalog = DfcRequest.new(user).call(urls.catalog_url) | ||
graph = DfcIo.import(json_catalog) | ||
|
||
products = graph.select do |subject| | ||
subject.is_a? DataFoodConsortium::Connector::SuppliedProduct | ||
end | ||
products_by_id = products.index_by(&:semanticId) | ||
product_ids = products_by_id.keys | ||
variants = Spree::Variant.where(supplier: user.enterprises) | ||
.includes(:semantic_links).references(:semantic_links) | ||
.where(semantic_links: { semantic_id: product_ids }) | ||
|
||
variants.each do |variant| | ||
next if variant.on_demand | ||
|
||
product = products_by_id[variant.semantic_links[0].semantic_id] | ||
catalog_item = product&.catalogItems&.first | ||
CatalogItemBuilder.apply_stock(catalog_item, variant) | ||
variant.stock_items[0].save! | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the model (or a module like VariantStock) should be deciding what is backorderable? Perhaps it can even be a scope.