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:
Maikel Linke
2024-09-16 15:48:03 +10:00
parent 4303f0e974
commit fb96f8f936
3 changed files with 229 additions and 6 deletions

View File

@@ -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)

File diff suppressed because one or more lines are too long

View File

@@ -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