From be1f4e91e1009b8edd003526b233c610f1089b37 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 18 Mar 2025 16:57:42 +1100 Subject: [PATCH] Map currency to DFC codes Probably should have just hardcoded it. Hopefully we can remove this soon anyway. --- .../app/services/offer_builder.rb | 28 +++++++++++++++---- .../spec/services/offer_builder_spec.rb | 14 +++++++--- swagger/dfc.yaml | 6 ++-- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/engines/dfc_provider/app/services/offer_builder.rb b/engines/dfc_provider/app/services/offer_builder.rb index e2c1ff2c37..d21a3ef375 100644 --- a/engines/dfc_provider/app/services/offer_builder.rb +++ b/engines/dfc_provider/app/services/offer_builder.rb @@ -9,12 +9,7 @@ class OfferBuilder < DfcBuilder 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}", + unit: price_measure(variant)&.semanticId, ) DataFoodConsortium::Connector::Offer.new( id, price:, stockLimitation: stock_limitation(variant), @@ -39,4 +34,25 @@ class OfferBuilder < DfcBuilder offer.price end end + + # 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 + def self.price_measure(variant) + measures = DfcLoader.vocabulary("measures") + + case variant.currency + when "AUD" + measures.AUSTRALIANDOLLAR + when "CAD" + measures.CANADIANDOLLAR + when "EUR" + measures.EURO + when "GBP" + measures.POUNDSTERLING + when "USD" + measures.USDOLLAR + end + end end diff --git a/engines/dfc_provider/spec/services/offer_builder_spec.rb b/engines/dfc_provider/spec/services/offer_builder_spec.rb index 8dc7c334aa..a64c7affaa 100644 --- a/engines/dfc_provider/spec/services/offer_builder_spec.rb +++ b/engines/dfc_provider/spec/services/offer_builder_spec.rb @@ -27,13 +27,19 @@ RSpec.describe OfferBuilder do expect(offer.stockLimitation).to eq nil end - it "assigns a price with currency" do - variant.id = 5 - + it "assigns a price with mapped currency" do offer = OfferBuilder.build(variant) expect(offer.price.value).to eq 19.99 - expect(offer.price.unit).to eq "dfc-m:AUD" + expect(offer.price.unit).to eq "dfc-m:AustralianDollar" # Hopefully change to ISO 4217 soon + end + + it "assigns a price when unknown currency" do + variant.default_price.currency = "XXX" + offer = OfferBuilder.build(variant) + + expect(offer.price.value).to eq 19.99 + expect(offer.price.unit).to be nil end end end diff --git a/swagger/dfc.yaml b/swagger/dfc.yaml index 81b21a3bd3..03022bcca1 100644 --- a/swagger/dfc.yaml +++ b/swagger/dfc.yaml @@ -189,7 +189,7 @@ paths: dfc-b:hasPrice: "@type": dfc-b:Price dfc-b:value: 19.99 - dfc-b:hasUnit: dfc-m:AUD + dfc-b:hasUnit: dfc-m:AustralianDollar dfc-b:stockLimitation: 0 '401': description: unauthorized @@ -230,7 +230,7 @@ paths: dfc-b:hasPrice: "@type": dfc-b:Price dfc-b:value: 19.99 - dfc-b:hasUnit: dfc-m:AUD + dfc-b:hasUnit: dfc-m:AustralianDollar dfc-b:stockLimitation: 0 '404': description: not found @@ -514,7 +514,7 @@ paths: dfc-b:hasPrice: "@type": dfc-b:Price dfc-b:value: 19.99 - dfc-b:hasUnit: dfc-m:AUD + dfc-b:hasUnit: dfc-m:AustralianDollar dfc-b:stockLimitation: 5 put: summary: Update Offer