Skip to content

Commit

Permalink
Merge pull request #11756 from rioug/11712_DFC_add_image_to_product
Browse files Browse the repository at this point in the history
[DFC API] add image to product
  • Loading branch information
mkllnk authored Nov 8, 2023
2 parents cbc2ef5 + d0dd6e5 commit 4e52fc2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.supplied_product(variant)
productType: product_type,
quantity: QuantitativeValueBuilder.quantity(variant),
spree_product_id: variant.product.id,
image_url: variant.product&.image&.url(:product)
)
end

Expand Down
9 changes: 7 additions & 2 deletions engines/dfc_provider/lib/dfc_provider/supplied_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

module DfcProvider
class SuppliedProduct < DataFoodConsortium::Connector::SuppliedProduct
attr_accessor :spree_product_id
attr_accessor :spree_product_id, :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
image
end
end
end
end
8 changes: 7 additions & 1 deletion engines/dfc_provider/spec/requests/enterprises_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
let!(:product) {
create(
:base_product,
:product_with_image,
id: 90_000, supplier: enterprise, name: "Apple", description: "Round",
variants: [variant],
)
Expand Down Expand Up @@ -59,6 +59,12 @@
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
Expand Down
13 changes: 10 additions & 3 deletions engines/dfc_provider/spec/requests/supplied_products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -118,6 +117,7 @@
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"
Expand Down Expand Up @@ -146,6 +146,13 @@
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")

# 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -45,5 +47,16 @@

expect(product.productType).to eq vegetable
end

it "assigns an image_url type" do
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
9 changes: 6 additions & 3 deletions swagger/dfc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/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
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
Expand Down Expand Up @@ -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: ''
Expand All @@ -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: 90000
"/api/dfc/enterprises/{enterprise_id}/supplied_products/{id}":
parameters:
- name: enterprise_id
Expand Down Expand Up @@ -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/url/logo-white.png
'404':
description: not found
put:
Expand Down

0 comments on commit 4e52fc2

Please sign in to comment.