module Comee module Core class ClientsController < ApplicationController include Common def index super do Client.includes(:parent, :agents, :contacts, :client_warehouses, :country, :user, :currency).all end end def filter clients = Client.includes(:parent, :agents, :contacts, :client_warehouses, :country, :user, :currency) .ransack(params[:q]).result render_content(clients) end def consignees client = Client.find(params[:id]) render json: {success: true, data: client.consignees} rescue StandardError => e render json: {success: false, error: e.message}, status: :unprocessable_entity end def create_agent client = Client.includes(:parent, :agents, :contacts, :client_warehouses).find(params[:id]) agent = Agent.new(agent_params) if agent.save client.agents << agent client.save! render_content(client) else render json: {success: false, error: agent.errors.full_messages[0]}, status: :unprocessable_entity end end def create_contact client = Client.includes(:parent, :agents, :contacts, :client_warehouses).find(params[:id]) contact = Contact.new(contact_params) if contact.save client.contacts << contact client.save! render_content(client) else render json: {success: false, error: contact.errors.full_messages[0]}, status: :unprocessable_entity end end def agents client = Client.find(params[:id]) render_content(client.agents) rescue StandardError => e render json: {success: false, error: e.message}, status: :unprocessable_entity end private def model_params params.require(:payload).permit(:code, :name, :address, :locale, :user_id, :parent_id, :currency, :subsidiary, :country_id, :tax_code, :vat_number, consignees: []) end def agent_params params.require(:payload).permit(:name, :address, :email, :telephone) end def contact_params params.require(:payload).permit(:first_name, :last_name, :email, :telephone) end end end end