summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-14 20:52:50 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-14 20:52:50 -0600
commit9607fcb5a9cdc7595633336881cc0dd8aa9fb8ca (patch)
treea03b7f31fdca2e531d082ac813dc341262631a85 /app/services
parent8fbeea986d06a0052ce0132717f22b6ff8bd1a04 (diff)
Corrige errores de OrdersController
Diffstat (limited to 'app/services')
-rw-r--r--app/services/payment/cart_to_order_service.rb21
-rw-r--r--app/services/payment/payment_service.rb42
-rw-r--r--app/services/payment_services/cart_to_order_service.rb33
-rw-r--r--app/services/payment_services/payment_service.rb48
4 files changed, 81 insertions, 63 deletions
diff --git a/app/services/payment/cart_to_order_service.rb b/app/services/payment/cart_to_order_service.rb
deleted file mode 100644
index 8dff43e..0000000
--- a/app/services/payment/cart_to_order_service.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-# frozen_string_literal: true
-
-module Payment
- # CartToOrderService
- class CartToOrderService
- def initialize(user_account)
- @user_account = user_account
- @cart = @user_account.cart
- end
-
- def call
- order = Order.create(user_account_id: @user_account.id)
-
- @cart.product_carts.pluck(:product_id, :quantity).each do |data|
- ProductOrder.create(order_id: order.id, product_id: data[0], quantity: data[1])
- end
-
- order
- end
- end
-end
diff --git a/app/services/payment/payment_service.rb b/app/services/payment/payment_service.rb
deleted file mode 100644
index 9f44f87..0000000
--- a/app/services/payment/payment_service.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-# frozen_string_literal: true
-
-module Payment
- # PaymentService
- class PaymentService
- attr_reader :error_messages
-
- def initialize(params, user_account)
- @user_account = user_account
- @params = params
- @order = Order.new(params.except(:card_id))
- @card_id = params[:card_id]
- @order = CartToOrderService.new(@user_account).call
- end
-
- def call
- payment = Payment.new(order_id: @order.id, card_id: @card_id, total:)
-
- @error_messages = { errors: @payment.errors.as_json } and return unless payment.save
-
- @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
- unitary_price * product_order.quantity
- else
- bulk_price * product_order.quantity
- end
- end
- end
- end
-end
diff --git a/app/services/payment_services/cart_to_order_service.rb b/app/services/payment_services/cart_to_order_service.rb
new file mode 100644
index 0000000..3985f90
--- /dev/null
+++ b/app/services/payment_services/cart_to_order_service.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module PaymentServices
+ # CartToOrderService
+ class CartToOrderService
+ attr_reader :error_messages
+
+ def initialize(user_account)
+ @user_account = user_account
+ @cart = @user_account.cart
+ end
+
+ def call
+ @error_messages = { errors: 'Cart is empty' } and return if @cart.product_carts.empty?
+
+ order = Order.create(user_account_id: @user_account.id, public_id: generate_public_id)
+
+ @cart.product_carts.pluck(:product_id, :quantity).each do |data|
+ ProductOrder.create(order_id: order.id, product_id: data[0], quantity: data[1])
+ end
+
+ order
+ end
+
+ private
+
+ def generate_public_id
+ public_id = SecureRandom.hex(12)
+ public_id = SecureRandom.hex(12) while Order.exists?(public_id:)
+ public_id
+ end
+ end
+end
diff --git a/app/services/payment_services/payment_service.rb b/app/services/payment_services/payment_service.rb
new file mode 100644
index 0000000..82267d5
--- /dev/null
+++ b/app/services/payment_services/payment_service.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module PaymentServices
+ # PaymentService
+ class PaymentService
+ attr_reader :error_messages
+
+ def initialize(card_id, user_account)
+ @user_account = user_account
+ @card_id = card_id
+ @service = CartToOrderService.new(@user_account)
+ @order = @service.call
+ end
+
+ def call
+ @error_messages = @service.error_messages and return if @order.nil?
+
+ @payment = Payment.new(order_id: @order.id, card_id: @card_id, total:)
+
+ unless @payment.save
+ @error_messages = @payment.errors.as_json
+ @order.destroy
+ return
+ end
+
+ @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
+ end
+ total
+ end
+ end
+end