diff --git a/app/models/data_stores/contract_holder_subject.rb b/app/models/data_stores/contract_holder_subject.rb index 5984c6b4..f79e401b 100644 --- a/app/models/data_stores/contract_holder_subject.rb +++ b/app/models/data_stores/contract_holder_subject.rb @@ -16,9 +16,12 @@ class ContractHolderSubject index({ primary_person_hbx_id: 1 }) index({ status: 1 }) - index({ 'request_event.timestamp': 1 }, { name: "request_event_timestamp_index" }) - index({ 'response_event.timestamp': 1 }, { name: "response_event_timestamp_index" }) - index({ 'transmit_events.timestamp': 1 }, { name: "transmit_events_timestamp_index" }) + index({ request_event: 1 }, { name: "request_event" }) + index({ response_event: 1 }, { name: "response_event" }) + index({ transmit_events: 1 }, { name: "transmit_events" }) + index({ "response_event.body" => 1 }) + index({ "response_event.status" => 1 }) + index({ contract_holder_sync_id: 1, "response_event.body" => 1, "response_event.status" => 1 }) scope :by_primary_hbx_id, ->(hbx_id) { where(primary_person_hbx_id: hbx_id) } diff --git a/app/models/insurance_policies/aca_individuals/insurance_policy.rb b/app/models/insurance_policies/aca_individuals/insurance_policy.rb index e776b9ca..2e9212c2 100644 --- a/app/models/insurance_policies/aca_individuals/insurance_policy.rb +++ b/app/models/insurance_policies/aca_individuals/insurance_policy.rb @@ -202,10 +202,10 @@ def fetch_slcsp_from_tax_household(tax_household, enr_thhs) return 0.0 unless enr_thhs.any? { |enr_thh| enr_thh.tax_household.is_aqhp == true } tax_filer = fetch_tax_filer(tax_household) - enr_thh_for_month = enr_thhs.detect do |enr_thh| + enr_thh_for_month = enr_thhs.select do |enr_thh| enr_thh.tax_household.is_aqhp && enr_thh.tax_household.tax_household_members.map(&:person_id).include?(tax_filer&.person_id) - end + end.max_by { |enr_thh| enr_thh.enrolled_members_tax_household_members.count } return 0.0 if enr_thh_for_month.blank? diff --git a/spec/models/insurance_policies/aca_individuals/insurance_policy_spec.rb b/spec/models/insurance_policies/aca_individuals/insurance_policy_spec.rb index 79e4179c..da6f063b 100644 --- a/spec/models/insurance_policies/aca_individuals/insurance_policy_spec.rb +++ b/spec/models/insurance_policies/aca_individuals/insurance_policy_spec.rb @@ -367,5 +367,125 @@ aqhp_enrollment_tax_household.applied_aptc)).to eq "400.00" end end + + context "when there is a dependent add mid term" do + let(:enrollment_subscriber) { FactoryBot.build(:enrolled_member, person: subscriber_person) } + let(:enrollment_dependent) { FactoryBot.build(:enrolled_member, person: dependent_person) } + let!(:enrollment_1) do + FactoryBot.create(:enrollment, start_on: Date.new(year, 1, 1), + effectuated_on: Date.new(year, 1, 1), + end_on: Date.new(year, 1, 23), + insurance_policy: insurance_policy, + subscriber: enrollment_subscriber) + end + let!(:enrollment_2) do + FactoryBot.create(:enrollment, start_on: Date.new(year, 1, 24), + effectuated_on: Date.new(year, 1, 24), + end_on: Date.new(year, 1, 31), + insurance_policy: insurance_policy, + subscriber: enrollment_subscriber, + dependents: [enrollment_dependent]) + end + + let!(:aqhp_tax_household_1) { FactoryBot.create(:tax_household, is_aqhp: true) } + let!(:aqhp_tax_household_2) { FactoryBot.create(:tax_household, is_aqhp: true) } + let!(:aqhp_thh_sub_tax_household_member) do + FactoryBot.create(:tax_household_member, tax_household: aqhp_tax_household_1, person: subscriber_person, + is_tax_filer: true) + end + let!(:aqhp_thh_sub_tax_household_member_2) do + FactoryBot.create(:tax_household_member, tax_household: aqhp_tax_household_2, person: subscriber_person, + is_tax_filer: true) + end + let!(:aqhp_thh_sub_tax_household_member_3) do + FactoryBot.create(:tax_household_member, tax_household: aqhp_tax_household_2, person: dependent_person, + is_tax_filer: false) + end + let!(:aqhp_enrollment_tax_household_1) do + FactoryBot.create(:enrollments_tax_households, enrollment_id: enrollment_1.id, + tax_household_id: aqhp_tax_household_1.id, + household_benchmark_ehb_premium: 400.0) + end + let!(:enr_members_thhm_1) do + FactoryBot.create(:enrolled_members_tax_household_members, enrollments_tax_households: aqhp_enrollment_tax_household_1) + end + + let!(:aqhp_enrollment_tax_household_2) do + FactoryBot.create(:enrollments_tax_households, enrollment_id: enrollment_2.id, + tax_household_id: aqhp_tax_household_2.id, + household_benchmark_ehb_premium: 500.0) + end + let!(:enr_members_thhm_2) do + FactoryBot.create(:enrolled_members_tax_household_members, enrollments_tax_households: aqhp_enrollment_tax_household_2) + end + let!(:enr_members_thhm_3) do + FactoryBot.create(:enrolled_members_tax_household_members, enrollments_tax_households: aqhp_enrollment_tax_household_2) + end + + it "should return member added slcsp premium" do + expect(insurance_policy.fetch_slcsp_premium([enrollment_1, enrollment_2], 1, aqhp_tax_household_2, + aqhp_enrollment_tax_household_2.applied_aptc)).to eq "500.00" + end + end + + context "when there is a dependent drop mid term" do + let(:enrollment_subscriber) { FactoryBot.build(:enrolled_member, person: subscriber_person) } + let(:enrollment_dependent) { FactoryBot.build(:enrolled_member, person: dependent_person) } + let!(:enrollment_1) do + FactoryBot.create(:enrollment, start_on: Date.new(year, 1, 1), + effectuated_on: Date.new(year, 1, 1), + end_on: Date.new(year, 1, 23), + insurance_policy: insurance_policy, + subscriber: enrollment_subscriber, + dependents: [enrollment_dependent]) + end + let!(:enrollment_2) do + FactoryBot.create(:enrollment, start_on: Date.new(year, 1, 24), + effectuated_on: Date.new(year, 1, 24), + end_on: Date.new(year, 1, 31), + insurance_policy: insurance_policy, + subscriber: enrollment_subscriber) + end + + let!(:aqhp_tax_household_1) { FactoryBot.create(:tax_household, is_aqhp: true) } + let!(:aqhp_tax_household_2) { FactoryBot.create(:tax_household, is_aqhp: true) } + let!(:aqhp_thh_sub_tax_household_member) do + FactoryBot.create(:tax_household_member, tax_household: aqhp_tax_household_1, person: subscriber_person, + is_tax_filer: true) + end + let!(:aqhp_thh_sub_tax_household_member_2) do + FactoryBot.create(:tax_household_member, tax_household: aqhp_tax_household_1, person: dependent_person, + is_tax_filer: false) + end + let!(:aqhp_thh_sub_tax_household_member_3) do + FactoryBot.create(:tax_household_member, tax_household: aqhp_tax_household_2, person: subscriber_person, + is_tax_filer: true) + end + let!(:aqhp_enrollment_tax_household_1) do + FactoryBot.create(:enrollments_tax_households, enrollment_id: enrollment_1.id, + tax_household_id: aqhp_tax_household_1.id, + household_benchmark_ehb_premium: 500.0) + end + let!(:enr_members_thhm_1) do + FactoryBot.create(:enrolled_members_tax_household_members, enrollments_tax_households: aqhp_enrollment_tax_household_1) + end + let!(:enr_members_thhm_2) do + FactoryBot.create(:enrolled_members_tax_household_members, enrollments_tax_households: aqhp_enrollment_tax_household_1) + end + + let!(:aqhp_enrollment_tax_household_2) do + FactoryBot.create(:enrollments_tax_households, enrollment_id: enrollment_2.id, + tax_household_id: aqhp_tax_household_2.id, + household_benchmark_ehb_premium: 400.0) + end + let!(:enr_members_thhm_3) do + FactoryBot.create(:enrolled_members_tax_household_members, enrollments_tax_households: aqhp_enrollment_tax_household_2) + end + + it "should not return member dropped slcsp premium" do + expect(insurance_policy.fetch_slcsp_premium([enrollment_1, enrollment_2], 1, aqhp_tax_household_2, + aqhp_enrollment_tax_household_2.applied_aptc)).to eq "500.00" + end + end end end