DRY registrant contact update action

This commit is contained in:
Thiago Youssef 2022-07-25 05:19:27 -03:00 committed by olegphenomenon
parent eb2c023e2a
commit 942cfe82b5
2 changed files with 87 additions and 52 deletions

View file

@ -48,62 +48,31 @@ module Api
def update def update
logger.debug 'Received update request' logger.debug 'Received update request'
logger.debug params logger.debug params
contact = current_user_contacts.find_by!(uuid: params[:uuid]) contact = find_contact_and_update_credentials(params[:uuid], params[:name], params[:email], params[:phone])
contact.name = params[:name] if params[:name].present?
contact.email = params[:email] if params[:email].present?
contact.phone = params[:phone] if params[:phone].present?
# Needed to support passing empty array, which otherwise gets parsed to nil reparsed_request = reparsed_request(request.body.string)
# https://github.com/rails/rails/pull/13157
reparsed_request_json = ActiveSupport::JSON.decode(request.body.string)
.with_indifferent_access
logger.debug 'Reparsed request is following'
logger.debug reparsed_request_json.to_s
disclosed_attributes = reparsed_request_json[:disclosed_attributes]
if disclosed_attributes disclosed_attributes = reparsed_request[:disclosed_attributes]
if disclosed_attributes.present? && contact.org? && !disclosed_attributes.include?('phone')
error_msg = "Legal person's data is visible by default and cannot be concealed." \
' Please remove this parameter.'
render json: { errors: [{ disclosed_attributes: [error_msg] }] }, status: :bad_request
return
end
contact.disclosed_attributes = disclosed_attributes render_disclosed_attributes_error and return if disclosed_attributes.present? && contact.org? &&
end !disclosed_attributes.include?('phone')
publishable = reparsed_request_json[:registrant_publishable] contact.disclosed_attributes = disclosed_attributes if disclosed_attributes
publishable = reparsed_request[:registrant_publishable]
contact.registrant_publishable = publishable if publishable.in? [true, false] contact.registrant_publishable = publishable if publishable.in? [true, false]
logger.debug "Setting.address_processing is set to #{Setting.address_processing}" logger.debug "Setting.address_processing is set to #{Setting.address_processing}"
if Setting.address_processing && params[:address] contact.address = parse_address(params[:address]) if Setting.address_processing && params[:address]
address = Contact::Address.new(params[:address][:street], render_address_error and return if !Setting.address_processing && params[:address]
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
contact.fax = params[:fax] if ENV['fax_enabled'] == 'true' && params[:fax].present? contact.fax = params[:fax] if ENV['fax_enabled'] == 'true' && params[:fax].present?
logger.debug "ENV['fax_enabled'] is set to #{ENV['fax_enabled']}" logger.debug "ENV['fax_enabled'] is set to #{ENV['fax_enabled']}"
if ENV['fax_enabled'] != 'true' && params[:fax] render_fax_error and return 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 = update_and_notify!(contact)
contact.save!
action = current_registrant_user.actions.create!(contact: contact, operation: :update)
contact.registrar.notify(action)
end
render json: serialize_contact(contact, false) render json: serialize_contact(contact, false)
end end
@ -139,6 +108,59 @@ module Api
def logger def logger
Rails.logger Rails.logger
end end
def render_disclosed_attributes_error
error_msg = "Legal person's data is visible by default and cannot be concealed." \
' Please remove this parameter.'
render json: { errors: [{ disclosed_attributes: [error_msg] }] }, status: :bad_request
end
def parse_address(address)
Contact::Address.new(
address[:street],
address[:zip],
address[:city],
address[:state],
address[:country_code]
)
end
def render_address_error
error_msg = 'Address processing is disabled and therefore cannot be updated'
render json: { errors: [{ address: [error_msg] }] }, status: :bad_request
end
def render_fax_error
error_msg = 'Fax processing is disabled and therefore cannot be updated'
render json: { errors: [{ address: [error_msg] }] }, status: :bad_request
end
def update_and_notify!(contact)
contact.transaction do
contact.save!
action = current_registrant_user.actions.create!(contact: contact, operation: :update)
contact.registrar.notify(action)
end
contact
end
def reparsed_request(request_body)
reparsed_request = ActiveSupport::JSON.decode(request_body).with_indifferent_access
logger.debug 'Reparsed request is following'
logger.debug reparsed_request.to_s
reparsed_request
end
def find_contact_and_update_credentials(uuid, name, email, phone)
contact = current_user_contacts.find_by!(uuid: uuid)
contact.name = name if name.present?
contact.email = email if email.present?
contact.phone = phone if phone.present?
contact
end
end end
end end
end end

View file

@ -32,27 +32,40 @@ module Zone::WhoisQueryable
# Take note - since this concern only used to zone whois queries, dnssec keys are set to # Take note - since this concern only used to zone whois queries, dnssec keys are set to
# empty array # empty array
def domain_vars def domain_vars
{ disclaimer: Setting.registry_whois_disclaimer, name: origin, {
disclaimer: Setting.registry_whois_disclaimer, name: origin,
registered: created_at.try(:to_s, :iso8601), status: ['ok (paid and in zone)'], registered: created_at.try(:to_s, :iso8601), status: ['ok (paid and in zone)'],
changed: updated_at.try(:to_s, :iso8601), email: Setting.registry_email, changed: updated_at.try(:to_s, :iso8601), email: Setting.registry_email,
admin_contacts: [contact_vars], tech_contacts: [contact_vars], admin_contacts: [contact_vars], tech_contacts: [contact_vars],
nameservers: nameserver_vars, dnssec_keys: [], dnssec_changed: nil } nameservers: nameserver_vars, dnssec_keys: [],
dnssec_changed: nil
}
end end
def registrar_vars def registrar_vars
{ registrar: Setting.registry_juridical_name, registrar_website: Setting.registry_url, {
registrar_phone: Setting.registry_phone } registrar: Setting.registry_juridical_name,
registrar_website: Setting.registry_url,
registrar_phone: Setting.registry_phone,
}
end end
def registrant_vars def registrant_vars
{ registrant: Setting.registry_juridical_name, registrant_reg_no: Setting.registry_reg_no, {
registrant_ident_country_code: Setting.registry_country_code, registrant_kind: 'org', registrant: Setting.registry_juridical_name,
registrant_disclosed_attributes: %w[name email phone] } registrant_reg_no: Setting.registry_reg_no,
registrant_ident_country_code: Setting.registry_country_code,
registrant_kind: 'org',
registrant_disclosed_attributes: %w[name email phone],
}
end end
def contact_vars def contact_vars
{ name: Setting.registry_invoice_contact, email: Setting.registry_email, {
disclosed_attributes: %w[name email] } name: Setting.registry_invoice_contact,
email: Setting.registry_email,
disclosed_attributes: %w[name email],
}
end end
def nameserver_vars def nameserver_vars