cirandas.net

commit bb2047bccdcaa25c4e7f727c5fb20634b92924e4

Author: Braulio Bhavamitra <braulio@prout.io>

stores_app/item: create order on add if needed

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/config/Gemfile b/config/Gemfile
index 891f4b7ca1129e5ff2573d80fb9d2337a431dcf0..64582dbfcc0c2d7ef019888f362d6c1776598193 100644
--- a/config/Gemfile
+++ b/config/Gemfile
@@ -66,7 +66,7 @@   end
 
   group :production do
     gem 'unicorn-worker-killer'
-    gem 'puma_worker_killer'
+    #gem 'puma_worker_killer'
   end
 end
 




diff --git a/plugins/stores_app/app/controllers/profile/stores_app_plugin/api_controller.rb b/plugins/stores_app/app/controllers/profile/stores_app_plugin/api_controller.rb
index f44f0455d044850947a8caff5ed6212c1d2f8071..39b09d30cacca3caafffd8c706477f4f768c5a5c 100644
--- a/plugins/stores_app/app/controllers/profile/stores_app_plugin/api_controller.rb
+++ b/plugins/stores_app/app/controllers/profile/stores_app_plugin/api_controller.rb
@@ -26,5 +26,12 @@     def set_environment
       @environment = Environment.default
     end
 
+    def order
+      @order ||= profile.sales
+        .where(consumer_id: user.person.id)
+        .order('created_at DESC')
+        .first
+    end
+
   end
 end




diff --git a/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb b/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb
index f88c62a43f95e471e2ea723ce39f31385713834e..587ebc2fdca1c9e09ff3c6fac77f2498757d9b02 100644
--- a/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb
+++ b/plugins/stores_app/app/controllers/profile/stores_app_plugin/items_controller.rb
@@ -2,6 +2,8 @@ module StoresAppPlugin
   class ItemsController < ApiController
 
     def add
+      create_order_if_needed
+
       @product = Product.find params[:product_id]
       @item    = order.items.find{ |i| i.product == @product }
       @item  ||= order.items.build product: @product, quantity_consumer_ordered: 0
@@ -14,16 +16,14 @@
     def remove
       @item = order.items.find params[:id]
       @item.destroy
-      render json: {}
+      render json: OrderSerializer.new(order, scope: self).to_hash
     end
 
     protected
 
-    def order
-      profile.sales
-        .of_user(session.id, user)
-        .order('created_at DESC')
-        .first
+    def create_order_if_needed
+      return if order
+      @order = profile.sales.create! consumer: user.person
     end
 
   end




diff --git a/plugins/stores_app/app/controllers/profile/stores_app_plugin/orders_controller.rb b/plugins/stores_app/app/controllers/profile/stores_app_plugin/orders_controller.rb
index e51e021fa60c2b202e1e382098b977a8699f8d2f..a81eaa9e6d2dcd1eb489e52522550f75d0c06dfa 100644
--- a/plugins/stores_app/app/controllers/profile/stores_app_plugin/orders_controller.rb
+++ b/plugins/stores_app/app/controllers/profile/stores_app_plugin/orders_controller.rb
@@ -2,17 +2,11 @@ module StoresAppPlugin
   class OrdersController < ApiController
 
     def last
-      render json: OrderSerializer.new(last_order, scope: self).to_hash
+      return render json: nil unless order
+      render json: OrderSerializer.new(order, scope: self).to_hash
     end
 
     protected
-
-    def last_order
-      profile.sales
-        .of_user(session.id, user)
-        .order('created_at DESC')
-        .first
-    end
 
   end
 end