From 7eabf4bf01a581ba17b8b1c373911e957bca8911 Mon Sep 17 00:00:00 2001 From: Sai Praveen Gudimetla Date: Mon, 29 Jul 2024 10:15:52 -0500 Subject: [PATCH] Map incarceration status of applicant in ATP inbound process (#4063) * Map incarceration status of applicant in ATP inbound process * fix rubocop issues * update aca entities reference --- Gemfile.lock | 2 +- .../operations/people/create_or_update.rb | 42 ++++++++---- .../medicaid_gateway/account_transfer_in.rb | 2 +- .../account_transfer_in_spec.rb | 64 +++++++++++++++++++ .../people/create_or_update_spec.rb | 12 ++++ 5 files changed, 108 insertions(+), 14 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f2422dd7f35..3ee7d7bf61e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,7 +7,7 @@ GIT GIT remote: https://github.com/ideacrew/aca_entities.git - revision: 11a86c60092ba59f512cf117dc330614e66f8815 + revision: 76f664dce913d31767227aa39221aaa0ebee31a2 branch: trunk specs: aca_entities (0.10.0) diff --git a/app/domain/operations/people/create_or_update.rb b/app/domain/operations/people/create_or_update.rb index d7bafd57a5c..b30ec1ac007 100644 --- a/app/domain/operations/people/create_or_update.rb +++ b/app/domain/operations/people/create_or_update.rb @@ -44,22 +44,40 @@ def create_entity(values) def match_or_update_or_create_person(person_entity) person = find_existing_person(person_entity.to_h) #create new person - if person.blank? - person = Person.new(person_entity.to_h) #if person_valid_params.success? - person.save! - else - return Success(person) if no_infomation_changed?(params: { attributes_hash: person_entity, person: person }) - person.assign_attributes(person_entity.except(:addresses, :phones, :emails, :hbx_id)) - person.save! - create_or_update_associations(person, person_entity.to_h, :addresses) - create_or_update_associations(person, person_entity.to_h, :emails) - create_or_update_associations(person, person_entity.to_h, :phones) - end - Success(person) + person_record = if person.blank? + create_new_person(person_entity) + else + return Success(person) if no_infomation_changed?(params: { attributes_hash: person_entity, person: person }) + update_existing_person(person, person_entity) + end + Success(person_record) rescue StandardError => e Failure(person.errors.messages) end + def create_new_person(person_entity) + person_params = person_entity.to_h + person_params[:is_incarcerated] = person_params[:is_incarcerated].nil? ? false : person_params[:is_incarcerated] + + person = Person.new(person_params) + person.save! + + person + end + + def update_existing_person(person, person_entity) + attributes_to_exclude = %i[addresses phones emails hbx_id] + attributes_to_exclude << :is_incarcerated if person_entity.to_h[:is_incarcerated].nil? + person.assign_attributes(person_entity.except(*attributes_to_exclude)) + person.save! + + %i[addresses emails phones].each do |association| + create_or_update_associations(person, person_entity.to_h, association) + end + + person + end + def create_or_update_associations(person, applicant_params, assoc) records = applicant_params[assoc.to_sym] return if records.empty? diff --git a/components/financial_assistance/app/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in.rb b/components/financial_assistance/app/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in.rb index c7f5325c6ae..593b92e1b9c 100644 --- a/components/financial_assistance/app/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in.rb +++ b/components/financial_assistance/app/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in.rb @@ -344,7 +344,7 @@ def sanitize_applicant_params(iap_hash, primary) is_veteran_or_active_military: applicant_hash['demographic']['is_veteran_or_active_military'], is_vets_spouse_or_child: applicant_hash['demographic']['is_vets_spouse_or_child'], same_with_primary: address_result.value!, - is_incarcerated: applicant_hash["is_incarcerated"], + is_incarcerated: family_member.person.is_incarcerated, is_physically_disabled: applicant_hash['attestation']['is_self_attested_disabled'], is_self_attested_disabled: applicant_hash['attestation']['is_self_attested_disabled'], is_self_attested_blind: applicant_hash['attestation']['is_self_attested_blind'], diff --git a/components/financial_assistance/spec/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in_spec.rb b/components/financial_assistance/spec/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in_spec.rb index 9e242ce0e8e..8098e5846f1 100644 --- a/components/financial_assistance/spec/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in_spec.rb +++ b/components/financial_assistance/spec/domain/financial_assistance/operations/transfers/medicaid_gateway/account_transfer_in_spec.rb @@ -230,6 +230,70 @@ end end + context 'Incarceration status' do + context "when incarceration indicator does not exists for any applicant" do + let(:document) do + document = Nokogiri::XML(xml) + document.xpath('//hix-ee:InsuranceApplicantIncarceration', 'hix-ee' => "http://hix.cms.gov/0.1/hix-ee").each(&:remove) + document + end + + it "should default the person/applicant incarceration status to false" do + record = serializer.parse(document.root.canonicalize, :single => true) + @transformed = transformer.transform(record.to_hash(identifier: true)) + @result = subject.call(@transformed) + app = FinancialAssistance::Application.find(@result.value!) + expect(app.applicants.first.is_incarcerated).to eq false + expect(Person.all.first.is_incarcerated).to eq false + end + end + + context "when incarceration indicator is set to true for all applicants" do + let(:document) do + document = Nokogiri::XML(xml) + document.xpath('//ns4:IncarcerationIndicator', 'ns4' => "http://hix.cms.gov/0.1/hix-core").each do |node| + node.content = 'true' + end + document + end + + it "should default the person/applicant incarceration status to false" do + record = serializer.parse(document.root.canonicalize, :single => true) + @transformed = transformer.transform(record.to_hash(identifier: true)) + @result = subject.call(@transformed) + app = FinancialAssistance::Application.find(@result.value!) + expect(app.applicants.first.is_incarcerated).to eq true + expect(Person.all.first.is_incarcerated).to eq true + end + end + + context "when person records exits and the payload has incarceration set to nil in the payload" do + let!(:person) do + FactoryBot.create(:person, first_name: "Junior", last_name: "Banfield", + dob: Date.new(2014,1,1), is_incarcerated: true) + end + + let(:document) do + document = Nokogiri::XML(xml) + document.xpath('//ns4:IncarcerationIndicator', 'ns4' => "http://hix.cms.gov/0.1/hix-core")[1].remove + document + end + + it "should default the incarceration status to existing person value" do + record = serializer.parse(document.root.canonicalize, :single => true) + @transformed = transformer.transform(record.to_hash(identifier: true)) + @result = subject.call(@transformed) + family_member = @transformed["family"]["family_members"].detect do |member| + member if member["person"]["person_name"]["first_name"] == person.first_name + end + matching_person = Person.all.where(first_name: "Junior").first + expect(matching_person).to be_present + expect(matching_person.is_incarcerated).to eq true + expect(family_member["person"]["person_demographics"]["is_incarcerated"]).not_to eq matching_person.is_incarcerated + end + end + end + context 'failure' do context 'load_county_on_inbound_transfer feature is enabled' do context 'with no counties loaded in database' do diff --git a/spec/domain/operations/people/create_or_update_spec.rb b/spec/domain/operations/people/create_or_update_spec.rb index e706ae725db..32b64849a8e 100644 --- a/spec/domain/operations/people/create_or_update_spec.rb +++ b/spec/domain/operations/people/create_or_update_spec.rb @@ -71,6 +71,18 @@ expect(@result).to be_a(Dry::Monads::Result::Success) end end + + context 'nil incarceration status' do + before do + person_params.merge!({is_incarcerated: nil}) + @result = subject.call(params: person_params) + end + + it 'should return success' do + expect(@result).to be_a(Dry::Monads::Result::Success) + expect(@result.value!.is_incarcerated).to eq false + end + end end context 'for failed case' do