From 82a9565c1013d84f26bd76dc42a78e4814d7fec9 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Fri, 26 Jun 2020 14:31:12 +0300 Subject: [PATCH] Extract action --- app/controllers/epp/contacts_controller.rb | 11 ++- app/models/actions/contact_update.rb | 105 +++++++++++++++++++++ app/models/epp/contact.rb | 62 ------------ lib/deserializers/xml/contact_update.rb | 27 ++++++ 4 files changed, 141 insertions(+), 64 deletions(-) create mode 100644 app/models/actions/contact_update.rb create mode 100644 lib/deserializers/xml/contact_update.rb diff --git a/app/controllers/epp/contacts_controller.rb b/app/controllers/epp/contacts_controller.rb index b6a26a626..df9755af6 100644 --- a/app/controllers/epp/contacts_controller.rb +++ b/app/controllers/epp/contacts_controller.rb @@ -1,3 +1,5 @@ +require 'deserializers/xml/contact_update' + module Epp class ContactsController < BaseController before_action :find_contact, only: [:info, :update, :delete] @@ -43,9 +45,14 @@ module Epp def update authorize! :update, @contact, @password - frame = params[:parsed_frame] + collected_data = ::Deserializers::Xml::ContactUpdate.new(params[:parsed_frame]) + action = Actions::ContactUpdate.new(@contact, + collected_data.contact, + collected_data.legal_document, + collected_data.ident, + current_user) - if @contact.update_attributes(frame, current_user) + if action.call if !address_processing? && address_given? @response_code = 1100 @response_description = t('epp.contacts.completed_without_address') diff --git a/app/models/actions/contact_update.rb b/app/models/actions/contact_update.rb new file mode 100644 index 000000000..6b237ca65 --- /dev/null +++ b/app/models/actions/contact_update.rb @@ -0,0 +1,105 @@ +module Actions + class ContactUpdate + attr_reader :contact + attr_reader :new_attributes + attr_reader :legal_document + attr_reader :ident + attr_reader :user + + def initialize(contact, new_attributes, legal_document, ident, user) + @contact = contact + @new_attributes = new_attributes + @legal_document = legal_document + @ident = ident + @user = user + end + + def call + maybe_remove_address + maybe_update_statuses + maybe_update_ident + maybe_attach_legal_doc + commit + end + + def maybe_remove_address + return if Setting.address_processing? + + new_attributes.delete(:city) + new_attributes.delete(:zip) + new_attributes.delete(:street) + new_attributes.delete(:state) + new_attributes.delete(:country_code) + end + + def maybe_update_statuses + return unless Setting.client_status_editing_enabled + + new_statuses = + contact.statuses - new_attributes[:statuses_to_remove] + new_attributes[:statuses_to_add] + + new_attributes[:statuses] = new_statuses + end + + def maybe_attach_legal_doc + return unless legal_document + + document = contact.legal_documents.create( + document_type: legal_document[:type], + body: legal_document[:body] + ) + + contact.legal_document_id = document.id + end + + def maybe_update_ident + if ident[:ident] + if contact.identifier.valid? + submitted_ident = ::Contact::Ident.new(code: ident[:ident], + type: ident[:ident_type], + country_code: ident[:ident_country_code]) + + if submitted_ident != contact.identifier + contact.add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident')) + @error = true + end + else + ident_update_attempt = ident[:ident] != contact.ident + + if ident_update_attempt + contact.add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.ident_update')) + @error = true + end + + identifier = ::Contact::Ident.new(code: ident[:ident], + type: ident[:ident_type], + country_code: ident[:ident_country_code]) + + identifier.validate + + contact.identifier = identifier + contact.ident_updated_at ||= Time.zone.now + end + end + end + + def commit + return false if @error + + contact.upid = user.registrar&.id + contact.up_date = Time.zone.now + + contact.attributes = new_attributes + + email_changed = contact.will_save_change_to_email? + old_email = contact.email_was + updated = contact.save + + if updated && email_changed && contact.registrant? + ContactMailer.email_changed(contact: contact, old_email: old_email).deliver_now + end + + updated + end + end +end diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index c86ed77da..bb2803980 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -88,68 +88,6 @@ class Epp::Contact < Contact } end - def update_attributes(frame, current_user) - return super if frame.blank? - new_attributes = self.class.attrs_from(frame, new_record: false) - - if Setting.client_status_editing_enabled - new_attributes[:statuses] = - statuses - new_attributes[:statuses_to_remove] + new_attributes[:statuses_to_add] - end - - if doc = attach_legal_document(::Deserializers::Xml::LegalDocument.new(frame).call) - frame.css("legalDocument").first.content = doc.path if doc&.persisted? - self.legal_document_id = doc.id - end - - ident_attributes = ::Deserializers::Xml::Ident.new(frame).call - - # https://github.com/internetee/registry/issues/576 - if ident_attributes[:ident] - if identifier.valid? - submitted_ident = Ident.new(code: ident_attributes[:ident], - type: ident_attributes[:ident_type], - country_code: ident_attributes[:ident_country_code]) - - if submitted_ident != identifier - add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident')) - return - end - else - ident_update_attempt = ident_attributes[:ident] != ident - - if ident_update_attempt - add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.ident_update')) - return - end - - identifier = Ident.new(code: ident_attributes[:ident], - type: ident_attributes[:ident_type], - country_code: ident_attributes[:ident_country_code]) - - identifier.validate - - self.identifier = identifier - self.ident_updated_at ||= Time.zone.now - end - end - - self.upid = current_user.registrar.id if current_user.registrar - self.up_date = Time.zone.now - - self.attributes = new_attributes - - email_changed = will_save_change_to_email? - old_email = email_was - updated = save - - if updated && email_changed && registrant? - ContactMailer.email_changed(contact: self, old_email: old_email).deliver_now - end - - updated - end - def statuses_attrs(frame, action) status_list = status_list_from(frame) diff --git a/lib/deserializers/xml/contact_update.rb b/lib/deserializers/xml/contact_update.rb new file mode 100644 index 000000000..b3bc6fe4a --- /dev/null +++ b/lib/deserializers/xml/contact_update.rb @@ -0,0 +1,27 @@ +require 'deserializers/xml/legal_document' +require 'deserializers/xml/ident' +require 'deserializers/xml/contact' + +module Deserializers + module Xml + class ContactUpdate + attr_reader :frame + + def initialize(frame) + @frame = frame + end + + def contact + @contact ||= ::Deserializers::Xml::Contact.new(frame).call + end + + def ident + @ident ||= ::Deserializers::Xml::Ident.new(frame).call + end + + def legal_document + @legal_document ||= ::Deserializers::Xml::LegalDocument.new(frame).call + end + end + end +end