diff --git a/app/controllers/epp/contacts_controller.rb b/app/controllers/epp/contacts_controller.rb index 5b0a39bbf..14c6de7b6 100644 --- a/app/controllers/epp/contacts_controller.rb +++ b/app/controllers/epp/contacts_controller.rb @@ -19,6 +19,8 @@ class Epp::ContactsController < EppController authorize! :create, Epp::Contact @contact = Epp::Contact.new(params[:parsed_frame], current_user.registrar) + @contact.add_legal_file_to_new(params[:parsed_frame]) + if @contact.save render_epp_response '/epp/contacts/create' else diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index 91ddeb93d..fb3441580 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -30,6 +30,8 @@ class Epp::DomainsController < EppController handle_errors and return unless balance_ok?('create') # loads pricelist in this method ActiveRecord::Base.transaction do + @domain.add_legal_file_to_new(params[:parsed_frame]) + if @domain.save # TODO: Maybe use validate: false here because we have already validated the domain? current_user.registrar.debit!({ sum: @domain_pricelist.price.amount, diff --git a/app/models/contact.rb b/app/models/contact.rb index fdb114673..5ec9c0a46 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -12,6 +12,10 @@ class Contact < ActiveRecord::Base # TODO: remove later has_many :depricated_statuses, class_name: 'DepricatedContactStatus', dependent: :destroy + has_paper_trail class_name: "ContactVersion", meta: { children: :children_log } + + attr_accessor :legal_document_id + accepts_nested_attributes_for :legal_documents validates :name, :phone, :email, :ident, :ident_type, @@ -497,8 +501,14 @@ class Contact < ActiveRecord::Base ]).present? end - def update_related_whois_records - related_domain_descriptions.each{ |x, y| WhoisRecord.find_by(name: x).try(:save) } - end + def update_related_whois_records + related_domain_descriptions.each{ |x, y| WhoisRecord.find_by(name: x).try(:save) } + end + + def children_log + log = HashWithIndifferentAccess.new + log[:legal_documents]= [legal_document_id] + log + end end diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index f4773f732..a3180a534 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -36,10 +36,7 @@ class Epp::Contact < Contact at[:country_code] = f.css('postalInfo addr cc').text if f.css('postalInfo addr cc').present? at[:auth_info] = f.css('authInfo pw').text if f.css('authInfo pw').present? - legal_frame = f.css('legalDocument').first - if legal_frame.present? - at[:legal_documents_attributes] = legal_document_attrs(legal_frame) - end + at.merge!(ident_attrs(f.css('ident').first)) if new_record at end @@ -152,6 +149,12 @@ class Epp::Contact < Contact legal_frame = frame.css('legalDocument').first at[:legal_documents_attributes] = self.class.legal_document_attrs(legal_frame) + + if doc = attach_legal_document(parse_legal_document_from_frame(frame)) + frame.css("legalDocument").first.content = doc.path if doc && doc.persisted? + self.legal_document_id = doc.id + end + self.deliver_emails = true # turn on email delivery for epp @@ -213,4 +216,34 @@ class Epp::Contact < Contact status_list end + + def attach_legal_document(legal_document_data) + return unless legal_document_data + + legal_documents.create( + document_type: legal_document_data[:type], + body: legal_document_data[:body] + ) + end + + def add_legal_file_to_new frame + if doc = attach_legal_document(parse_legal_document_from_frame(frame)) + raise ActiveRecord::Rollback if doc && doc.id.nil? + + frame.css("legalDocument").first.content = doc.path if doc && doc.persisted? + self.legal_document_id = doc.id + end + end + + def parse_legal_document_from_frame frame + ld = frame.css('legalDocument').first + return nil unless ld + return nil if ld.text.starts_with?(ENV['legal_documents_dir']) + + { + body: ld.text, + type: ld['type'] + } + end + end diff --git a/app/models/epp/domain.rb b/app/models/epp/domain.rb index 6cda7712f..d0f83fd9f 100644 --- a/app/models/epp/domain.rb +++ b/app/models/epp/domain.rb @@ -194,9 +194,21 @@ class Epp::Domain < Domain end at[:dnskeys_attributes] = dnskeys_attrs(dnskey_frame, action) - at[:legal_documents_attributes] = legal_document_from(frame) + at end + + + # Adding legal doc to domain and + # if something goes wrong - raise Rollback error + def add_legal_file_to_new frame + if doc = attach_legal_document(Epp::Domain.parse_legal_document_from_frame(frame)) + raise ActiveRecord::Rollback if doc && doc.id.nil? + + frame.css("legalDocument").first.content = doc.path if doc && doc.persisted? + self.legal_document_id = doc.id + end + end # rubocop: enable Metrics/PerceivedComplexity # rubocop: enable Metrics/CyclomaticComplexity # rubocop: enable Metrics/MethodLength @@ -456,15 +468,6 @@ class Epp::Domain < Domain status_list end - def legal_document_from(frame) - ld = frame.css('legalDocument').first - return [] unless ld - - [{ - body: ld.text, - document_type: ld['type'] - }] - end # rubocop: disable Metrics/AbcSize # rubocop: disable Metrics/CyclomaticComplexity