Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DFC API] add image to product #11756

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On production, this will directly link to AWS S3. And in order to do that for variants, it will process the image first. That shouldn't be a problem because all images should be processed already. Just flagging it here in case there's some performance issue later.

)
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
Comment on lines -350 to +351
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that's due to the different factory used?

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
Loading