mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-21 05:09:15 +00:00
Map DFC SuppliedProduct with OFN Variant
This commit is contained in:
@@ -9,13 +9,17 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def show
|
||||
@variant =
|
||||
render json: variant, serializer: DfcProvider::CatalogItemSerializer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def variant
|
||||
@variant ||=
|
||||
Spree::Variant.
|
||||
joins(product: :supplier).
|
||||
where('enterprises.id' => @enterprise.id).
|
||||
find(params[:id])
|
||||
|
||||
render json: @variant, serializer: DfcProvider::CatalogItemSerializer
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,9 +5,17 @@ module DfcProvider
|
||||
module Api
|
||||
class SuppliedProductsController < BaseController
|
||||
def show
|
||||
@product = @enterprise.supplied_products.find(params[:id])
|
||||
render json: variant, serializer: DfcProvider::SuppliedProductSerializer
|
||||
end
|
||||
|
||||
render json: @product, serializer: DfcProvider::SuppliedProductSerializer
|
||||
private
|
||||
|
||||
def variant
|
||||
@variant ||=
|
||||
Spree::Variant.
|
||||
joins(product: :supplier).
|
||||
where('enterprises.id' => @enterprise.id).
|
||||
find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,9 +33,9 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def supplies
|
||||
object.
|
||||
supplied_products.
|
||||
includes(variants: :product)
|
||||
Spree::Variant.
|
||||
joins(product: :supplier).
|
||||
where('enterprises.id' => object.id)
|
||||
end
|
||||
|
||||
def manages
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Serializer used to render a DFC SuppliedProduct from an OFN Product
|
||||
# Serializer used to render a DFC SuppliedProduct from an OFN Variant
|
||||
# into JSON-LD format based on DFC ontology
|
||||
module DfcProvider
|
||||
class SuppliedProductSerializer < ActiveModel::Serializer
|
||||
@@ -18,7 +18,7 @@ module DfcProvider
|
||||
|
||||
def id
|
||||
dfc_provider_routes.api_dfc_provider_enterprise_supplied_product_url(
|
||||
enterprise_id: object.supplier_id,
|
||||
enterprise_id: object.product.supplier_id,
|
||||
id: object.id,
|
||||
host: root_url
|
||||
)
|
||||
|
||||
@@ -8,6 +8,7 @@ describe DfcProvider::Api::CatalogItemsController, type: :controller do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:enterprise) { create(:distributor_enterprise, owner: user) }
|
||||
let!(:product) { create(:simple_product, supplier: enterprise ) }
|
||||
let!(:variant) { product.variants.first }
|
||||
|
||||
describe('.index') do
|
||||
context 'with authorization token' do
|
||||
@@ -33,11 +34,11 @@ describe DfcProvider::Api::CatalogItemsController, type: :controller do
|
||||
|
||||
it 'renders the required content' do
|
||||
expect(response.body)
|
||||
.to include(product.name)
|
||||
.to include(variant.name)
|
||||
expect(response.body)
|
||||
.to include(product.sku)
|
||||
.to include(variant.sku)
|
||||
expect(response.body)
|
||||
.to include("offers/#{product.variants.first.id}")
|
||||
.to include("offers/#{variant.id}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,11 +61,11 @@ describe DfcProvider::Api::CatalogItemsController, type: :controller do
|
||||
|
||||
it 'renders the required content' do
|
||||
expect(response.body)
|
||||
.to include(product.name)
|
||||
.to include(variant.name)
|
||||
expect(response.body)
|
||||
.to include(product.sku)
|
||||
.to include(variant.sku)
|
||||
expect(response.body)
|
||||
.to include("offers/#{product.variants.first.id}")
|
||||
.to include("offers/#{variant.id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -117,7 +118,7 @@ describe DfcProvider::Api::CatalogItemsController, type: :controller do
|
||||
before do
|
||||
api_get :show,
|
||||
enterprise_id: enterprise.id,
|
||||
id: product.variants.first.id
|
||||
id: variant.id
|
||||
end
|
||||
|
||||
it 'is successful' do
|
||||
@@ -128,7 +129,7 @@ describe DfcProvider::Api::CatalogItemsController, type: :controller do
|
||||
expect(response.body)
|
||||
.to include('dfc:CatalogItem')
|
||||
expect(response.body)
|
||||
.to include("offers/#{product.variants.first.id}")
|
||||
.to include("offers/#{variant.id}")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ describe DfcProvider::Api::SuppliedProductsController, type: :controller do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:enterprise) { create(:distributor_enterprise, owner: user) }
|
||||
let!(:product) { create(:simple_product, supplier: enterprise ) }
|
||||
let!(:variant) { product.variants.first }
|
||||
|
||||
describe('.show') do
|
||||
context 'with authorization token' do
|
||||
@@ -27,7 +28,7 @@ describe DfcProvider::Api::SuppliedProductsController, type: :controller do
|
||||
before do
|
||||
api_get :show,
|
||||
enterprise_id: 'default',
|
||||
id: product.id
|
||||
id: variant.id
|
||||
end
|
||||
|
||||
it 'is successful' do
|
||||
@@ -36,7 +37,7 @@ describe DfcProvider::Api::SuppliedProductsController, type: :controller do
|
||||
|
||||
it 'renders the required content' do
|
||||
expect(response.body)
|
||||
.to include(product.name)
|
||||
.to include(variant.name)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user