From 631d5889c09affd170c88cfca3cb2d8aa44f0e27 Mon Sep 17 00:00:00 2001 From: Du Peng Date: Sun, 28 Aug 2022 23:23:58 +0200 Subject: [PATCH] refactoring create cart service --- app/controllers/api/cart_controller.rb | 17 +----- app/services/cart/create_service.rb | 19 ------- app/services/cart/find_or_create_service.rb | 60 +++++++++++++++++++++ 3 files changed, 61 insertions(+), 35 deletions(-) delete mode 100644 app/services/cart/create_service.rb create mode 100644 app/services/cart/find_or_create_service.rb diff --git a/app/controllers/api/cart_controller.rb b/app/controllers/api/cart_controller.rb index 6ecc1108f..14f3becb5 100644 --- a/app/controllers/api/cart_controller.rb +++ b/app/controllers/api/cart_controller.rb @@ -9,22 +9,7 @@ class API::CartController < API::ApiController def create authorize :cart, :create? - @order = Order.find_by(token: order_token, state: 'cart') - if @order.nil? - if current_user&.member? - @order = Order.where(statistic_profile_id: current_user.statistic_profile.id, - state: 'cart').last - end - if current_user&.privileged? - @order = Order.where(operator_profile_id: current_user.invoicing_profile.id, - state: 'cart').last - end - end - if @order - @order.update(statistic_profile_id: current_user.statistic_profile.id) if @order.statistic_profile_id.nil? && current_user&.member? - @order.update(operator_profile_id: current_user.invoicing_profile.id) if @order.operator_profile_id.nil? && current_user&.privileged? - end - @order ||= Cart::CreateService.new.call(current_user) + @order ||= Cart::FindOrCreateService.new.call(order_token, current_user) render 'api/orders/show' end diff --git a/app/services/cart/create_service.rb b/app/services/cart/create_service.rb deleted file mode 100644 index 996baa73d..000000000 --- a/app/services/cart/create_service.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -# Provides methods for create cart -class Cart::CreateService - def call(user) - token = GenerateTokenService.new.call(Order) - order_param = { - token: token, - state: 'cart', - total: 0 - } - if user - order_param[:statistic_profile_id] = user.statistic_profile.id if user.member? - - order_param[:operator_profile_id] = user.invoicing_profile.id if user.privileged? - end - Order.create!(order_param) - end -end diff --git a/app/services/cart/find_or_create_service.rb b/app/services/cart/find_or_create_service.rb new file mode 100644 index 000000000..cb48dc159 --- /dev/null +++ b/app/services/cart/find_or_create_service.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +# Provides methods for find or create a cart +class Cart::FindOrCreateService + def call(order_token, user) + order = Order.find_by(token: order_token, state: 'cart') + + if order && user && ((user.member? && order.statistic_profile_id.present? && order.statistic_profile_id != user.statistic_profile.id) || + (user.privileged? && order.operator_profile_id.present? && order.operator_profile_id != user.invoicing_profile.id)) + order = nil + end + order = nil if order && !user && order.statistic_profile_id.present? + if order && order.statistic_profile_id.present? && Order.where(statistic_profile_id: order.statistic_profile_id, + payment_state: 'paid').where('created_at > ?', order.created_at).last.present? + order = nil + end + + if order.nil? + if user&.member? + last_paid_order = Order.where(statistic_profile_id: user.statistic_profile.id, + payment_state: 'paid').last + order = if last_paid_order + Order.where(statistic_profile_id: user.statistic_profile.id, + state: 'cart').where('created_at > ?', last_paid_order.created_at).last + else + Order.where(statistic_profile_id: user.statistic_profile.id, state: 'cart').last + end + end + if user&.privileged? + last_paid_order = Order.where(operator_profile_id: user.invoicing_profile.id, + payment_state: 'paid').last + order = if last_paid_order + Order.where(operator_profile_id: user.invoicing_profile.id, + state: 'cart').where('created_at > ?', last_paid_order.created_at).last + else + Order.where(operator_profile_id: user.invoicing_profile.id, state: 'cart').last + end + end + end + + if order + order.update(statistic_profile_id: user.statistic_profile.id) if order.statistic_profile_id.nil? && user&.member? + order.update(operator_profile_id: user.invoicing_profile.id) if order.operator_profile_id.nil? && user&.privileged? + return order + end + + token = GenerateTokenService.new.call(Order) + order_param = { + token: token, + state: 'cart', + total: 0 + } + if user + order_param[:statistic_profile_id] = user.statistic_profile.id if user.member? + + order_param[:operator_profile_id] = user.invoicing_profile.id if user.privileged? + end + Order.create!(order_param) + end +end