mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-30 06:31:16 +00:00
Fall back to given product w/o wholesale variant
The class is moving to providing all data with several methods instead of a data class containing the information. That should be more flexible. Still some work to do.
This commit is contained in:
@@ -11,16 +11,31 @@ class FdcOfferBroker
|
||||
end
|
||||
|
||||
def best_offer(product_id)
|
||||
consumption_flow = catalog_item("#{product_id}/AsPlannedConsumptionFlow")
|
||||
Solution.new(
|
||||
wholesale_product(product_id),
|
||||
contained_quantity(product_id),
|
||||
offer_of(wholesale_product(product_id))
|
||||
)
|
||||
end
|
||||
|
||||
def wholesale_product(product_id)
|
||||
production_flow = catalog_item("#{product_id}/AsPlannedProductionFlow")
|
||||
|
||||
contained_quantity = consumption_flow.quantity.value.to_i
|
||||
wholesale_product_id = production_flow.product
|
||||
wholesale_product = catalog_item(wholesale_product_id)
|
||||
if production_flow
|
||||
wholesale_product_id = production_flow.product
|
||||
catalog_item(wholesale_product_id)
|
||||
else
|
||||
# We didn't find a wholesale variant, falling back to the given product.
|
||||
catalog_item(product_id)
|
||||
end
|
||||
end
|
||||
|
||||
offer = offer_of(wholesale_product)
|
||||
def contained_quantity(product_id)
|
||||
consumption_flow = catalog_item("#{product_id}/AsPlannedConsumptionFlow")
|
||||
|
||||
Solution.new(wholesale_product, contained_quantity, offer)
|
||||
# If we don't find a transformation, we return the original product,
|
||||
# which contains exactly one of itself (identity).
|
||||
consumption_flow&.quantity&.value&.to_i || 1
|
||||
end
|
||||
|
||||
def wholesale_to_retail(wholesale_product_id)
|
||||
|
||||
198
spec/fixtures/vcr_cassettes/FdcOfferBroker/_best_offer/falls_back_to_the_original_product_offer.yml
vendored
Normal file
198
spec/fixtures/vcr_cassettes/FdcOfferBroker/_best_offer/falls_back_to_the_original_product_offer.yml
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -24,5 +24,15 @@ RSpec.describe FdcOfferBroker do
|
||||
expect(solution.factor).to eq 12
|
||||
expect(solution.offer.offeredItem).to eq solution.product
|
||||
end
|
||||
|
||||
it "falls back to the original product offer", vcr: true do
|
||||
solution = subject.best_offer(product.semanticId)
|
||||
fallback_solution = subject.best_offer(solution.product.semanticId)
|
||||
|
||||
# These values depend on the test data but are a good sanity check:
|
||||
expect(fallback_solution.product.name).to eq "Baked British Beans - Case, 12 x 400g (can)"
|
||||
expect(fallback_solution.factor).to eq 1
|
||||
expect(fallback_solution.offer.offeredItem).to eq fallback_solution.product
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user