mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-28 21:07:16 +00:00
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user