module Logistics module Core class AgenciesController < ApplicationController before_action :set_agency, only: [:update] # GET /agencies # GET /agencies.json def index data = ApplicationRecord.as_json(Agency.includes(:agency_category)) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end # POST /agencies # POST /agencies.json def create @agency = Agency.new(agency_params) if @agency.save response = Mks::Common::MethodResponse.new(true, "Agency information saved successfully!", @agency, nil, nil) else errors = Mks::Common::Util.error_messages @agency, "Agency" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end # PATCH/PUT /agencies/1 # PATCH/PUT /agencies/1.json def update if @agency.update(agency_params) response = Mks::Common::MethodResponse.new(true, "Agency information updated successfully!", @agency, nil, nil) else errors = Mks::Common::Util.error_messages @agency, "Agency" response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end def get_permit_documents agency_id = params[:id] agency_permit_documents = AgencyPermitDocument.where agency_id: agency_id result = [] agency_permit_documents.each do |document_type| result.push({ id: document_type.id, agency_id: document_type.agency_id, agency_name: document_type.agency.name, document_type_id: document_type.document_type_id, document_name: document_type.document_type.name, document_type_category: document_type.document_type.document_type_category.name, document_origin: document_type.document_type.document_type_origin.name }) end response = Mks::Common::MethodResponse.new(true, nil, result, nil, nil) render json: response end def get_document_type_with_no_agency dt_ids = AgencyPermitDocument.where(agency_id: params[:id]).map(&:document_type_id) document_types = DocumentType.where.not(id: dt_ids) data = JSON(ActiveModelSerializers::SerializableResource.new(document_types).to_json) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end private def set_agency @agency = Agency.find(params[:id]) end def agency_params params.require(:agency).permit(:code, :name, :agency_category_id, :city, :subcity, :woreda, :house_no, :telephone, :email, :postal_code, :contact_person, :contact_telephone, :contact_email) end end end end