diff --git a/rails_application/app/controllers/client/orders_controller.rb b/rails_application/app/controllers/client/orders_controller.rb index 5e5bb8670..8cfd2dc59 100644 --- a/rails_application/app/controllers/client/orders_controller.rb +++ b/rails_application/app/controllers/client/orders_controller.rb @@ -22,16 +22,12 @@ def create end def show - @order = ClientOrders::Order.find_by_order_uid(params[:id]) - @order_lines = ClientOrders::OrderLine.where(order_uid: params[:id]) - render html: ClientOrders::ShowOrder.build(view_context, @order, @order_lines), layout: true + render html: ClientOrders::ShowOrder.build(view_context, params[:id]), layout: true end def edit order_id = params[:id] - order_lines = ClientOrders::OrderLine.where(order_uid: params[:id]) - products = ClientOrders::Product.all - render html: ClientOrders::EditOrder.build(view_context, order_id, order_lines, products), layout: true + render html: ClientOrders::EditOrder.build(view_context, order_id), layout: true end def add_item diff --git a/rails_application/app/read_models/client_orders/assign_customer_to_order.rb b/rails_application/app/read_models/client_orders/assign_customer_to_order.rb deleted file mode 100644 index 76fcd6547..000000000 --- a/rails_application/app/read_models/client_orders/assign_customer_to_order.rb +++ /dev/null @@ -1,15 +0,0 @@ -module ClientOrders - class AssignCustomerToOrder - def call(event) - order_uid = event.data.fetch(:order_id) - order = Order.find_by(order_uid: order_uid) - - if order.nil? - order = Order.create!(order_uid: order_uid, state: "Draft") - end - - order.client_uid = event.data.fetch(:customer_id) - order.save! - end - end -end diff --git a/rails_application/app/read_models/client_orders/cancel_order.rb b/rails_application/app/read_models/client_orders/cancel_order.rb deleted file mode 100644 index 48b21b3b5..000000000 --- a/rails_application/app/read_models/client_orders/cancel_order.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ClientOrders - class CancelOrder - def call(event) - order = Order.find_by(order_uid: event.data.fetch(:order_id)) - order.state = "Cancelled" - order.save! - end - end -end - diff --git a/rails_application/app/read_models/client_orders/change_product_name.rb b/rails_application/app/read_models/client_orders/change_product_name.rb deleted file mode 100644 index 139f76bac..000000000 --- a/rails_application/app/read_models/client_orders/change_product_name.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ClientOrders - class ChangeProductName - def call(event) - Product.find_or_create_by(uid: event.data.fetch(:product_id)).update( - name: event.data.fetch(:name) - ) - end - end -end - diff --git a/rails_application/app/read_models/client_orders/change_product_price.rb b/rails_application/app/read_models/client_orders/change_product_price.rb deleted file mode 100644 index b3a6a016c..000000000 --- a/rails_application/app/read_models/client_orders/change_product_price.rb +++ /dev/null @@ -1,7 +0,0 @@ -module ClientOrders - class ChangeProductPrice - def call(event) - Product.find_or_create_by(uid: event.data.fetch(:product_id)).update(price: event.data.fetch(:price)) - end - end -end diff --git a/rails_application/app/read_models/client_orders/configuration.rb b/rails_application/app/read_models/client_orders/configuration.rb index 43dbebda5..735772e73 100644 --- a/rails_application/app/read_models/client_orders/configuration.rb +++ b/rails_application/app/read_models/client_orders/configuration.rb @@ -1,3 +1,10 @@ +require_relative 'rendering/orders_list' +require_relative 'rendering/show_order' +require_relative 'rendering/edit_order' + +require_relative 'product_handlers' +require_relative 'order_handlers' + module ClientOrders class Client < ApplicationRecord diff --git a/rails_application/app/read_models/client_orders/confirm_order.rb b/rails_application/app/read_models/client_orders/confirm_order.rb deleted file mode 100644 index 14f47521f..000000000 --- a/rails_application/app/read_models/client_orders/confirm_order.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ClientOrders - class ConfirmOrder - def call(event) - order = Order.find_by(order_uid: event.data.fetch(:order_id)) - order.state = "Paid" - order.save! - end - end -end - diff --git a/rails_application/app/read_models/client_orders/expire_order.rb b/rails_application/app/read_models/client_orders/expire_order.rb deleted file mode 100644 index b3663460e..000000000 --- a/rails_application/app/read_models/client_orders/expire_order.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ClientOrders - class ExpireOrder - def call(event) - order = Order.find_by(order_uid: event.data.fetch(:order_id)) - order.state = "Expired" - order.save! - end - end -end - diff --git a/rails_application/app/read_models/client_orders/order_handlers.rb b/rails_application/app/read_models/client_orders/order_handlers.rb new file mode 100644 index 000000000..ae2dce03d --- /dev/null +++ b/rails_application/app/read_models/client_orders/order_handlers.rb @@ -0,0 +1,82 @@ +module ClientOrders + + class ConfirmOrder + def call(event) + order = Order.find_by(order_uid: event.data.fetch(:order_id)) + order.state = "Paid" + order.save! + end + end + + class SubmitOrder + def call(event) + order = Order.find_or_create_by(order_uid: event.data.fetch(:order_id)) + order.number = event.data.fetch(:order_number) + order.state = "Submitted" + order.save! + end + end + + class UpdateDiscount + def call(event) + order = Order.find_or_create_by!(order_uid: event.data.fetch(:order_id)) + order.percentage_discount = event.data.fetch(:amount) + order.save! + end + end + + class UpdateOrderTotalValue + def call(event) + order = Order.find_or_create_by!(order_uid: event.data.fetch(:order_id)) { |order| order.state = "Draft" } + order.discounted_value = event.data.fetch(:discounted_amount) + order.total_value = event.data.fetch(:total_amount) + order.save! + end + end + + class UpdatePaidOrdersSummary + def call(event) + order = Order.find_by(order_uid: event.data.fetch(:order_id)) + client = Client.where(uid: order.client_uid).first + client.update(paid_orders_summary: client.paid_orders_summary + order.discounted_value) + end + end + + class ExpireOrder + def call(event) + order = Order.find_by(order_uid: event.data.fetch(:order_id)) + order.state = "Expired" + order.save! + end + end + + class CancelOrder + def call(event) + order = Order.find_by(order_uid: event.data.fetch(:order_id)) + order.state = "Cancelled" + order.save! + end + end + + class AssignCustomerToOrder + def call(event) + order_uid = event.data.fetch(:order_id) + order = Order.find_by(order_uid: order_uid) + + if order.nil? + order = Order.create!(order_uid: order_uid, state: "Draft") + end + + order.client_uid = event.data.fetch(:customer_id) + order.save! + end + end + + class ResetDiscount + def call(event) + order = Order.find_by(order_uid: event.data.fetch(:order_id)) + order.percentage_discount = nil + order.save! + end + end +end diff --git a/rails_application/app/read_models/client_orders/product_handlers.rb b/rails_application/app/read_models/client_orders/product_handlers.rb new file mode 100644 index 000000000..36df67172 --- /dev/null +++ b/rails_application/app/read_models/client_orders/product_handlers.rb @@ -0,0 +1,30 @@ +module ClientOrders + class RegisterProduct + def call(event) + Product.find_or_create_by(uid: event.data.fetch(:product_id)) + end + end + + class ChangeProductName + def call(event) + Product.find_or_create_by(uid: event.data.fetch(:product_id)).update( + name: event.data.fetch(:name) + ) + end + end + + class ChangeProductPrice + def call(event) + Product.find_or_create_by(uid: event.data.fetch(:product_id)).update(price: event.data.fetch(:price)) + end + end + + class UpdateProductAvailability + def call(event) + product = Product.find_by(uid: event.data.fetch(:product_id)) + available = event.data.fetch(:available) + + product.update(available: available.positive?) + end + end +end diff --git a/rails_application/app/read_models/client_orders/register_product.rb b/rails_application/app/read_models/client_orders/register_product.rb deleted file mode 100644 index 12b8c813a..000000000 --- a/rails_application/app/read_models/client_orders/register_product.rb +++ /dev/null @@ -1,7 +0,0 @@ -module ClientOrders - class RegisterProduct - def call(event) - Product.find_or_create_by(uid: event.data.fetch(:product_id)) - end - end -end diff --git a/rails_application/app/read_models/client_orders/edit_order.rb b/rails_application/app/read_models/client_orders/rendering/edit_order.rb similarity index 95% rename from rails_application/app/read_models/client_orders/edit_order.rb rename to rails_application/app/read_models/client_orders/rendering/edit_order.rb index b6a4e13f7..503ab998d 100644 --- a/rails_application/app/read_models/client_orders/edit_order.rb +++ b/rails_application/app/read_models/client_orders/rendering/edit_order.rb @@ -4,7 +4,9 @@ class EditOrder < Arbre::Component include ActionView::Helpers::FormTagHelper include ActionView::Helpers::UrlHelper - def self.build(view_context, order_id, order_lines, products) + def self.build(view_context, order_id) + order_lines = ClientOrders::OrderLine.where(order_uid: order_id) + products = ClientOrders::Product.all new(Arbre::Context.new(nil, view_context)).build(order_id, order_lines, products) end diff --git a/rails_application/app/read_models/client_orders/orders_list.rb b/rails_application/app/read_models/client_orders/rendering/orders_list.rb similarity index 100% rename from rails_application/app/read_models/client_orders/orders_list.rb rename to rails_application/app/read_models/client_orders/rendering/orders_list.rb diff --git a/rails_application/app/read_models/client_orders/show_order.rb b/rails_application/app/read_models/client_orders/rendering/show_order.rb similarity index 93% rename from rails_application/app/read_models/client_orders/show_order.rb rename to rails_application/app/read_models/client_orders/rendering/show_order.rb index c764b43d7..27611d83b 100644 --- a/rails_application/app/read_models/client_orders/show_order.rb +++ b/rails_application/app/read_models/client_orders/rendering/show_order.rb @@ -2,7 +2,9 @@ module ClientOrders class ShowOrder < Arbre::Component include Rails.application.routes.url_helpers - def self.build(view_context, order, order_lines) + def self.build(view_context, order_id) + order = ClientOrders::Order.find_by_order_uid(order_id) + order_lines = ClientOrders::OrderLine.where(order_uid: order_id) new(Arbre::Context.new(nil, view_context)).build(order, order_lines) end diff --git a/rails_application/app/read_models/client_orders/reset_discount.rb b/rails_application/app/read_models/client_orders/reset_discount.rb deleted file mode 100644 index 8af8d72d7..000000000 --- a/rails_application/app/read_models/client_orders/reset_discount.rb +++ /dev/null @@ -1,9 +0,0 @@ -module ClientOrders - class ResetDiscount - def call(event) - order = Order.find_by(order_uid: event.data.fetch(:order_id)) - order.percentage_discount = nil - order.save! - end - end -end diff --git a/rails_application/app/read_models/client_orders/submit_order.rb b/rails_application/app/read_models/client_orders/submit_order.rb deleted file mode 100644 index 0cc1ecb89..000000000 --- a/rails_application/app/read_models/client_orders/submit_order.rb +++ /dev/null @@ -1,11 +0,0 @@ -module ClientOrders - class SubmitOrder - def call(event) - order = Order.find_or_create_by(order_uid: event.data.fetch(:order_id)) - order.number = event.data.fetch(:order_number) - order.state = "Submitted" - order.save! - end - end -end - diff --git a/rails_application/app/read_models/client_orders/update_discount.rb b/rails_application/app/read_models/client_orders/update_discount.rb deleted file mode 100644 index ca393197c..000000000 --- a/rails_application/app/read_models/client_orders/update_discount.rb +++ /dev/null @@ -1,9 +0,0 @@ -module ClientOrders - class UpdateDiscount - def call(event) - order = Order.find_or_create_by!(order_uid: event.data.fetch(:order_id)) - order.percentage_discount = event.data.fetch(:amount) - order.save! - end - end -end diff --git a/rails_application/app/read_models/client_orders/update_order_total_value.rb b/rails_application/app/read_models/client_orders/update_order_total_value.rb deleted file mode 100644 index 87a879b48..000000000 --- a/rails_application/app/read_models/client_orders/update_order_total_value.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ClientOrders - class UpdateOrderTotalValue - def call(event) - order = Order.find_or_create_by!(order_uid: event.data.fetch(:order_id)) { |order| order.state = "Draft" } - order.discounted_value = event.data.fetch(:discounted_amount) - order.total_value = event.data.fetch(:total_amount) - order.save! - end - end -end diff --git a/rails_application/app/read_models/client_orders/update_paid_orders_summary.rb b/rails_application/app/read_models/client_orders/update_paid_orders_summary.rb deleted file mode 100644 index 20f73be80..000000000 --- a/rails_application/app/read_models/client_orders/update_paid_orders_summary.rb +++ /dev/null @@ -1,9 +0,0 @@ -module ClientOrders - class UpdatePaidOrdersSummary - def call(event) - order = Order.find_by(order_uid: event.data.fetch(:order_id)) - client = Client.where(uid: order.client_uid).first - client.update(paid_orders_summary: client.paid_orders_summary + order.discounted_value) - end - end -end diff --git a/rails_application/app/read_models/client_orders/update_product_availability.rb b/rails_application/app/read_models/client_orders/update_product_availability.rb deleted file mode 100644 index a35546300..000000000 --- a/rails_application/app/read_models/client_orders/update_product_availability.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ClientOrders - class UpdateProductAvailability - def call(event) - product = Product.find_by(uid: event.data.fetch(:product_id)) - available = event.data.fetch(:available) - - product.update(available: available.positive?) - end - end -end