From 9607fcb5a9cdc7595633336881cc0dd8aa9fb8ca Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 14 Apr 2023 20:52:50 -0600 Subject: Corrige errores de OrdersController --- .../payment_services/cart_to_order_service.rb | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/services/payment_services/cart_to_order_service.rb (limited to 'app/services/payment_services/cart_to_order_service.rb') 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 -- cgit v1.2.3