From 942cfe82b5336807d1380eb44fb1fd90954366e4 Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Mon, 25 Jul 2022 05:19:27 -0300 Subject: [PATCH] DRY registrant contact update action --- .../api/v1/registrant/contacts_controller.rb | 108 +++++++++++------- app/models/concerns/zone/whois_queryable.rb | 31 +++-- 2 files changed, 87 insertions(+), 52 deletions(-) diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index 867eaf881..e7270e9c3 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -48,62 +48,31 @@ module Api def update logger.debug 'Received update request' logger.debug params - contact = current_user_contacts.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? + contact = find_contact_and_update_credentials(params[:uuid], params[:name], params[:email], params[:phone]) - # Needed to support passing empty array, which otherwise gets parsed to nil - # 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] + reparsed_request = reparsed_request(request.body.string) - if 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 + disclosed_attributes = reparsed_request[:disclosed_attributes] - contact.disclosed_attributes = disclosed_attributes - end + render_disclosed_attributes_error and return if disclosed_attributes.present? && contact.org? && + !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] logger.debug "Setting.address_processing is set to #{Setting.address_processing}" - 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 + contact.address = parse_address(params[:address]) if Setting.address_processing && params[:address] + render_address_error and return if !Setting.address_processing && params[:address] contact.fax = params[:fax] if ENV['fax_enabled'] == 'true' && params[:fax].present? logger.debug "ENV['fax_enabled'] is set to #{ENV['fax_enabled']}" - 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 + render_fax_error and return if ENV['fax_enabled'] != 'true' && params[:fax] - contact.transaction do - contact.save! - action = current_registrant_user.actions.create!(contact: contact, operation: :update) - contact.registrar.notify(action) - end + contact = update_and_notify!(contact) render json: serialize_contact(contact, false) end @@ -139,6 +108,59 @@ module Api def logger Rails.logger 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 diff --git a/app/models/concerns/zone/whois_queryable.rb b/app/models/concerns/zone/whois_queryable.rb index 1a08b7ab1..fe5da8ffb 100644 --- a/app/models/concerns/zone/whois_queryable.rb +++ b/app/models/concerns/zone/whois_queryable.rb @@ -32,27 +32,40 @@ module Zone::WhoisQueryable # Take note - since this concern only used to zone whois queries, dnssec keys are set to # empty array 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)'], changed: updated_at.try(:to_s, :iso8601), email: Setting.registry_email, 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 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 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_disclosed_attributes: %w[name email phone] } + { + registrant: Setting.registry_juridical_name, + 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 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 def nameserver_vars