Skip to content

Commit

Permalink
Merge pull request openfoodfoundation#12994 from mkllnk/dfc-update-voc
Browse files Browse the repository at this point in the history
Add new DFC vocabulary for order states
  • Loading branch information
mkllnk authored Dec 4, 2024
2 parents bc97092 + 5719d06 commit 4c71ea3
Show file tree
Hide file tree
Showing 9 changed files with 864 additions and 13 deletions.
10 changes: 8 additions & 2 deletions app/services/fdc_backorderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def lookup_open_order(ofn_order)
.map { |id| find_order(id) }
.compact
# Just in case someone completed the order without updating our database:
.select { |o| o.orderStatus[:path] == "Held" }
.select { |o| o.orderStatus == order_status.HELD }
.first
# The DFC Connector doesn't recognise status values properly yet.
# So we are overriding the value with something that can be exported.
Expand All @@ -52,7 +52,7 @@ def lookup_open_order(ofn_order)
def find_last_open_order
graph = import(urls.orders_url)
open_orders = graph&.select do |o|
o.semanticType == "dfc-b:Order" && o.orderStatus[:path] == "Held"
o.semanticType == "dfc-b:Order" && o.orderStatus == order_status.HELD
end

return if open_orders.blank?
Expand Down Expand Up @@ -160,4 +160,10 @@ def build_sale_session(order)
session.semanticId = urls.sale_session_url
end
end

private

def order_status
DfcLoader.vocabulary("vocabulary").STATES.ORDERSTATE
end
end
7 changes: 3 additions & 4 deletions app/services/fdc_offer_broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def wholesale_product(product_id)
production_flow = catalog_item("#{product_id}/AsPlannedProductionFlow")

if production_flow
wholesale_product_id = production_flow.product
catalog_item(wholesale_product_id)
production_flow.product
else
# We didn't find a wholesale variant, falling back to the given product.
catalog_item(product_id)
Expand All @@ -57,7 +56,7 @@ def wholesale_to_retail(wholesale_product_id)
consumption_flow = catalog_item(
production_flow.semanticId.sub("AsPlannedProductionFlow", "AsPlannedConsumptionFlow")
)
retail_product_id = consumption_flow.product
retail_product_id = consumption_flow.product.semanticId

contained_quantity = consumption_flow.quantity.value.to_i

Expand All @@ -77,7 +76,7 @@ def catalog_item(id)
end

def flow_producing(wholesale_product_id)
@production_flows_by_product_id ||= production_flows.index_by(&:product)
@production_flows_by_product_id ||= production_flows.index_by { |flow| flow.product.semanticId }
@production_flows_by_product_id[wholesale_product_id]
end

Expand Down
22 changes: 19 additions & 3 deletions engines/dfc_provider/app/services/dfc_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

class DfcLoader
def self.connector
@connector ||= load_vocabularies
unless @connector
@connector = DataFoodConsortium::Connector::Connector.instance
load_context
load_vocabularies
end

@connector
end

def self.load_context
JSON::LD::Context.add_preloaded("http://www.datafoodconsortium.org/") {
JSON::LD::Context.parse(read_file("context_1.8.2")["@context"])
}
end

def self.vocabulary(name)
@vocabs ||= {}
@vocabs[name] ||= connector.__send__(:loadThesaurus, read_file(name))
end

def self.load_vocabularies
connector = DataFoodConsortium::Connector::Connector.instance
connector.loadMeasures(read_file("measures"))
connector.loadFacets(read_file("facets"))
connector.loadProductTypes(read_file("productTypes"))
connector
vocabulary("vocabulary") # order states etc
end

def self.read_file(name)
Expand Down
1 change: 1 addition & 0 deletions engines/dfc_provider/script/update-dfc-vocabularies
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mkdir -p "$dst"
curl --location "$src/facets.json" > "$dst/facets.json"
curl --location "$src/measures.json" > "$dst/measures.json"
curl --location "$src/productTypes.json" > "$dst/productTypes.json"
curl --location "$src/vocabulary.json" > "$dst/vocabulary.json"
21 changes: 21 additions & 0 deletions engines/dfc_provider/spec/services/dfc_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
let(:enterprise) do
DataFoodConsortium::Connector::Enterprise.new("Pete's Pumpkins")
end
let(:order) do
DataFoodConsortium::Connector::Order.new("https://example.net", orderStatus: orderstate.HELD)
end
let(:orderstate) do
DfcLoader.vocabulary("vocabulary").STATES.ORDERSTATE
end

describe ".export" do
it "exports nothing" do
Expand All @@ -33,5 +39,20 @@
*%w(@id @type dfc-b:affiliates)
)
end

it "recognises loaded vocabularies" do
json = DfcIo.export(order)
result = JSON.parse(json)

expect(result["dfc-b:hasOrderStatus"]).to eq "dfc-v:Held"
end
end

describe ".import" do
it "recognises loaded vocabularies" do
result = DfcIo.import(DfcIo.export(order))

expect(result.orderStatus).to eq orderstate.HELD
end
end
end
6 changes: 6 additions & 0 deletions engines/dfc_provider/spec/services/dfc_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
)
expect(result["dfc-b:name"]).to eq "Tomato"
end

it "loads vocabularies" do
terms = DfcLoader.vocabulary("vocabulary")
expect(terms.STATES.ORDERSTATE.HELD.semanticId)
.to eq "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/vocabulary.rdf#Held"
end
end
Loading

0 comments on commit 4c71ea3

Please sign in to comment.