From 3c76bc939f44614850898bcf1e5086f997d94dcf Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sun, 16 Apr 2023 11:29:32 -0600 Subject: Refactoriza PaymentService --- app/services/payment_services/payment_service.rb | 26 +++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/services/payment_services/payment_service.rb b/app/services/payment_services/payment_service.rb index 82267d5..32f6d94 100644 --- a/app/services/payment_services/payment_service.rb +++ b/app/services/payment_services/payment_service.rb @@ -23,24 +23,30 @@ module PaymentServices return end + update_products_available_quantity + update_order + end + + private + + def update_products_available_quantity + @order.products.joins(:product_orders).select('products.*, product_orders.quantity AS bought_quantity').distinct + .find_each do |product| + product.update(available_quantity: product.available_quantity - product.bought_quantity) + end + end + + def update_order @order.update(payment_id: @payment.id) @user_account.cart.product_carts.destroy_all @order end - private - def total total = 0 - @order.product_orders.joins(:product).select( - 'product_orders.*, products.bulk_price AS bulk_price, products.unitary_price AS unitary_price' - ).each do |product_order| - total += if product_order.quantity < 5 - product_order.unitary_price * product_order.quantity - else - product_order.bulk_price * product_order.quantity - end + @order.product_orders.includes(:product).find_each do |product_order| + total += product_order.total end total end -- cgit v1.2.3