2019-07-25 18:09:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ImportPerson
|
|
|
|
include Interactor
|
|
|
|
|
|
|
|
def call
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
create_person
|
2019-07-25 18:18:10 -04:00
|
|
|
create_general_comments_person_comment
|
2019-07-25 18:30:17 -04:00
|
|
|
create_first_contact_date_person_comment
|
2019-07-25 18:09:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_person
|
2019-07-25 18:35:33 -04:00
|
|
|
context.person = Person.where(id: person_id).lock(true).first_or_create!(
|
|
|
|
person_attributes.reverse_merge(
|
|
|
|
contacts_list: ContactsList.new,
|
|
|
|
),
|
|
|
|
)
|
2019-07-25 18:09:55 -04:00
|
|
|
end
|
|
|
|
|
2019-07-25 18:40:04 -04:00
|
|
|
# rubocop:disable Metrics/AbcSize
|
|
|
|
|
2019-07-25 18:18:10 -04:00
|
|
|
def create_general_comments_person_comment
|
2019-07-25 18:35:33 -04:00
|
|
|
return if general_comments.blank?
|
2019-07-25 18:18:10 -04:00
|
|
|
|
|
|
|
context.general_comments_person_comment =
|
|
|
|
context.person.person_comments.where(origin: :general_comments)
|
2019-07-25 18:40:04 -04:00
|
|
|
.lock(true).first_or_initialize
|
2019-07-25 18:18:10 -04:00
|
|
|
|
2019-07-25 18:35:33 -04:00
|
|
|
context.general_comments_person_comment.text = general_comments
|
2019-07-25 18:18:10 -04:00
|
|
|
|
|
|
|
context.general_comments_person_comment.save!
|
|
|
|
end
|
|
|
|
|
2019-07-25 18:30:17 -04:00
|
|
|
def create_first_contact_date_person_comment
|
2019-07-25 18:35:33 -04:00
|
|
|
return if first_contact_date.blank?
|
2019-07-25 18:30:17 -04:00
|
|
|
|
|
|
|
context.first_contact_date_person_comment =
|
|
|
|
context.person.person_comments.where(origin: :first_contact_date)
|
2019-07-25 18:40:04 -04:00
|
|
|
.lock(true).first_or_initialize
|
2019-07-25 18:30:17 -04:00
|
|
|
|
2019-07-25 18:35:33 -04:00
|
|
|
context.first_contact_date_person_comment.text = first_contact_date
|
2019-07-25 18:30:17 -04:00
|
|
|
|
|
|
|
context.first_contact_date_person_comment.save!
|
|
|
|
end
|
|
|
|
|
2019-07-25 18:40:04 -04:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
|
|
|
|
2019-07-25 18:35:33 -04:00
|
|
|
def person_id
|
|
|
|
context.row[0]
|
|
|
|
end
|
|
|
|
|
2019-07-25 18:09:55 -04:00
|
|
|
def person_attributes
|
|
|
|
{
|
2019-07-25 18:35:33 -04:00
|
|
|
last_name: context.row[2],
|
|
|
|
first_name: context.row[1],
|
|
|
|
middle_name: context.row[3],
|
|
|
|
sex: nil,
|
|
|
|
date_of_birth: context.row[8],
|
|
|
|
place_of_birth: context.row[9],
|
2019-07-25 18:09:55 -04:00
|
|
|
}
|
|
|
|
end
|
2019-07-25 18:35:33 -04:00
|
|
|
|
|
|
|
def general_comments
|
|
|
|
context.row[5]
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_contact_date
|
|
|
|
context.row[6]
|
|
|
|
end
|
2019-07-25 18:09:55 -04:00
|
|
|
end
|