mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
Update price and stock through the DFC API
This commit is contained in:
@@ -59,5 +59,9 @@ module DfcProvider
|
||||
def current_ability
|
||||
@current_ability ||= Spree::Ability.new(current_user)
|
||||
end
|
||||
|
||||
def import
|
||||
DfcIo.import(request.body)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,16 @@ module DfcProvider
|
||||
render json: DfcIo.export(subject)
|
||||
end
|
||||
|
||||
def update
|
||||
offer = import
|
||||
|
||||
return head :bad_request unless offer
|
||||
|
||||
OfferBuilder.apply(offer, variant)
|
||||
|
||||
variant.save!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def variant
|
||||
|
||||
@@ -48,10 +48,6 @@ module DfcProvider
|
||||
|
||||
private
|
||||
|
||||
def import
|
||||
DfcIo.import(request.body)
|
||||
end
|
||||
|
||||
def variant
|
||||
@variant ||= current_enterprise.supplied_variants.find(params[:id])
|
||||
end
|
||||
|
||||
8
engines/dfc_provider/app/services/offer_builder.rb
Normal file
8
engines/dfc_provider/app/services/offer_builder.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class OfferBuilder < DfcBuilder
|
||||
def self.apply(offer, variant)
|
||||
variant.on_hand = offer.stockLimitation
|
||||
variant.price = offer.price
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@ DfcProvider::Engine.routes.draw do
|
||||
resources :addresses, only: [:show]
|
||||
resources :enterprises, only: [:show] do
|
||||
resources :catalog_items, only: [:index, :show, :update]
|
||||
resources :offers, only: [:show]
|
||||
resources :offers, only: [:show, :update]
|
||||
resources :supplied_products, only: [:create, :show, :update]
|
||||
resources :social_medias, only: [:show]
|
||||
end
|
||||
|
||||
@@ -32,5 +32,34 @@ describe "Offers", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: true
|
||||
run_test!
|
||||
end
|
||||
end
|
||||
|
||||
put "Update Offer" do
|
||||
consumes "application/json"
|
||||
|
||||
parameter name: :offer, in: :body, schema: {
|
||||
example: {
|
||||
'@context': "https://www.datafoodconsortium.org",
|
||||
'@id': "http://test.host/api/dfc/enterprises/10000/offers/10001",
|
||||
'@type': "dfc-b:Offer",
|
||||
'dfc-b:hasPrice': 9.99,
|
||||
'dfc-b:stockLimitation': 7
|
||||
}
|
||||
}
|
||||
|
||||
let(:id) { variant.id }
|
||||
let(:offer) { |example|
|
||||
example.metadata[:operation][:parameters].first[:schema][:example]
|
||||
}
|
||||
|
||||
response "204", "success" do
|
||||
it "updates a variant" do |example|
|
||||
expect {
|
||||
submit_request(example.metadata)
|
||||
variant.reload
|
||||
}.to change { variant.price }.to(9.99)
|
||||
.and change { variant.on_hand }.to(7)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -438,6 +438,24 @@ paths:
|
||||
"@type": dfc-b:Offer
|
||||
dfc-b:hasPrice: 19.99
|
||||
dfc-b:stockLimitation: 5
|
||||
put:
|
||||
summary: Update Offer
|
||||
parameters: []
|
||||
tags:
|
||||
- Offers
|
||||
responses:
|
||||
'204':
|
||||
description: success
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
example:
|
||||
"@context": https://www.datafoodconsortium.org
|
||||
"@id": http://test.host/api/dfc/enterprises/10000/offers/10001
|
||||
"@type": dfc-b:Offer
|
||||
dfc-b:hasPrice: 9.99
|
||||
dfc-b:stockLimitation: 7
|
||||
"/api/dfc/persons/{id}":
|
||||
get:
|
||||
summary: Show person
|
||||
|
||||
Reference in New Issue
Block a user