Repp: Contact create: validate identifier

This commit is contained in:
Karl Erik Õunapuu 2020-10-07 12:46:19 +03:00
parent d4628d52ba
commit 01004b8ed4
No known key found for this signature in database
GPG key ID: C9DD647298A34764
5 changed files with 67 additions and 19 deletions

View file

@ -34,18 +34,29 @@ module Repp
desc 'Creates a new contact object'
params do
requires :contact, type: Hash, allow_blank: false do
# Contact info
requires :name, type: String, desc: 'Full name of contact'
requires :ident, type: String, desc: 'Government identifier of contact'
requires :ident_type, type: String, desc: 'Type of contact ident'
requires :ident_country_code, type: String, desc: 'Ident country code'
requires :country_code, type: String, desc: 'Address country'
requires :phone, type: String, desc: 'Phone number of contact. In format of +country_prefix.number'
requires :email, type: String, desc: 'Email address of contact'
optional :fax, type: String, desc: 'Fax number of contact'
optional :street, type: String, desc: 'Address street'
optional :city, type: String, desc: 'Address city'
optional :zip, type: String, desc: 'Address ZIP'
optional :fax, type: String, allow_blank: true, desc: 'Fax number of contact'
# Ident
requires :ident, type: Hash do
requires :ident, type: String, allow_blank: false, desc: 'Government identifier of contact'
requires :ident_type, type: String, allow_blank: false, desc: 'Type of contact ident'
requires :ident_country_code, type: String, allow_blank: false, desc: 'Ident country code'
end
# Physical address
optional :addr, type: Hash do
requires :country_code, type: String, allow_blank: false, desc: 'Address country'
requires :street, type: String, allow_blank: false, desc: 'Address street'
requires :city, type: String, allow_blank: false, desc: 'Address city'
requires :zip, type: String, allow_blank: false, desc: 'Address ZIP'
end
end
# Legal document
optional :legal_document, type: Hash, allow_blank: false do
requires :body, type: String, desc: 'Raw data of legal document'
requires :type, type: String, desc: 'Format of legal document'
@ -54,9 +65,20 @@ module Repp
post '/' do
@legal_doc = params[:legal_documents]
@contact = Contact.new(params[:contact])
@contact.registrar = current_user.registrar
action = Actions::ContactCreate.new(@contact, @legal_doc)
@contact_params = params[:contact]
# Ident object
@ident = @contact_params[:ident]
@contact_params.delete(:ident)
# Address
%w[city street zip country_code].each { |k| @contact_params[k] = @contact_params[:addr][k] }
@contact_params.delete(:addr)
@contact = Epp::Contact.new(@contact_params, current_user.registrar, epp: false)
puts "#{params[:contact]}"
action = Actions::ContactCreate.new(@contact, @legal_doc, @ident)
if action.call
@response = { contact: { id: @contact.code } }
@ -89,7 +111,7 @@ module Repp
end
end
post '/:code' do
put '/:code' do
@contact = current_user.registrar.contacts.find_by(code: params[:code])
(status(:not_found) && return) unless @contact

View file

@ -25,7 +25,7 @@ module Epp
@contact = Epp::Contact.new(params[:parsed_frame], current_user.registrar)
collected_data = ::Deserializers::Xml::ContactCreate.new(params[:parsed_frame])
action = Actions::ContactCreate.new(@contact, collected_data.legal_document)
action = Actions::ContactCreate.new(@contact, collected_data.legal_document, collected_data.ident)
if action.call
if !address_processing? && address_given?

View file

@ -1,15 +1,17 @@
module Actions
class ContactCreate
attr_reader :contact, :legal_document
attr_reader :contact, :legal_document, :ident
def initialize(contact, legal_document)
def initialize(contact, legal_document, ident)
@contact = contact
@legal_document = legal_document
@ident = ident
end
def call
maybe_remove_address
maybe_attach_legal_doc
validate_ident
commit
end
@ -23,6 +25,24 @@ module Actions
contact.country_code = nil
end
def validate_ident
if ident.present? && ident[:ident_type].blank?
contact.add_epp_error('2003', nil, 'ident_type', I18n.t('errors.messages.required_ident_attribute_missing'))
@error = true
end
if ident.present? && ident[:ident_type] != 'birthday' && ident[:ident_country_code].blank?
contact.add_epp_error('2003', nil, 'ident_country_code', I18n.t('errors.messages.required_ident_attribute_missing'))
@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
end
def maybe_attach_legal_doc
return unless legal_document
@ -36,8 +56,9 @@ module Actions
end
def commit
contact.generate_code
return false if @error
contact.generate_code
contact.save
end
end

View file

@ -30,12 +30,13 @@ class Epp::Contact < Contact
at
end
def new(frame, registrar)
def new(frame, registrar, epp: true)
return super if frame.blank?
attrs = epp ? attrs_from(frame, new_record: true) : frame
super(
attrs_from(frame, new_record: true).merge(
code: frame.css('id').text,
attrs.merge(
code: epp ? frame.css('id').text : frame[:id],
registrar: registrar
)
)

View file

@ -18,6 +18,10 @@ module Deserializers
def legal_document
@legal_document ||= ::Deserializers::Xml::LegalDocument.new(frame).call
end
def ident
@ident ||= ::Deserializers::Xml::Ident.new(frame).call
end
end
end
end