module Comee module Core class ClientsController < ApplicationController include Common def index super do Client.includes( :parent, :agents, :contacts, :client_warehouses, :client_addresses, :country, :user, :currency ).all end end def filter clients = Client.includes( :parent, :agents, :contacts, :client_warehouses, :client_addresses, :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: 422 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: 422 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: 422 end end def agents client = Client.find(params[:id]) render_content(client.agents) rescue StandardError => e render json: {success: false, error: e.message}, status: 422 end private def model_params params.require(:payload).permit(:code, :name, :match_code, :locale, :user_id, :parent_id, :currency, :subsidiary, :country_id, :tax_code, :vat_number, :print_details, consignees: []) end def agent_params params.require(:payload).permit( :name, :address_line1, :street, :city, :state, :country_id, :postal_code, :email, :telephone ) end def contact_params params.require(:payload).permit(:first_name, :last_name, :email, :telephone) end end end end