From 424954ec54f74721e3d2c912de48523fae2eba90 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 3 Nov 2023 14:44:37 +1100 Subject: [PATCH 1/6] Add ofn:image to SuppliedProduct Temporary solution as we wait for the DFC connector to be updated to support "dfc_b:image" for SuppliedProduct --- engines/dfc_provider/lib/dfc_provider/supplied_product.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/engines/dfc_provider/lib/dfc_provider/supplied_product.rb b/engines/dfc_provider/lib/dfc_provider/supplied_product.rb index 06b762ba87..dcaf35d9b5 100644 --- a/engines/dfc_provider/lib/dfc_provider/supplied_product.rb +++ b/engines/dfc_provider/lib/dfc_provider/supplied_product.rb @@ -3,15 +3,21 @@ module DfcProvider class SuppliedProduct < DataFoodConsortium::Connector::SuppliedProduct attr_accessor :spree_product_id + attr_accessor :image - def initialize(semantic_id, spree_product_id: nil, **properties) + def initialize(semantic_id, spree_product_id: nil, image_url: nil, **properties) super(semantic_id, **properties) self.spree_product_id = spree_product_id + self.image = image_url registerSemanticProperty("ofn:spree_product_id") do self.spree_product_id end + # Temporary solution, will be replaced by "dfc_b:image" in future version of the DFC connector + registerSemanticProperty("ofn:image") do + self.image + end end end end From 12c6257a0f8c1ec6c36a4d606a7928b0477edf95 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 3 Nov 2023 16:00:49 +1100 Subject: [PATCH 2/6] Pass the image url to DfcProvider::SuppliedProduct --- .../app/services/supplied_product_builder.rb | 1 + .../spec/services/supplied_product_builder_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/engines/dfc_provider/app/services/supplied_product_builder.rb b/engines/dfc_provider/app/services/supplied_product_builder.rb index a706a72eb7..23be177ee1 100644 --- a/engines/dfc_provider/app/services/supplied_product_builder.rb +++ b/engines/dfc_provider/app/services/supplied_product_builder.rb @@ -14,6 +14,7 @@ class SuppliedProductBuilder < DfcBuilder productType: product_type, quantity: QuantitativeValueBuilder.quantity(variant), spree_product_id: variant.product.id, + image_url: variant.product&.image&.url(:product) ) end diff --git a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb index 53ac99f16a..21d4c8fcbb 100644 --- a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb +++ b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb @@ -3,6 +3,8 @@ require_relative "../spec_helper" describe SuppliedProductBuilder do + include FileHelper + subject(:builder) { described_class } let(:variant) { build(:variant, id: 5).tap { |v| v.product.supplier_id = 7 } @@ -45,5 +47,16 @@ describe SuppliedProductBuilder do expect(product.productType).to eq vegetable end + + it "assigns an image_url type" do + image = Spree::Image.create!( + attachment: white_logo_file, + viewable_id: variant.product.id, + viewable_type: 'Spree::Product' + ) + product = builder.supplied_product(variant) + + expect(product.image).to eq variant.product.image.url(:product) + end end end From 7987967824f102eb57408ff26ae631b6b5cd5d89 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 3 Nov 2023 16:03:23 +1100 Subject: [PATCH 3/6] Check product image url is present on supplied product endpoint --- engines/dfc_provider/spec/requests/enterprises_spec.rb | 2 +- .../dfc_provider/spec/requests/supplied_products_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/engines/dfc_provider/spec/requests/enterprises_spec.rb b/engines/dfc_provider/spec/requests/enterprises_spec.rb index 0e5f885490..2f8e5a6fa3 100644 --- a/engines/dfc_provider/spec/requests/enterprises_spec.rb +++ b/engines/dfc_provider/spec/requests/enterprises_spec.rb @@ -21,7 +21,7 @@ describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: end let!(:product) { create( - :base_product, + :product_with_image, 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 a3e2ed2d77..47e7d7440f 100644 --- a/engines/dfc_provider/spec/requests/supplied_products_spec.rb +++ b/engines/dfc_provider/spec/requests/supplied_products_spec.rb @@ -2,13 +2,12 @@ require_relative "../swagger_helper" -describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", - rswag_autodoc: true do +describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: true do let!(:user) { create(:oidc_user) } let!(:enterprise) { create(:distributor_enterprise, id: 10_000, owner: user) } let!(:product) { create( - :base_product, + :product_with_image, id: 90_000, supplier: enterprise, name: "Pesto", description: "Basil Pesto", variants: [variant], @@ -146,6 +145,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 + expect(json_response["ofn:image"]).to include("logo-white.png") end end From 11ee4b30c2d32f1e671532029b02c12a8587614b Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 3 Nov 2023 16:04:53 +1100 Subject: [PATCH 4/6] Update Swagger documentation to include ofn:image --- swagger/dfc.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/swagger/dfc.yaml b/swagger/dfc.yaml index f08222b960..06c89d015b 100644 --- a/swagger/dfc.yaml +++ b/swagger/dfc.yaml @@ -343,11 +343,12 @@ paths: dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 ofn:spree_product_id: 90000 + ofn:image: http://test.host/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZzBCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cec5b9e04720cada9134f6757e3dd8156aeccbf0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJY0c1bkJqb0dSVlE2RkhKbGMybDZaVjkwYjE5c2FXMXBkRnNIYVFId2FRSHciLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--a8632e6a563a139dfae774530b275266f6aef5f5/logo-white.png - "@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 dfc-b:sku: APP - dfc-b:stockLimitation: 0 + dfc-b:stockLimitation: 5 dfc-b:offeredThrough: http://test.host/api/dfc/enterprises/10000/offers/10001 '404': description: not found @@ -406,7 +407,7 @@ paths: dfc-b:hasQuantity: "@type": dfc-b:QuantitativeValue dfc-b:hasUnit: dfc-m:Gram - dfc-b:value: 3.0 + dfc-b:value: 6.0 dfc-b:alcoholPercentage: 0.0 dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' @@ -431,11 +432,12 @@ paths: dfc-b:hasQuantity: "@type": dfc-b:QuantitativeValue dfc-b:hasUnit: dfc-m:Gram - dfc-b:value: 3.0 + dfc-b:value: 6 dfc-b:alcoholPercentage: 0.0 dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 + ofn:spree_product_id: 33774 "/api/dfc/enterprises/{enterprise_id}/supplied_products/{id}": parameters: - name: enterprise_id @@ -475,6 +477,7 @@ paths: dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 ofn:spree_product_id: 90000 + ofn:image: http://test.host/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBaElCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f578e58da95a3e1f6a4774dfeef31ac7cb039f0f/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJY0c1bkJqb0dSVlE2RkhKbGMybDZaVjkwYjE5c2FXMXBkRnNIYVFId2FRSHciLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--a8632e6a563a139dfae774530b275266f6aef5f5/logo-white.png '404': description: not found put: From f76bdf0c6f48ae21c388cdd856999876b59c9f3b Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 3 Nov 2023 16:46:42 +1100 Subject: [PATCH 5/6] Fix rubocop warning --- engines/dfc_provider/lib/dfc_provider/supplied_product.rb | 5 ++--- .../spec/services/supplied_product_builder_spec.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/engines/dfc_provider/lib/dfc_provider/supplied_product.rb b/engines/dfc_provider/lib/dfc_provider/supplied_product.rb index dcaf35d9b5..2e9e3a22c2 100644 --- a/engines/dfc_provider/lib/dfc_provider/supplied_product.rb +++ b/engines/dfc_provider/lib/dfc_provider/supplied_product.rb @@ -2,8 +2,7 @@ module DfcProvider class SuppliedProduct < DataFoodConsortium::Connector::SuppliedProduct - attr_accessor :spree_product_id - attr_accessor :image + attr_accessor :spree_product_id, :image def initialize(semantic_id, spree_product_id: nil, image_url: nil, **properties) super(semantic_id, **properties) @@ -16,7 +15,7 @@ module DfcProvider end # Temporary solution, will be replaced by "dfc_b:image" in future version of the DFC connector registerSemanticProperty("ofn:image") do - self.image + image end end end diff --git a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb index 21d4c8fcbb..464cc73036 100644 --- a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb +++ b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb @@ -49,7 +49,7 @@ describe SuppliedProductBuilder do end it "assigns an image_url type" do - image = Spree::Image.create!( + Spree::Image.create!( attachment: white_logo_file, viewable_id: variant.product.id, viewable_type: 'Spree::Product' From d0dd6e57ca682758328cdbc41c564aecd087a2f9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 8 Nov 2023 17:12:42 +1100 Subject: [PATCH 6/6] Replace variable values with fixed values in Swagger doc --- engines/dfc_provider/spec/requests/enterprises_spec.rb | 6 ++++++ .../dfc_provider/spec/requests/supplied_products_spec.rb | 7 +++++++ swagger/dfc.yaml | 6 +++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/engines/dfc_provider/spec/requests/enterprises_spec.rb b/engines/dfc_provider/spec/requests/enterprises_spec.rb index 2f8e5a6fa3..574e3c4400 100644 --- a/engines/dfc_provider/spec/requests/enterprises_spec.rb +++ b/engines/dfc_provider/spec/requests/enterprises_spec.rb @@ -59,6 +59,12 @@ describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: expect(json_response["@graph"][0]).to include( "dfc-b:affiliates" => "http://test.host/api/dfc/enterprise_groups/60000", ) + + # Insert static value to keep documentation deterministic: + response.body.gsub!( + %r{active_storage/[0-9A-Za-z/=-]*/logo-white.png}, + "active_storage/url/logo-white.png", + ) end end end diff --git a/engines/dfc_provider/spec/requests/supplied_products_spec.rb b/engines/dfc_provider/spec/requests/supplied_products_spec.rb index 47e7d7440f..3e59aa2bad 100644 --- a/engines/dfc_provider/spec/requests/supplied_products_spec.rb +++ b/engines/dfc_provider/spec/requests/supplied_products_spec.rb @@ -117,6 +117,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto expect(second_variant.unit_value).to eq 6 # Insert static value to keep documentation deterministic: + supplied_product[:'ofn:spree_product_id'] = 90_000 response.body.gsub!( "supplied_products/#{variant_id}", "supplied_products/10001" @@ -146,6 +147,12 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto expect(response.body).to include variant.name expect(json_response["ofn:spree_product_id"]).to eq 90_000 expect(json_response["ofn:image"]).to include("logo-white.png") + + # Insert static value to keep documentation deterministic: + response.body.gsub!( + %r{active_storage/[0-9A-Za-z/=-]*/logo-white.png}, + "active_storage/url/logo-white.png", + ) end end diff --git a/swagger/dfc.yaml b/swagger/dfc.yaml index 06c89d015b..aa75b40b89 100644 --- a/swagger/dfc.yaml +++ b/swagger/dfc.yaml @@ -343,7 +343,7 @@ paths: dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 ofn:spree_product_id: 90000 - ofn:image: http://test.host/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZzBCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cec5b9e04720cada9134f6757e3dd8156aeccbf0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJY0c1bkJqb0dSVlE2RkhKbGMybDZaVjkwYjE5c2FXMXBkRnNIYVFId2FRSHciLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--a8632e6a563a139dfae774530b275266f6aef5f5/logo-white.png + ofn:image: http://test.host/rails/active_storage/url/logo-white.png - "@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 @@ -437,7 +437,7 @@ paths: dfc-b:lifetime: '' dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 - ofn:spree_product_id: 33774 + ofn:spree_product_id: 90000 "/api/dfc/enterprises/{enterprise_id}/supplied_products/{id}": parameters: - name: enterprise_id @@ -477,7 +477,7 @@ paths: dfc-b:usageOrStorageCondition: '' dfc-b:totalTheoreticalStock: 0.0 ofn:spree_product_id: 90000 - ofn:image: http://test.host/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBaElCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f578e58da95a3e1f6a4774dfeef31ac7cb039f0f/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJY0c1bkJqb0dSVlE2RkhKbGMybDZaVjkwYjE5c2FXMXBkRnNIYVFId2FRSHciLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--a8632e6a563a139dfae774530b275266f6aef5f5/logo-white.png + ofn:image: http://test.host/rails/active_storage/url/logo-white.png '404': description: not found put: