mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #11756 from rioug/11712_DFC_add_image_to_product
[DFC API] add image to product
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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],
|
||||
)
|
||||
@@ -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
|
||||
|
||||
@@ -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],
|
||||
@@ -118,6 +117,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml",
|
||||
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 +146,13 @@ 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")
|
||||
|
||||
# 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
|
||||
|
||||
|
||||
@@ -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
|
||||
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
|
||||
|
||||
@@ -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
|
||||
@@ -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: 90000
|
||||
"/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/url/logo-white.png
|
||||
'404':
|
||||
description: not found
|
||||
put:
|
||||
|
||||
Reference in New Issue
Block a user