Update price and stock through the DFC API

This commit is contained in:
Maikel Linke
2024-01-11 16:34:10 +11:00
parent 8d6ae18fb6
commit af51177593
7 changed files with 70 additions and 5 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View 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

View File

@@ -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

View File

@@ -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

View File

@@ -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