goca-spree-api 3.1.14.rails.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE +26 -0
- data/Rakefile +16 -0
- data/app/controllers/spree/api/base_controller.rb +167 -0
- data/app/controllers/spree/api/v1/addresses_controller.rb +46 -0
- data/app/controllers/spree/api/v1/checkouts_controller.rb +103 -0
- data/app/controllers/spree/api/v1/classifications_controller.rb +20 -0
- data/app/controllers/spree/api/v1/countries_controller.rb +24 -0
- data/app/controllers/spree/api/v1/credit_cards_controller.rb +27 -0
- data/app/controllers/spree/api/v1/images_controller.rb +56 -0
- data/app/controllers/spree/api/v1/inventory_units_controller.rb +54 -0
- data/app/controllers/spree/api/v1/line_items_controller.rb +70 -0
- data/app/controllers/spree/api/v1/option_types_controller.rb +51 -0
- data/app/controllers/spree/api/v1/option_values_controller.rb +60 -0
- data/app/controllers/spree/api/v1/orders_controller.rb +148 -0
- data/app/controllers/spree/api/v1/payments_controller.rb +83 -0
- data/app/controllers/spree/api/v1/product_properties_controller.rb +70 -0
- data/app/controllers/spree/api/v1/products_controller.rb +126 -0
- data/app/controllers/spree/api/v1/promotions_controller.rb +28 -0
- data/app/controllers/spree/api/v1/properties_controller.rb +72 -0
- data/app/controllers/spree/api/v1/return_authorizations_controller.rb +71 -0
- data/app/controllers/spree/api/v1/shipments_controller.rb +163 -0
- data/app/controllers/spree/api/v1/states_controller.rb +37 -0
- data/app/controllers/spree/api/v1/stock_items_controller.rb +77 -0
- data/app/controllers/spree/api/v1/stock_locations_controller.rb +52 -0
- data/app/controllers/spree/api/v1/stock_movements_controller.rb +45 -0
- data/app/controllers/spree/api/v1/stores_controller.rb +57 -0
- data/app/controllers/spree/api/v1/taxonomies_controller.rb +66 -0
- data/app/controllers/spree/api/v1/taxons_controller.rb +95 -0
- data/app/controllers/spree/api/v1/users_controller.rb +60 -0
- data/app/controllers/spree/api/v1/variants_controller.rb +75 -0
- data/app/controllers/spree/api/v1/zones_controller.rb +51 -0
- data/app/helpers/spree/api/api_helpers.rb +173 -0
- data/app/models/spree/api_configuration.rb +5 -0
- data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
- data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
- data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
- data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
- data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
- data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
- data/app/views/spree/api/v1/addresses/show.v1.rabl +10 -0
- data/app/views/spree/api/v1/adjustments/show.v1.rabl +4 -0
- data/app/views/spree/api/v1/config/money.v1.rabl +2 -0
- data/app/views/spree/api/v1/config/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/countries/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/countries/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/credit_cards/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/credit_cards/show.v1.rabl +3 -0
- data/app/views/spree/api/v1/images/index.v1.rabl +4 -0
- data/app/views/spree/api/v1/images/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/images/show.v1.rabl +6 -0
- data/app/views/spree/api/v1/inventory_units/show.rabl +2 -0
- data/app/views/spree/api/v1/line_items/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/line_items/show.v1.rabl +15 -0
- data/app/views/spree/api/v1/option_types/index.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_types/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_types/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/option_values/index.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_values/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/option_values/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/orders/address.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/canceled.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/cart.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/complete.v1.rabl +0 -0
- data/app/views/spree/api/v1/orders/could_not_apply_coupon.v1.rabl +2 -0
- data/app/views/spree/api/v1/orders/could_not_transition.v1.rabl +3 -0
- data/app/views/spree/api/v1/orders/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/orders/invalid_shipping_method.v1.rabl +2 -0
- data/app/views/spree/api/v1/orders/mine.v1.rabl +9 -0
- data/app/views/spree/api/v1/orders/order.v1.rabl +10 -0
- data/app/views/spree/api/v1/orders/payment.v1.rabl +3 -0
- data/app/views/spree/api/v1/orders/show.v1.rabl +51 -0
- data/app/views/spree/api/v1/payments/credit_over_limit.v1.rabl +2 -0
- data/app/views/spree/api/v1/payments/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/payments/new.v1.rabl +5 -0
- data/app/views/spree/api/v1/payments/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/payments/update_forbidden.v1.rabl +2 -0
- data/app/views/spree/api/v1/product_properties/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/product_properties/new.v1.rabl +2 -0
- data/app/views/spree/api/v1/product_properties/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/products/index.v1.rabl +9 -0
- data/app/views/spree/api/v1/products/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/products/product.v1.rabl +1 -0
- data/app/views/spree/api/v1/products/show.v1.rabl +32 -0
- data/app/views/spree/api/v1/promotions/handler.v1.rabl +5 -0
- data/app/views/spree/api/v1/promotions/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/properties/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/properties/new.v1.rabl +2 -0
- data/app/views/spree/api/v1/properties/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/return_authorizations/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/return_authorizations/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/return_authorizations/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/shared/stock_location_required.v1.rabl +2 -0
- data/app/views/spree/api/v1/shipments/big.v1.rabl +48 -0
- data/app/views/spree/api/v1/shipments/cannot_ready_shipment.v1.rabl +2 -0
- data/app/views/spree/api/v1/shipments/mine.v1.rabl +9 -0
- data/app/views/spree/api/v1/shipments/show.v1.rabl +32 -0
- data/app/views/spree/api/v1/shipments/small.v1.rabl +37 -0
- data/app/views/spree/api/v1/shipping_rates/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/states/index.v1.rabl +14 -0
- data/app/views/spree/api/v1/states/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/stock_items/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/stock_items/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/stock_locations/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/stock_locations/show.v1.rabl +8 -0
- data/app/views/spree/api/v1/stock_movements/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/stock_movements/show.v1.rabl +5 -0
- data/app/views/spree/api/v1/stores/index.v1.rabl +4 -0
- data/app/views/spree/api/v1/stores/show.v1.rabl +2 -0
- data/app/views/spree/api/v1/taxonomies/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/taxonomies/jstree.rabl +8 -0
- data/app/views/spree/api/v1/taxonomies/nested.v1.rabl +11 -0
- data/app/views/spree/api/v1/taxonomies/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/taxonomies/show.v1.rabl +15 -0
- data/app/views/spree/api/v1/taxons/index.v1.rabl +12 -0
- data/app/views/spree/api/v1/taxons/jstree.rabl +8 -0
- data/app/views/spree/api/v1/taxons/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/taxons/show.v1.rabl +6 -0
- data/app/views/spree/api/v1/taxons/taxons.v1.rabl +5 -0
- data/app/views/spree/api/v1/users/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/users/new.v1.rabl +3 -0
- data/app/views/spree/api/v1/users/show.v1.rabl +10 -0
- data/app/views/spree/api/v1/variants/big.v1.rabl +14 -0
- data/app/views/spree/api/v1/variants/index.v1.rabl +9 -0
- data/app/views/spree/api/v1/variants/new.v1.rabl +2 -0
- data/app/views/spree/api/v1/variants/show.v1.rabl +3 -0
- data/app/views/spree/api/v1/variants/small.v1.rabl +17 -0
- data/app/views/spree/api/v1/zones/index.v1.rabl +7 -0
- data/app/views/spree/api/v1/zones/show.v1.rabl +6 -0
- data/config/initializers/metal_load_paths.rb +1 -0
- data/config/locales/en.yml +27 -0
- data/config/routes.rb +128 -0
- data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
- data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
- data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
- data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +7 -0
- data/lib/spree/api/controller_setup.rb +20 -0
- data/lib/spree/api/engine.rb +35 -0
- data/lib/spree/api/responders/rabl_template.rb +30 -0
- data/lib/spree/api/responders.rb +11 -0
- data/lib/spree/api/testing_support/caching.rb +10 -0
- data/lib/spree/api/testing_support/helpers.rb +44 -0
- data/lib/spree/api/testing_support/setup.rb +16 -0
- data/lib/spree/api.rb +10 -0
- data/lib/spree_api.rb +2 -0
- data/script/rails +9 -0
- data/spec/fixtures/thinking-cat.jpg +0 -0
- data/spree_api.gemspec +20 -0
- metadata +221 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class ReturnAuthorizationsController < Spree::Api::BaseController
|
5
|
+
|
6
|
+
def create
|
7
|
+
authorize! :create, ReturnAuthorization
|
8
|
+
@return_authorization = order.return_authorizations.build(return_authorization_params)
|
9
|
+
if @return_authorization.save
|
10
|
+
respond_with(@return_authorization, status: 201, default_template: :show)
|
11
|
+
else
|
12
|
+
invalid_resource!(@return_authorization)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def destroy
|
17
|
+
@return_authorization = order.return_authorizations.accessible_by(current_ability, :destroy).find(params[:id])
|
18
|
+
@return_authorization.destroy
|
19
|
+
respond_with(@return_authorization, status: 204)
|
20
|
+
end
|
21
|
+
|
22
|
+
def index
|
23
|
+
authorize! :admin, ReturnAuthorization
|
24
|
+
@return_authorizations = order.return_authorizations.accessible_by(current_ability, :read).
|
25
|
+
ransack(params[:q]).result.
|
26
|
+
page(params[:page]).per(params[:per_page])
|
27
|
+
respond_with(@return_authorizations)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new
|
31
|
+
authorize! :admin, ReturnAuthorization
|
32
|
+
end
|
33
|
+
|
34
|
+
def show
|
35
|
+
authorize! :admin, ReturnAuthorization
|
36
|
+
@return_authorization = order.return_authorizations.accessible_by(current_ability, :read).find(params[:id])
|
37
|
+
respond_with(@return_authorization)
|
38
|
+
end
|
39
|
+
|
40
|
+
def update
|
41
|
+
@return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
|
42
|
+
if @return_authorization.update_attributes(return_authorization_params)
|
43
|
+
respond_with(@return_authorization, default_template: :show)
|
44
|
+
else
|
45
|
+
invalid_resource!(@return_authorization)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def cancel
|
50
|
+
@return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
|
51
|
+
if @return_authorization.cancel
|
52
|
+
respond_with @return_authorization, default_template: :show
|
53
|
+
else
|
54
|
+
invalid_resource!(@return_authorization)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def order
|
61
|
+
@order ||= Spree::Order.find_by!(number: order_id)
|
62
|
+
authorize! :read, @order
|
63
|
+
end
|
64
|
+
|
65
|
+
def return_authorization_params
|
66
|
+
params.require(:return_authorization).permit(permitted_return_authorization_attributes)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class ShipmentsController < Spree::Api::BaseController
|
5
|
+
|
6
|
+
before_action :find_and_update_shipment, only: [:ship, :ready, :add, :remove]
|
7
|
+
before_action :load_transfer_params, only: [:transfer_to_location, :transfer_to_shipment]
|
8
|
+
|
9
|
+
def mine
|
10
|
+
if current_api_user.persisted?
|
11
|
+
@shipments = Spree::Shipment
|
12
|
+
.reverse_chronological
|
13
|
+
.joins(:order)
|
14
|
+
.where(spree_orders: {user_id: current_api_user.id})
|
15
|
+
.includes(mine_includes)
|
16
|
+
.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
|
17
|
+
else
|
18
|
+
render "spree/api/errors/unauthorized", status: :unauthorized
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def create
|
23
|
+
@order = Spree::Order.find_by!(number: params.fetch(:shipment).fetch(:order_id))
|
24
|
+
authorize! :read, @order
|
25
|
+
authorize! :create, Shipment
|
26
|
+
quantity = params[:quantity].to_i
|
27
|
+
@shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))
|
28
|
+
@order.contents.add(variant, quantity, {shipment: @shipment})
|
29
|
+
|
30
|
+
@shipment.save!
|
31
|
+
|
32
|
+
respond_with(@shipment.reload, default_template: :show)
|
33
|
+
end
|
34
|
+
|
35
|
+
def update
|
36
|
+
@shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).friendly.find(params[:id])
|
37
|
+
@shipment.update_attributes_and_order(shipment_params)
|
38
|
+
|
39
|
+
respond_with(@shipment.reload, default_template: :show)
|
40
|
+
end
|
41
|
+
|
42
|
+
def ready
|
43
|
+
unless @shipment.ready?
|
44
|
+
if @shipment.can_ready?
|
45
|
+
@shipment.ready!
|
46
|
+
else
|
47
|
+
render 'spree/api/v1/shipments/cannot_ready_shipment', status: 422 and return
|
48
|
+
end
|
49
|
+
end
|
50
|
+
respond_with(@shipment, default_template: :show)
|
51
|
+
end
|
52
|
+
|
53
|
+
def ship
|
54
|
+
unless @shipment.shipped?
|
55
|
+
@shipment.ship!
|
56
|
+
end
|
57
|
+
respond_with(@shipment, default_template: :show)
|
58
|
+
end
|
59
|
+
|
60
|
+
def add
|
61
|
+
quantity = params[:quantity].to_i
|
62
|
+
|
63
|
+
@shipment.order.contents.add(variant, quantity, {shipment: @shipment})
|
64
|
+
|
65
|
+
respond_with(@shipment, default_template: :show)
|
66
|
+
end
|
67
|
+
|
68
|
+
def remove
|
69
|
+
quantity = params[:quantity].to_i
|
70
|
+
|
71
|
+
@shipment.order.contents.remove(variant, quantity, {shipment: @shipment})
|
72
|
+
@shipment.reload if @shipment.persisted?
|
73
|
+
respond_with(@shipment, default_template: :show)
|
74
|
+
end
|
75
|
+
|
76
|
+
def transfer_to_location
|
77
|
+
@stock_location = Spree::StockLocation.find(params[:stock_location_id])
|
78
|
+
|
79
|
+
unless @quantity > 0
|
80
|
+
unprocessable_entity('ArgumentError')
|
81
|
+
return
|
82
|
+
end
|
83
|
+
|
84
|
+
@original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
|
85
|
+
render json: {success: true, message: Spree.t(:shipment_transfer_success)}, status: 201
|
86
|
+
end
|
87
|
+
|
88
|
+
def transfer_to_shipment
|
89
|
+
@target_shipment = Spree::Shipment.friendly.find(params[:target_shipment_number])
|
90
|
+
|
91
|
+
if @quantity < 0 || @target_shipment == @original_shipment
|
92
|
+
unprocessable_entity('ArgumentError')
|
93
|
+
return
|
94
|
+
end
|
95
|
+
|
96
|
+
@original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
|
97
|
+
render json: {success: true, message: Spree.t(:shipment_transfer_success)}, status: 201
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def load_transfer_params
|
103
|
+
@original_shipment = Spree::Shipment.friendly.find(params[:original_shipment_number])
|
104
|
+
@variant = Spree::Variant.find(params[:variant_id])
|
105
|
+
@quantity = params[:quantity].to_i
|
106
|
+
authorize! :read, @original_shipment
|
107
|
+
authorize! :create, Shipment
|
108
|
+
end
|
109
|
+
|
110
|
+
def find_and_update_shipment
|
111
|
+
@shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).friendly.find(params[:id])
|
112
|
+
@shipment.update_attributes(shipment_params)
|
113
|
+
@shipment.reload
|
114
|
+
end
|
115
|
+
|
116
|
+
def shipment_params
|
117
|
+
if params[:shipment] && !params[:shipment].empty?
|
118
|
+
params.require(:shipment).permit(permitted_shipment_attributes)
|
119
|
+
else
|
120
|
+
{}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def variant
|
125
|
+
@variant ||= Spree::Variant.unscoped.find(params.fetch(:variant_id))
|
126
|
+
end
|
127
|
+
|
128
|
+
def mine_includes
|
129
|
+
{
|
130
|
+
order: {
|
131
|
+
bill_address: {
|
132
|
+
state: {},
|
133
|
+
country: {},
|
134
|
+
},
|
135
|
+
ship_address: {
|
136
|
+
state: {},
|
137
|
+
country: {},
|
138
|
+
},
|
139
|
+
adjustments: {},
|
140
|
+
payments: {
|
141
|
+
order: {},
|
142
|
+
payment_method: {},
|
143
|
+
},
|
144
|
+
},
|
145
|
+
inventory_units: {
|
146
|
+
line_item: {
|
147
|
+
product: {},
|
148
|
+
variant: {},
|
149
|
+
},
|
150
|
+
variant: {
|
151
|
+
product: {},
|
152
|
+
default_price: {},
|
153
|
+
option_values: {
|
154
|
+
option_type: {},
|
155
|
+
},
|
156
|
+
},
|
157
|
+
},
|
158
|
+
}
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class StatesController < Spree::Api::BaseController
|
5
|
+
skip_before_action :authenticate_user
|
6
|
+
|
7
|
+
def index
|
8
|
+
@states = scope.ransack(params[:q]).result.includes(:country)
|
9
|
+
|
10
|
+
if params[:page] || params[:per_page]
|
11
|
+
@states = @states.page(params[:page]).per(params[:per_page])
|
12
|
+
end
|
13
|
+
|
14
|
+
state = @states.last
|
15
|
+
if stale?(state)
|
16
|
+
respond_with(@states)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def show
|
21
|
+
@state = scope.find(params[:id])
|
22
|
+
respond_with(@state)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def scope
|
27
|
+
if params[:country_id]
|
28
|
+
@country = Country.accessible_by(current_ability, :read).find(params[:country_id])
|
29
|
+
return @country.states.accessible_by(current_ability, :read).order('name ASC')
|
30
|
+
else
|
31
|
+
return State.accessible_by(current_ability, :read).order('name ASC')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class StockItemsController < Spree::Api::BaseController
|
5
|
+
before_action :stock_location, except: [:update, :destroy]
|
6
|
+
|
7
|
+
def index
|
8
|
+
@stock_items = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
|
9
|
+
respond_with(@stock_items)
|
10
|
+
end
|
11
|
+
|
12
|
+
def show
|
13
|
+
@stock_item = scope.find(params[:id])
|
14
|
+
respond_with(@stock_item)
|
15
|
+
end
|
16
|
+
|
17
|
+
def create
|
18
|
+
authorize! :create, StockItem
|
19
|
+
|
20
|
+
count_on_hand = 0
|
21
|
+
if params[:stock_item].has_key?(:count_on_hand)
|
22
|
+
count_on_hand = params[:stock_item][:count_on_hand].to_i
|
23
|
+
end
|
24
|
+
|
25
|
+
@stock_item = scope.new(stock_item_params)
|
26
|
+
if @stock_item.save
|
27
|
+
@stock_item.adjust_count_on_hand(count_on_hand)
|
28
|
+
respond_with(@stock_item, status: 201, default_template: :show)
|
29
|
+
else
|
30
|
+
invalid_resource!(@stock_item)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def update
|
35
|
+
@stock_item = StockItem.accessible_by(current_ability, :update).find(params[:id])
|
36
|
+
|
37
|
+
count_on_hand = 0
|
38
|
+
if params[:stock_item].has_key?(:count_on_hand)
|
39
|
+
count_on_hand = params[:stock_item][:count_on_hand].to_i
|
40
|
+
params[:stock_item].delete(:count_on_hand)
|
41
|
+
end
|
42
|
+
|
43
|
+
updated = params[:stock_item][:force] ? @stock_item.set_count_on_hand(count_on_hand)
|
44
|
+
: @stock_item.adjust_count_on_hand(count_on_hand)
|
45
|
+
|
46
|
+
if updated
|
47
|
+
respond_with(@stock_item, status: 200, default_template: :show)
|
48
|
+
else
|
49
|
+
invalid_resource!(@stock_item)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def destroy
|
54
|
+
@stock_item = StockItem.accessible_by(current_ability, :destroy).find(params[:id])
|
55
|
+
@stock_item.destroy
|
56
|
+
respond_with(@stock_item, status: 204)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def stock_location
|
62
|
+
render 'spree/api/v1/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
|
63
|
+
@stock_location ||= StockLocation.accessible_by(current_ability, :read).find(params[:stock_location_id])
|
64
|
+
end
|
65
|
+
|
66
|
+
def scope
|
67
|
+
includes = {:variant => [{ :option_values => :option_type }, :product] }
|
68
|
+
@stock_location.stock_items.accessible_by(current_ability, :read).includes(includes)
|
69
|
+
end
|
70
|
+
|
71
|
+
def stock_item_params
|
72
|
+
params.require(:stock_item).permit(permitted_stock_item_attributes)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class StockLocationsController < Spree::Api::BaseController
|
5
|
+
def index
|
6
|
+
authorize! :read, StockLocation
|
7
|
+
@stock_locations = StockLocation.accessible_by(current_ability, :read).order('name ASC').ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
|
8
|
+
respond_with(@stock_locations)
|
9
|
+
end
|
10
|
+
|
11
|
+
def show
|
12
|
+
respond_with(stock_location)
|
13
|
+
end
|
14
|
+
|
15
|
+
def create
|
16
|
+
authorize! :create, StockLocation
|
17
|
+
@stock_location = StockLocation.new(stock_location_params)
|
18
|
+
if @stock_location.save
|
19
|
+
respond_with(@stock_location, status: 201, default_template: :show)
|
20
|
+
else
|
21
|
+
invalid_resource!(@stock_location)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def update
|
26
|
+
authorize! :update, stock_location
|
27
|
+
if stock_location.update_attributes(stock_location_params)
|
28
|
+
respond_with(stock_location, status: 200, default_template: :show)
|
29
|
+
else
|
30
|
+
invalid_resource!(stock_location)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy
|
35
|
+
authorize! :destroy, stock_location
|
36
|
+
stock_location.destroy
|
37
|
+
respond_with(stock_location, :status => 204)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def stock_location
|
43
|
+
@stock_location ||= StockLocation.accessible_by(current_ability, :read).find(params[:id])
|
44
|
+
end
|
45
|
+
|
46
|
+
def stock_location_params
|
47
|
+
params.require(:stock_location).permit(permitted_stock_location_attributes)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class StockMovementsController < Spree::Api::BaseController
|
5
|
+
before_action :stock_location, except: [:update, :destroy]
|
6
|
+
|
7
|
+
def index
|
8
|
+
authorize! :read, StockMovement
|
9
|
+
@stock_movements = scope.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
|
10
|
+
respond_with(@stock_movements)
|
11
|
+
end
|
12
|
+
|
13
|
+
def show
|
14
|
+
@stock_movement = scope.find(params[:id])
|
15
|
+
respond_with(@stock_movement)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create
|
19
|
+
authorize! :create, StockMovement
|
20
|
+
@stock_movement = scope.new(stock_movement_params)
|
21
|
+
if @stock_movement.save
|
22
|
+
respond_with(@stock_movement, status: 201, default_template: :show)
|
23
|
+
else
|
24
|
+
invalid_resource!(@stock_movement)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def stock_location
|
31
|
+
render 'spree/api/v1/shared/stock_location_required', status: 422 and return unless params[:stock_location_id]
|
32
|
+
@stock_location ||= StockLocation.accessible_by(current_ability, :read).find(params[:stock_location_id])
|
33
|
+
end
|
34
|
+
|
35
|
+
def scope
|
36
|
+
@stock_location.stock_movements.accessible_by(current_ability, :read)
|
37
|
+
end
|
38
|
+
|
39
|
+
def stock_movement_params
|
40
|
+
params.require(:stock_movement).permit(permitted_stock_movement_attributes)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class StoresController < Spree::Api::BaseController
|
5
|
+
|
6
|
+
before_action :get_store, except: [:index, :create]
|
7
|
+
|
8
|
+
def index
|
9
|
+
authorize! :read, Store
|
10
|
+
@stores = Store.accessible_by(current_ability, :read).all
|
11
|
+
respond_with(@stores)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
authorize! :create, Store
|
16
|
+
@store = Store.new(store_params)
|
17
|
+
@store.code = params[:store][:code]
|
18
|
+
if @store.save
|
19
|
+
respond_with(@store, status: 201, default_template: :show)
|
20
|
+
else
|
21
|
+
invalid_resource!(@store)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def update
|
26
|
+
authorize! :update, @store
|
27
|
+
if @store.update_attributes(store_params)
|
28
|
+
respond_with(@store, status: 200, default_template: :show)
|
29
|
+
else
|
30
|
+
invalid_resource!(@store)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def show
|
35
|
+
authorize! :read, @store
|
36
|
+
respond_with(@store)
|
37
|
+
end
|
38
|
+
|
39
|
+
def destroy
|
40
|
+
authorize! :destroy, @store
|
41
|
+
@store.destroy
|
42
|
+
respond_with(@store, status: 204)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def get_store
|
48
|
+
@store = Store.find(params[:id])
|
49
|
+
end
|
50
|
+
|
51
|
+
def store_params
|
52
|
+
params.require(:store).permit(permitted_store_attributes)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class TaxonomiesController < Spree::Api::BaseController
|
5
|
+
|
6
|
+
def index
|
7
|
+
respond_with(taxonomies)
|
8
|
+
end
|
9
|
+
|
10
|
+
def show
|
11
|
+
respond_with(taxonomy)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Because JSTree wants parameters in a *slightly* different format
|
15
|
+
def jstree
|
16
|
+
show
|
17
|
+
end
|
18
|
+
|
19
|
+
def create
|
20
|
+
authorize! :create, Taxonomy
|
21
|
+
@taxonomy = Taxonomy.new(taxonomy_params)
|
22
|
+
if @taxonomy.save
|
23
|
+
respond_with(@taxonomy, :status => 201, :default_template => :show)
|
24
|
+
else
|
25
|
+
invalid_resource!(@taxonomy)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def update
|
30
|
+
authorize! :update, taxonomy
|
31
|
+
if taxonomy.update_attributes(taxonomy_params)
|
32
|
+
respond_with(taxonomy, :status => 200, :default_template => :show)
|
33
|
+
else
|
34
|
+
invalid_resource!(taxonomy)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroy
|
39
|
+
authorize! :destroy, taxonomy
|
40
|
+
taxonomy.destroy
|
41
|
+
respond_with(taxonomy, :status => 204)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def taxonomies
|
47
|
+
@taxonomies = Taxonomy.accessible_by(current_ability, :read).order('name').includes(:root => :children).
|
48
|
+
ransack(params[:q]).result.
|
49
|
+
page(params[:page]).per(params[:per_page])
|
50
|
+
end
|
51
|
+
|
52
|
+
def taxonomy
|
53
|
+
@taxonomy ||= Taxonomy.accessible_by(current_ability, :read).find(params[:id])
|
54
|
+
end
|
55
|
+
|
56
|
+
def taxonomy_params
|
57
|
+
if params[:taxonomy] && !params[:taxonomy].empty?
|
58
|
+
params.require(:taxonomy).permit(permitted_taxonomy_attributes)
|
59
|
+
else
|
60
|
+
{}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module V1
|
4
|
+
class TaxonsController < Spree::Api::BaseController
|
5
|
+
def index
|
6
|
+
if taxonomy
|
7
|
+
@taxons = taxonomy.root.children
|
8
|
+
else
|
9
|
+
if params[:ids]
|
10
|
+
@taxons = Spree::Taxon.includes(:children).accessible_by(current_ability, :read).where(id: params[:ids].split(','))
|
11
|
+
else
|
12
|
+
@taxons = Spree::Taxon.includes(:children).accessible_by(current_ability, :read).order(:taxonomy_id, :lft).ransack(params[:q]).result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
@taxons = @taxons.page(params[:page]).per(params[:per_page])
|
17
|
+
respond_with(@taxons)
|
18
|
+
end
|
19
|
+
|
20
|
+
def show
|
21
|
+
@taxon = taxon
|
22
|
+
respond_with(@taxon)
|
23
|
+
end
|
24
|
+
|
25
|
+
def jstree
|
26
|
+
show
|
27
|
+
end
|
28
|
+
|
29
|
+
def create
|
30
|
+
authorize! :create, Taxon
|
31
|
+
@taxon = Spree::Taxon.new(taxon_params)
|
32
|
+
@taxon.taxonomy_id = params[:taxonomy_id]
|
33
|
+
taxonomy = Spree::Taxonomy.find_by(id: params[:taxonomy_id])
|
34
|
+
|
35
|
+
if taxonomy.nil?
|
36
|
+
@taxon.errors[:taxonomy_id] = I18n.t(:invalid_taxonomy_id, scope: 'spree.api')
|
37
|
+
invalid_resource!(@taxon) and return
|
38
|
+
end
|
39
|
+
|
40
|
+
@taxon.parent_id = taxonomy.root.id unless params[:taxon][:parent_id]
|
41
|
+
|
42
|
+
if @taxon.save
|
43
|
+
respond_with(@taxon, status: 201, default_template: :show)
|
44
|
+
else
|
45
|
+
invalid_resource!(@taxon)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def update
|
50
|
+
authorize! :update, taxon
|
51
|
+
if taxon.update_attributes(taxon_params)
|
52
|
+
respond_with(taxon, status: 200, default_template: :show)
|
53
|
+
else
|
54
|
+
invalid_resource!(taxon)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def destroy
|
59
|
+
authorize! :destroy, taxon
|
60
|
+
taxon.destroy
|
61
|
+
respond_with(taxon, status: 204)
|
62
|
+
end
|
63
|
+
|
64
|
+
def products
|
65
|
+
# Returns the products sorted by their position with the classification
|
66
|
+
# Products#index does not do the sorting.
|
67
|
+
taxon = Spree::Taxon.find(params[:id])
|
68
|
+
@products = taxon.products.ransack(params[:q]).result
|
69
|
+
@products = @products.page(params[:page]).per(params[:per_page] || 500)
|
70
|
+
render "spree/api/v1/products/index"
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def taxonomy
|
76
|
+
if params[:taxonomy_id].present?
|
77
|
+
@taxonomy ||= Spree::Taxonomy.accessible_by(current_ability, :read).find(params[:taxonomy_id])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def taxon
|
82
|
+
@taxon ||= taxonomy.taxons.accessible_by(current_ability, :read).find(params[:id])
|
83
|
+
end
|
84
|
+
|
85
|
+
def taxon_params
|
86
|
+
if params[:taxon] && !params[:taxon].empty?
|
87
|
+
params.require(:taxon).permit(permitted_taxon_attributes)
|
88
|
+
else
|
89
|
+
{}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|