1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/app/controllers/api/users_controller.rb

62 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-04-21 16:47:35 +02:00
# API Controller for resources of type Users with role :partner or :manager
2016-03-23 18:39:41 +01:00
class API::UsersController < API::ApiController
before_action :authenticate_user!
2020-04-21 16:47:35 +02:00
before_action :set_user, only: %i[destroy]
2016-03-23 18:39:41 +01:00
def index
authorize User
if %w[partner manager].include?(params[:role])
2020-04-21 16:47:35 +02:00
@users = User.with_role(params[:role].to_sym).includes(:profile)
2016-03-23 18:39:41 +01:00
else
head :forbidden
2016-03-23 18:39:41 +01:00
end
end
def create
2019-09-25 16:37:42 +02:00
authorize User
2020-04-22 10:09:16 +02:00
res = if !params[:user].empty?
2020-04-21 17:24:22 +02:00
UserService.create_partner(partner_params)
2020-04-22 10:09:16 +02:00
elsif !params[:manager].empty?
2020-04-21 17:24:22 +02:00
UserService.create_manager(manager_params)
else
nil
end
2016-03-23 18:39:41 +01:00
2019-09-25 16:37:42 +02:00
if res[:saved]
@user = res[:user]
render status: :created
2020-04-21 17:24:22 +02:00
else
2019-09-25 16:37:42 +02:00
render json: res[:user].errors.full_messages, status: :unprocessable_entity
2016-03-23 18:39:41 +01:00
end
end
2020-04-21 16:47:35 +02:00
def destroy
authorize User
@user.destroy
head :no_content
end
2016-03-23 18:39:41 +01:00
private
2019-01-07 12:48:22 +01:00
2020-04-21 16:47:35 +02:00
def set_user
@user = User.find(params[:id])
end
2016-03-23 18:39:41 +01:00
def partner_params
params.require(:user).permit(:email, :first_name, :last_name)
end
2020-04-21 17:24:22 +02:00
def manager_params
params.require(:manager).permit(
:username, :email, :group_id,
tag_ids: [],
profile_attributes: %i[first_name last_name phone],
invoicing_profile_attributes: [address_attributes: [:address]],
statistic_profile_attributes: %i[gender birthday]
2020-04-21 17:24:22 +02:00
)
end
2016-03-23 18:39:41 +01:00
end