mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Move building of Offer to the right module
The DfcBuilder was doing everything to start with but we are moving its parts to smaller modules now.
This commit is contained in:
@@ -5,7 +5,7 @@ module DfcProvider
|
||||
before_action :check_enterprise
|
||||
|
||||
def show
|
||||
subject = DfcBuilder.offer(variant)
|
||||
subject = OfferBuilder.build(variant)
|
||||
render json: DfcIo.export(subject)
|
||||
end
|
||||
|
||||
|
||||
@@ -12,22 +12,7 @@ class DfcBuilder
|
||||
id, product:,
|
||||
sku: variant.sku,
|
||||
stockLimitation: stock_limitation(variant),
|
||||
offers: [offer(variant)],
|
||||
)
|
||||
end
|
||||
|
||||
def self.offer(variant)
|
||||
# We don't have an endpoint for offers yet and this URL is only a
|
||||
# placeholder for now. The offer is actually affected by order cycle and
|
||||
# customer tags. We'll solve that at a later stage.
|
||||
enterprise_url = urls.enterprise_url(id: variant.product.supplier_id)
|
||||
id = "#{enterprise_url}/offers/#{variant.id}"
|
||||
offered_to = []
|
||||
|
||||
DataFoodConsortium::Connector::Offer.new(
|
||||
id, offeredTo: offered_to,
|
||||
price: variant.price.to_f,
|
||||
stockLimitation: stock_limitation(variant),
|
||||
offers: [OfferBuilder.build(variant)],
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,6 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class OfferBuilder < DfcBuilder
|
||||
def self.build(variant)
|
||||
id = urls.enterprise_offer_url(
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
|
||||
DataFoodConsortium::Connector::Offer.new(
|
||||
id,
|
||||
price: variant.price.to_f,
|
||||
stockLimitation: stock_limitation(variant),
|
||||
)
|
||||
end
|
||||
|
||||
def self.apply(offer, variant)
|
||||
variant.on_hand = offer.stockLimitation
|
||||
variant.price = offer.price
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require_relative "../spec_helper"
|
||||
|
||||
describe DfcBuilder do
|
||||
describe OfferBuilder do
|
||||
let(:variant) { build(:variant) }
|
||||
|
||||
describe ".offer" do
|
||||
@@ -11,7 +11,7 @@ describe DfcBuilder do
|
||||
variant.save!
|
||||
variant.on_hand = 5
|
||||
|
||||
offer = DfcBuilder.offer(variant)
|
||||
offer = OfferBuilder.build(variant)
|
||||
|
||||
expect(offer.stockLimitation).to eq 5
|
||||
end
|
||||
@@ -22,7 +22,7 @@ describe DfcBuilder do
|
||||
variant.on_hand = 5
|
||||
variant.on_demand = true
|
||||
|
||||
offer = DfcBuilder.offer(variant)
|
||||
offer = OfferBuilder.build(variant)
|
||||
|
||||
expect(offer.stockLimitation).to eq nil
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user