Map currency to DFC codes

Probably should have just hardcoded it. Hopefully we can remove this soon anyway.
This commit is contained in:
David Cook
2025-03-18 16:57:42 +11:00
parent e6ef52f91c
commit be1f4e91e1
3 changed files with 35 additions and 13 deletions

View File

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

View File

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

View File

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