# frozen_string_literal: true module EgovUtils module Iszr module NaturalPeople class CreateRequest < Request string :firstname string :lastname string :birth_date string :birth_place, default: nil private def body if birth_place.present? search_stage_two else search_stage_one end end def response_error_code @response_error_code ||= response_body.xpath('//VysledekSubKod').last&.text&.presence end def internal_error_code return unless response_error_code code = if response_error_code == 'ZAZNAM NENALEZEN' "not_found" elsif response_error_code == 'PREKROCEN POCET' "multiple_records_found" end if birth_place.present? "extended_search_#{code}" else code end end def handle_response! if internal_error_code errors.add(:base, internal_error_code) return end EgovUtils::Services::Iszr::NaturalPeople.new( firstname: response_body.xpath('//Jmeno').text.capitalize, lastname: response_body.xpath('//Prijmeni').text.capitalize, birth_date: response_body.xpath('//DatumNarozeni').text, birth_place: response_body.xpath('//Lokalita/Nazev').text, remote_id: response_body.xpath('//GlobalniAifo').text ) end def search_stage_one Nokogiri::XML::Builder.new do |xml| xml[service_id].RobCtiPodleUdaju2( 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation' => 'urn:cz:isvs:iszr:schemas:IszrRobCtiPodleUdaju2:v1 IszrRobCtiPodleUdaju2.xsd', 'xmlns:abs' => 'urn:cz:isvs:iszr:schemas:IszrAbstract:v1', 'xmlns:E278' => 'urn:cz:isvs:iszr:schemas:IszrRobCtiPodleUdaju2:v1', 'xmlns:reg' => 'urn:cz:isvs:reg:schemas:RegTypy:v1', 'xmlns:rod' => 'urn:cz:isvs:rob:schemas:RobDotazyData:v1', 'xmlns:rob' => 'urn:cz:isvs:rob:schemas:RobTypy:v1' ) { xml['abs'].ZadostInfo { xml['reg'].CasZadosti Time.current.iso8601 xml['reg'].Agenda EgovUtils::Settings['iszr']['agenda'] xml['reg'].AgendovaRole EgovUtils::Settings['iszr']['agendova_role'] xml['reg'].Ovm '00025429' xml['reg'].Ais EgovUtils::Settings['iszr']['ais'] xml['reg'].Subjekt '00025429' xml['reg'].Uzivatel 'Administrator' xml['reg'].DuvodUcel 'ztotozneni' xml['reg'].AgendaZadostId SecureRandom.uuid } xml['abs'].AutorizaceInfo { xml['abs'].SeznamUdaju 'Aifo Prijmeni Jmeno DatumNarozeni RUIANCti AdresaPobytu MistoNarozeni' } xml[service_id].Zadost { xml[service_id].RobCtiPodleUdaju2Data { xml['rod'].DatumNarozeni birth_date xml['rod'].Jmeno firstname xml['rod'].Prijmeni lastname } } } end end def search_stage_two Nokogiri::XML::Builder.new do |xml| xml[service_id].RobCtiPodleUdaju2( 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation' => 'urn:cz:isvs:iszr:schemas:IszrRobCtiPodleUdaju2:v1 IszrRobCtiPodleUdaju2.xsd', 'xmlns:abs' => 'urn:cz:isvs:iszr:schemas:IszrAbstract:v1', 'xmlns:E278' => 'urn:cz:isvs:iszr:schemas:IszrRobCtiPodleUdaju2:v1', 'xmlns:reg' => 'urn:cz:isvs:reg:schemas:RegTypy:v1', 'xmlns:rod' => 'urn:cz:isvs:rob:schemas:RobDotazyData:v1', 'xmlns:rob' => 'urn:cz:isvs:rob:schemas:RobTypy:v1' ) { xml['abs'].ZadostInfo { xml['reg'].CasZadosti Time.current.iso8601 xml['reg'].Agenda EgovUtils::Settings['iszr']['agenda'] xml['reg'].AgendovaRole EgovUtils::Settings['iszr']['agendova_role'] xml['reg'].Ovm '00025429' xml['reg'].Ais EgovUtils::Settings['iszr']['ais'] xml['reg'].Subjekt '00025429' xml['reg'].Uzivatel 'Administrator' xml['reg'].DuvodUcel 'ztotozneni' xml['reg'].AgendaZadostId SecureRandom.uuid } xml['abs'].AutorizaceInfo { xml['abs'].SeznamUdaju 'Aifo Prijmeni Jmeno DatumNarozeni RUIANCti AdresaPobytu MistoNarozeni' } xml[service_id].Zadost { xml[service_id].RobCtiPodleUdaju2Data { xml['rod'].DatumNarozeni birth_date xml['rod'].Jmeno firstname xml['rod'].MistoNarozeni { xml['rob'].NarozeniCr birth_place } xml['rod'].Prijmeni lastname } } } end end def service_id 'E278' end end end end end