From c42f62e09f7a23aa91163ebdcd29b3fd0dbd24d9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 10 Aug 2023 14:55:50 +1000 Subject: [PATCH] Add custom OFN product id to DFC SuppliedProduct The Spree::Variant in OFN corresponds to a DFC SuppliedProduct. But several Spree::Variant can be grouped under one Spree::Product which wasn't exposed on the DFC API. I'm adding a custom property here which can be used internally and shouldn't break any other DFC tools. A gotcha of this first test implementation: The `ofn:` prefix has not been defined in the context. Software needs to know that this is an Open Food Network attribute or ignore it. We could define our own context and ontology and publish it on our website but I don't see any benefit of that at this point. --- .../app/services/supplied_product_builder.rb | 6 +++++- .../dfc_provider/spec/requests/catalog_items_spec.rb | 2 +- .../dfc_provider/spec/requests/enterprises_spec.rb | 2 +- .../spec/requests/supplied_products_spec.rb | 11 ++++++++++- swagger/dfc.yaml | 4 ++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/engines/dfc_provider/app/services/supplied_product_builder.rb b/engines/dfc_provider/app/services/supplied_product_builder.rb index fe036dcfb1..1f9ff42df8 100644 --- a/engines/dfc_provider/app/services/supplied_product_builder.rb +++ b/engines/dfc_provider/app/services/supplied_product_builder.rb @@ -13,7 +13,11 @@ class SuppliedProductBuilder < DfcBuilder description: variant.description, productType: product_type, quantity: QuantitativeValueBuilder.quantity(variant), - ) + ).tap do |supplied_product| + supplied_product.registerSemanticProperty("ofn:spree_product_id") do + variant.product.id + end + end end def self.import(supplied_product) diff --git a/engines/dfc_provider/spec/requests/catalog_items_spec.rb b/engines/dfc_provider/spec/requests/catalog_items_spec.rb index 0d401f9090..c082630044 100644 --- a/engines/dfc_provider/spec/requests/catalog_items_spec.rb +++ b/engines/dfc_provider/spec/requests/catalog_items_spec.rb @@ -15,7 +15,7 @@ describe "CatalogItems", type: :request, swagger_doc: "dfc.yaml", let(:product) { create( :base_product, - supplier: enterprise, name: "Apple", description: "Red", + id: 90_000, supplier: enterprise, name: "Apple", description: "Red", variants: [variant], ) } diff --git a/engines/dfc_provider/spec/requests/enterprises_spec.rb b/engines/dfc_provider/spec/requests/enterprises_spec.rb index 3c3835452f..0e5f885490 100644 --- a/engines/dfc_provider/spec/requests/enterprises_spec.rb +++ b/engines/dfc_provider/spec/requests/enterprises_spec.rb @@ -22,7 +22,7 @@ describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: let!(:product) { create( :base_product, - supplier: enterprise, name: "Apple", description: "Round", + id: 90_000, supplier: enterprise, name: "Apple", description: "Round", variants: [variant], ) } diff --git a/engines/dfc_provider/spec/requests/supplied_products_spec.rb b/engines/dfc_provider/spec/requests/supplied_products_spec.rb index b310fff021..966ff2b56f 100644 --- a/engines/dfc_provider/spec/requests/supplied_products_spec.rb +++ b/engines/dfc_provider/spec/requests/supplied_products_spec.rb @@ -9,6 +9,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", let!(:product) { create( :base_product, + id: 90_000, supplier: enterprise, name: "Pesto", description: "Basil Pesto", variants: [variant], ) @@ -78,7 +79,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", example.metadata[:operation][:parameters].first[:schema][:example] end - it "creates a variant" do |example| + it "creates a product and variant" do |example| expect { submit_request(example.metadata) } .to change { enterprise.supplied_products.count }.by(1) @@ -87,16 +88,23 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", %r|^http://test\.host/api/dfc/enterprises/10000/supplied_products/[0-9]+$| ) + spree_product_id = json_response["ofn:spree_product_id"].to_i + variant_id = dfc_id.split("/").last.to_i variant = Spree::Variant.find(variant_id) expect(variant.name).to eq "Apple" expect(variant.unit_value).to eq 3 + expect(variant.product_id).to eq spree_product_id # Insert static value to keep documentation deterministic: response.body.gsub!( "supplied_products/#{variant_id}", "supplied_products/10001" ) + .gsub!( + "\"ofn:spree_product_id\":#{spree_product_id}", + '"ofn:spree_product_id":90000' + ) end end end @@ -116,6 +124,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", run_test! do expect(response.body).to include variant.name + expect(json_response["ofn:spree_product_id"]).to eq 90_000 end end diff --git a/swagger/dfc.yaml b/swagger/dfc.yaml index 8927ccfc1d..f08222b960 100644 --- a/swagger/dfc.yaml +++ b/swagger/dfc.yaml @@ -118,6 +118,7 @@ paths: dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 + ofn:spree_product_id: 90000 - "@id": http://test.host/api/dfc/enterprises/10000/offers/10001 "@type": dfc-b:Offer dfc-b:price: 19.99 @@ -341,6 +342,7 @@ paths: dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 + ofn:spree_product_id: 90000 - "@id": http://test.host/api/dfc/enterprises/10000/catalog_items/10001 "@type": dfc-b:CatalogItem dfc-b:references: http://test.host/api/dfc/enterprises/10000/supplied_products/10001 @@ -409,6 +411,7 @@ paths: dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 + ofn:spree_product_id: 90000 requestBody: content: application/json: @@ -471,6 +474,7 @@ paths: dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 + ofn:spree_product_id: 90000 '404': description: not found put: