2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-21 16:47:35 +02:00
|
|
|
# API Controller for resources of type Users with role :partner or :manager
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::UsersController < API::APIController
|
2016-03-23 18:39:41 +01:00
|
|
|
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
|
2020-04-27 17:48:13 +02:00
|
|
|
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
|
2022-11-14 17:54:14 +01:00
|
|
|
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(
|
2020-04-22 10:46:46 +02:00
|
|
|
: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
|