2019-08-14 09:48:44 +00:00
|
|
|
class Api::V1::AgentsController < Api::BaseController
|
|
|
|
before_action :fetch_agent, except: [:create, :index]
|
|
|
|
before_action :check_authorization
|
|
|
|
before_action :build_agent, only: [:create]
|
|
|
|
|
|
|
|
def index
|
|
|
|
render json: agents
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@agent.destroy
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2019-12-10 04:59:35 +00:00
|
|
|
@agent.update!(agent_params)
|
2019-08-14 09:48:44 +00:00
|
|
|
render json: @agent
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@agent.save!
|
|
|
|
render json: @agent
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def check_authorization
|
|
|
|
authorize(User)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_agent
|
|
|
|
@agent = agents.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_agent
|
|
|
|
@agent = agents.new(new_agent_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent_params
|
|
|
|
params.require(:agent).permit(:email, :name, :role)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_agent_params
|
|
|
|
time = Time.now.to_i
|
2019-10-14 08:54:58 +00:00
|
|
|
params.require(:agent).permit(:email, :name, :role)
|
2019-10-20 08:47:26 +00:00
|
|
|
.merge!(password: time, password_confirmation: time, inviter: current_user)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def agents
|
|
|
|
@agents ||= current_account.users
|
|
|
|
end
|
|
|
|
end
|