Add Registrant API contact update action

Closes #849
This commit is contained in:
Artur Beljajev 2018-08-20 17:19:54 +03:00
parent 90ed23f64d
commit b6ecae6a35
41 changed files with 1239 additions and 61 deletions

View file

@ -8,6 +8,8 @@ module Api
before_action :authenticate
before_action :set_paper_trail_whodunnit
rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error
rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
error = {}
error[parameter_missing_exception.param] = ['parameter is required']
@ -49,6 +51,14 @@ module Api
def set_paper_trail_whodunnit
::PaperTrail.whodunnit = current_registrant_user.id_role_username
end
def show_not_found_error
render json: { errors: [{ base: ['Not found'] }] }, status: :not_found
end
def show_invalid_record_error(exception)
render json: { errors: exception.record.errors }, status: :bad_request
end
end
end
end

View file

@ -32,6 +32,63 @@ module Api
end
end
def update
contact = @contacts_pool.find_by!(uuid: params[:uuid])
contact.name = params[:name] if params[:name].present?
contact.email = params[:email] if params[:email].present?
contact.phone = params[:phone] if params[:phone].present?
if Setting.address_processing && params[:address]
address = Contact::Address.new(params[:address][:street],
params[:address][:zip],
params[:address][:city],
params[:address][:state],
params[:address][:country_code])
contact.address = address
end
if !Setting.address_processing && params[:address]
error_msg = 'Address processing is disabled and therefore cannot be updated'
render json: { errors: [{ address: [error_msg] }] }, status: :bad_request and return
end
if ENV['fax_enabled'] == 'true'
contact.fax = params[:fax] if params[:fax].present?
end
if ENV['fax_enabled'] != 'true' && params[:fax]
error_msg = 'Fax processing is disabled and therefore cannot be updated'
render json: { errors: [{ address: [error_msg] }] }, status: :bad_request and return
end
contact.transaction do
contact.save!
action = current_registrant_user.actions.create!(contact: contact, operation: :update)
contact.registrar.notify(action)
end
render json: { id: contact.uuid,
name: contact.name,
code: contact.code,
ident: {
code: contact.ident,
type: contact.ident_type,
country_code: contact.ident_country_code,
},
email: contact.email,
phone: contact.phone,
fax: contact.fax,
address: {
street: contact.street,
zip: contact.zip,
city: contact.city,
state: contact.state,
country_code: contact.country_code,
},
auth_info: contact.auth_info,
statuses: contact.statuses }
end
private
def set_contacts_pool

View file

@ -1,6 +1,8 @@
class Registrant::ContactsController < RegistrantController
helper_method :domain_ids
helper_method :domain
helper_method :fax_enabled?
skip_authorization_check only: %i[edit update]
def show
@contact = Contact.where(id: contacts).find_by(id: params[:id])
@ -8,6 +10,25 @@ class Registrant::ContactsController < RegistrantController
authorize! :read, @contact
end
def edit
@contact = Contact.where(id: contacts).find(params[:id])
end
def update
@contact = Contact.where(id: contacts).find(params[:id])
@contact.attributes = contact_params
response = update_contact_via_api(@contact.uuid)
updated = response.is_a?(Net::HTTPSuccess)
if updated
redirect_to registrant_domain_contact_url(domain, @contact), notice: t('.updated')
else
parsed_response = JSON.parse(response.body, symbolize_names: true)
@errors = parsed_response[:errors]
render :edit
end
end
private
def contacts
@ -41,4 +62,68 @@ class Registrant::ContactsController < RegistrantController
current_registrant_user.domains
end
end
end
def contact_params
permitted = %i[
name
email
phone
]
permitted << :fax if fax_enabled?
permitted += %i[street zip city state country_code] if Contact.address_processing?
params.require(:contact).permit(*permitted)
end
def access_token
uri = URI.parse("#{ENV['registrant_api_base_url']}/api/v1/registrant/auth/eid")
request = Net::HTTP::Post.new(uri)
request.form_data = access_token_request_params
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
http.request(request)
end
json_doc = JSON.parse(response.body, symbolize_names: true)
json_doc[:access_token]
end
def access_token_request_params
{ ident: current_registrant_user.ident,
first_name: current_registrant_user.first_name,
last_name: current_registrant_user.last_name }
end
def fax_enabled?
ENV['fax_enabled'] == 'true'
end
def contact_update_api_params
params = contact_params
params = normalize_address_attributes_for_api(params) if Contact.address_processing?
params
end
def normalize_address_attributes_for_api(params)
normalized = params
Contact.address_attribute_names.each do |attr|
attr = attr.to_sym
normalized["address[#{attr}]"] = params[attr]
normalized.delete(attr)
end
normalized
end
def update_contact_via_api(uuid)
uri = URI.parse("#{ENV['registrant_api_base_url']}/api/v1/registrant/contacts/#{uuid}")
request = Net::HTTP::Patch.new(uri)
request['Authorization'] = "Bearer #{access_token}"
request.form_data = contact_update_api_params
Net::HTTP.start(uri.hostname, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
http.request(request)
end
end
end