module Comee module Core class CustomerOrdersController < ApplicationController include Common before_action :set_service, only: %i[create_with_items submit submit_for_confirmation accept cancel] def create_with_items order = @service.create(create_with_item_params) render json: {success: true, data: serialize(order)} end def submit order = @service.submit(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def submit_for_confirmation order = @service.submit_for_confirmation(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def accept order = @service.accept(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def cancel order = @service.cancel(params[:id]) render json: {success: true, data: serialize(order)} rescue StandardError => e render json: {success: false, error: e.message} end def filter @customer_orders = Comee::Core::CustomerOrder.ransack(params[:q]).result render json: {success: true, data: serialize(@customer_orders)} end private def model_params params.require(:payload) .permit( :order_number, :order_date, :order_terms, :consignee, :final_destination, :final_delivery_date, :handover_date, :shipping_date, :delivery_address, :invoice_address, :status, :client_id, :file ) end def create_with_item_params params.require(:payload) .permit( :order_number, :order_date, :order_terms, :consignee, :final_destination, :final_delivery_date, :handover_date, :shipping_date, :delivery_address, :invoice_address, :status, :client_id, items: %i[customer_item_no product_id quantity price unit_id delivery_date] ) end def set_service @service = CustomerOrderService.new end end end end