From 84648690a6781ff4035e8c15457862faa899ddce Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 14 Jan 2025 15:36:28 +1100 Subject: [PATCH] Publish price as in new DFC standard --- .../dfc_provider/app/services/offer_builder.rb | 13 ++++++++++--- .../spec/services/offer_builder_spec.rb | 11 ++++++++++- swagger/dfc.yaml | 15 ++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/engines/dfc_provider/app/services/offer_builder.rb b/engines/dfc_provider/app/services/offer_builder.rb index d8c9cdfffe..e2c1ff2c37 100644 --- a/engines/dfc_provider/app/services/offer_builder.rb +++ b/engines/dfc_provider/app/services/offer_builder.rb @@ -7,10 +7,17 @@ class OfferBuilder < DfcBuilder id: variant.id, ) + price = DataFoodConsortium::Connector::Price.new( + value: variant.price.to_f, + + # The DFC measures define only five currencies at the moment. + # And they are not standardised enough to align with our ISO 4217 + # currency codes. So I propose to just use those currency codes instead. + # https://github.com/datafoodconsortium/taxonomies/issues/48 + unit: "dfc-m:#{variant.currency}", + ) DataFoodConsortium::Connector::Offer.new( - id, - price: variant.price.to_f, - stockLimitation: stock_limitation(variant), + id, price:, stockLimitation: stock_limitation(variant), ) end diff --git a/engines/dfc_provider/spec/services/offer_builder_spec.rb b/engines/dfc_provider/spec/services/offer_builder_spec.rb index 0fd9732d47..8dc7c334aa 100644 --- a/engines/dfc_provider/spec/services/offer_builder_spec.rb +++ b/engines/dfc_provider/spec/services/offer_builder_spec.rb @@ -3,7 +3,7 @@ require_relative "../spec_helper" RSpec.describe OfferBuilder do - let(:variant) { build(:variant) } + let(:variant) { build(:variant, id: 5) } describe ".offer" do it "assigns a stock level" do @@ -26,5 +26,14 @@ RSpec.describe OfferBuilder do expect(offer.stockLimitation).to eq nil end + + it "assigns a price with currency" do + variant.id = 5 + + offer = OfferBuilder.build(variant) + + expect(offer.price.value).to eq 19.99 + expect(offer.price.unit).to eq "dfc-m:AUD" + end end end diff --git a/swagger/dfc.yaml b/swagger/dfc.yaml index 3f325a5b73..0724baad61 100644 --- a/swagger/dfc.yaml +++ b/swagger/dfc.yaml @@ -181,7 +181,10 @@ paths: ofn:spree_product_uri: http://test.host/api/dfc/enterprises/10000?spree_product_id=90000 - "@id": http://test.host/api/dfc/enterprises/10000/offers/10001 "@type": dfc-b:Offer - dfc-b:hasPrice: 19.99 + dfc-b:hasPrice: + "@type": dfc-b:Price + dfc-b:value: 19.99 + dfc-b:hasUnit: dfc-m:AUD dfc-b:stockLimitation: 0 '401': description: unauthorized @@ -219,7 +222,10 @@ paths: dfc-b:offeredThrough: http://test.host/api/dfc/enterprises/10000/offers/10001 - "@id": http://test.host/api/dfc/enterprises/10000/offers/10001 "@type": dfc-b:Offer - dfc-b:hasPrice: 19.99 + dfc-b:hasPrice: + "@type": dfc-b:Price + dfc-b:value: 19.99 + dfc-b:hasUnit: dfc-m:AUD dfc-b:stockLimitation: 0 '404': description: not found @@ -499,7 +505,10 @@ paths: "@context": https://www.datafoodconsortium.org "@id": http://test.host/api/dfc/enterprises/10000/offers/10001 "@type": dfc-b:Offer - dfc-b:hasPrice: 19.99 + dfc-b:hasPrice: + "@type": dfc-b:Price + dfc-b:value: 19.99 + dfc-b:hasUnit: dfc-m:AUD dfc-b:stockLimitation: 5 put: summary: Update Offer