Commit 077e0016 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei

Add possibility to update self professional info

Signed-off-by: Bruno Freitas Tissei's avatarBruno Freitas Tissei <bft15@inf.ufpr.br>
parent 8cc1aa4c
Pipeline #14096 failed with stage
in 1 minute and 24 seconds
......@@ -100,9 +100,8 @@ module Api::V1
return
end
# Update account with params except citizen's
if @resource.send(resource_update_method, account_update_params.except(:citizen))
if @resource.send(resource_update_method, account_update_params.except(:citizen, :professional))
yield @resource if block_given?
# Check if citizen's params are not empty
......@@ -114,6 +113,18 @@ module Api::V1
# Update citizen with account_update_params[:citizen]
if @resource.citizen.update(account_update_params[:citizen])
# Update professional info if provided
if not account_update_params[:professional].nil?
if not @resource.citizen.professional.nil? and
(not @resource.citizen.professional.update(account_update_params[:professional]))
render json: @resource.citizen.professional.errors, status: :unprocessable_entity
return
else
@resource.citizen.professional.save!
end
end
# Update image if provided
if params[:citizen][:image]
if params[:citizen][:image][:content_type] == "delete"
......
......@@ -13,6 +13,7 @@ class ApplicationController < ActionController::API
protect_from_forgery with: :null_session,
if: Proc.new { |c| c.request.format.json? }
# This method is useful in the rest of the application for checking permissions
#
# @return [Citizen, String] the current logged citizen and the permission
......@@ -23,6 +24,7 @@ class ApplicationController < ActionController::API
end
end
# It is executed in every request that requires authorization (called in
# authenticable concern as a before_action). It checks if the current_user
# has the permission he claims in the parameters, an errors gets returned if
......@@ -71,11 +73,14 @@ class ApplicationController < ActionController::API
end
end
protected
# permit parameters for devise functions
def configure_permitted_parameters
citizen_keys = Citizen.keys
professional_keys = [:registration, :occupation_id]
# set sign_up hash to keys from citizen's registration form
devise_parameter_sanitizer.permit(
......@@ -112,11 +117,13 @@ class ApplicationController < ActionController::API
# set account_update hash to keys required to update citizen's account
devise_parameter_sanitizer.permit(
:account_update, keys: [
citizen: citizen_keys
citizen: citizen_keys,
professional: professional_keys
]
)
end
private
def user_not_authorized(exception)
......
......@@ -31,6 +31,7 @@ class Account < ApplicationRecord
response = self.as_json(except: [
:tokens, :created_at, :updated_at
]).merge({
professional: self.professional.basic_info_response,
citizen: self.citizen.complete_info_response
})
......
......@@ -75,6 +75,12 @@ class Professional < ApplicationRecord
})
end
# @return [Json] basic professional's data
def basic_info_response
return self.as_json(only: [:id, :registration, :active, :occupation_id],
methods: %w(occupation_name))
end
# @return [Array] list of roles
def roles_ids
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment