diff --git a/app/business_processes/handlers/enrollment_event_xml_helper.rb b/app/business_processes/handlers/enrollment_event_xml_helper.rb index a6c337bb1..68c600b14 100644 --- a/app/business_processes/handlers/enrollment_event_xml_helper.rb +++ b/app/business_processes/handlers/enrollment_event_xml_helper.rb @@ -18,6 +18,16 @@ def extract_enrollee_start(enrollee) Date.strptime(val, "%Y%m%d") end + def enrollee_start_for_member_id(policy_cv, member_id) + enrollee_list = Maybe.new(policy_cv).enrollees.value + + enrollee_list ||= [] + enrollee = enrollee_list.detect{|enrollee| extract_member_id(enrollee) == member_id} + + return nil if enrollee.blank? + extract_enrollee_start(enrollee) + end + def extract_carrier_id(policy_cv) carrier = Maybe.new(policy_cv).policy_enrollment.plan.carrier.value return nil unless carrier diff --git a/app/models/enrollment_action/dependent_add.rb b/app/models/enrollment_action/dependent_add.rb index 96ac7ad1f..417670adb 100644 --- a/app/models/enrollment_action/dependent_add.rb +++ b/app/models/enrollment_action/dependent_add.rb @@ -60,8 +60,26 @@ def publish publish_edi(amqp_connection, change_publish_helper.to_xml, action.hbx_enrollment_id, action.employer_hbx_id) else member_date_map = {} + # pick policy members + policy_to_change.enrollees.each do |en| - member_date_map[en.m_id] = en.coverage_start + if en.coverage_end.blank? + member_date_map[en.m_id] = en.coverage_start + else + ##en.coverage_end on the member + ## start date of the members(from the XML) + ## CHECK FOR DATE EXISTS? + member_start_from_xml = action.enrollee_start_for_member_id(action.policy_cv, en.m_id) + # coverage end of enrollee + # if that date is 1 day + # if member_start_from_xml nil? + + next if member_start_from_xml.blank? + + if en.coverage_end + 1.day >= member_start_from_xml + member_date_map[en.m_id] = en.coverage_start + end + end end change_publish_helper.set_policy_id(policy_to_change.eg_id) change_publish_helper.set_member_starts(member_date_map) diff --git a/spec/business_processes/handlers/enrollment_event_xml_helper_spec.rb b/spec/business_processes/handlers/enrollment_event_xml_helper_spec.rb index fce8d9ec5..46aa2b016 100644 --- a/spec/business_processes/handlers/enrollment_event_xml_helper_spec.rb +++ b/spec/business_processes/handlers/enrollment_event_xml_helper_spec.rb @@ -47,6 +47,27 @@ end end + describe "#enrollee_start_for_member_id" do + let(:member_id) { "54" } + let(:member) { instance_double(::Openhbx::Cv2::EnrolleeMember, id: "ABC##{member_id}") } + let(:benefit) { instance_double(::Openhbx::Cv2::EnrolleeBenefit, begin_date: "20170101") } + let(:enrollee) { instance_double(::Openhbx::Cv2::Enrollee, member: member, benefit: benefit) } + let(:other_enrollee) { instance_double(::Openhbx::Cv2::Enrollee, member: instance_double(::Openhbx::Cv2::EnrolleeMember, id: "DEF#99")) } + let(:policy_cv) { instance_double(::Openhbx::Cv2::Policy, enrollees: [other_enrollee, enrollee]) } + + it "returns the start date for the given member id" do + expect(subject.enrollee_start_for_member_id(policy_cv, member_id)).to eq(Date.new(2017, 1, 1)) + end + + context "when the member id is not found" do + let(:policy_cv) { instance_double(::Openhbx::Cv2::Policy, enrollees: [other_enrollee]) } + + it "returns nil" do + expect(subject.enrollee_start_for_member_id(policy_cv, member_id)).to be_nil + end + end + end + describe "#extract_enrollment_group_id" do let(:policy_cv) { double(:id => "SOMEGRP#1234") } it "returns the parsed id" do diff --git a/spec/models/enrollment_action/dependent_add_spec.rb b/spec/models/enrollment_action/dependent_add_spec.rb index 977ea3f7c..1000dc257 100644 --- a/spec/models/enrollment_action/dependent_add_spec.rb +++ b/spec/models/enrollment_action/dependent_add_spec.rb @@ -82,8 +82,8 @@ let(:amqp_connection) { double } let(:event_xml) { double } let(:event_responder) { instance_double(::ExternalEvents::EventResponder, :connection => amqp_connection) } - let(:enrollee_primary) { double(:m_id => 1, :coverage_start => :one_month_ago) } - let(:enrollee_new) { double(:m_id => 2, :coverage_start => :one_month_ago) } + let(:enrollee_primary) { double(:m_id => 1, :coverage_start => :one_month_ago, :coverage_end => nil) } + let(:enrollee_new) { double(:m_id => 2, :coverage_start => :one_month_ago, :coverage_end => nil) } let(:plan) { instance_double(Plan, :id => 1) } let(:carrier) { instance_double(Carrier, :renewal_dependent_add_transmitted_as_renewal => true) } @@ -413,3 +413,158 @@ end end end + +describe EnrollmentAction::DependentAdd, "given overlapping coverage" do + let(:amqp_connection) { double } + let(:event_xml) { double } + let(:event_responder) { instance_double(::ExternalEvents::EventResponder, :connection => amqp_connection) } + let(:enrollee_primary) { double(:m_id => 1, :coverage_start => :one_month_ago, :coverage_end => nil) } + let(:enrollee_new) { double(:m_id => 2, :coverage_start => :one_month_ago, :coverage_end => enrollee_new_original_end) } + + let(:enrollee_new_original_end) { Date.new(2024,5,29) } + let(:enrollee_new_xml_start) { Date.new(2024,5,29) } + + let(:plan) { instance_double(Plan, :id => 1) } + let(:carrier) { instance_double(Carrier, :renewal_dependent_add_transmitted_as_renewal => true) } + let(:policy) { instance_double(Policy, :enrollees => [enrollee_primary, enrollee_new], :eg_id => 1, carrier: carrier) } + let(:policy_cv) { instance_double(::Openhbx::Cv2::Policy, enrollees: [enrollee_primary, enrollee_new]) } + + let(:dependent_add_event) { instance_double( + ::ExternalEvents::EnrollmentEventNotification, + :event_xml => event_xml, + :all_member_ids => [1,2], + :hbx_enrollment_id => 2, + employer_hbx_id: 1, + :policy_cv => policy_cv + ) } + let(:termination_event) { instance_double( + ::ExternalEvents::EnrollmentEventNotification, + :existing_policy => policy, + :all_member_ids => [1], + :event_responder => event_responder, + :hbx_enrollment_id => 1, + :employer_hbx_id => 1 + ) } + let(:action_helper_result_xml) { double } + + let(:action_publish_helper) { instance_double( + EnrollmentAction::ActionPublishHelper, + :to_xml => action_helper_result_xml + ) } + + before :each do + allow(EnrollmentAction::ActionPublishHelper).to receive(:new).with(event_xml).and_return(action_publish_helper) + allow(action_publish_helper).to receive(:set_policy_id).with(1).and_return(true) + allow(action_publish_helper).to receive(:filter_affected_members).with([2]).and_return(true) + allow(action_publish_helper).to receive(:set_event_action).with("urn:openhbx:terms:v1:enrollment#change_member_add") + allow(action_publish_helper).to receive(:set_member_starts).with({ 1 => :one_month_ago, 2 => :one_month_ago }) + allow(action_publish_helper).to receive(:keep_member_ends).with([]) + allow(subject).to receive(:publish_edi).with(amqp_connection, action_helper_result_xml, termination_event.hbx_enrollment_id, termination_event.employer_hbx_id) + allow(subject).to receive(:same_carrier_renewal_candidates).with(dependent_add_event).and_return([]) + allow(dependent_add_event).to receive(:enrollee_start_for_member_id).with(policy, 2).and_return(enrollee_new_xml_start) + allow(dependent_add_event).to receive(:policy_cv).and_return(policy) + end + + subject do + EnrollmentAction::DependentAdd.new(termination_event, dependent_add_event) + end + + it "publishes an event of type add dependents" do + expect(action_publish_helper).to receive(:set_event_action).with("urn:openhbx:terms:v1:enrollment#change_member_add") + subject.publish + end + + it "sets member start dates" do + expect(action_publish_helper).to receive(:set_member_starts).with({ 1 => :one_month_ago, 2 => :one_month_ago }) + subject.publish + end + + it "clears all member end dates before publishing" do + expect(action_publish_helper).to receive(:keep_member_ends).with([]) + subject.publish + end + + it "publishes resulting xml to edi" do + expect(subject).to receive(:publish_edi).with(amqp_connection, action_helper_result_xml, termination_event.hbx_enrollment_id, termination_event.employer_hbx_id) + subject.publish + end + + it "publishes an auto renew event when adding dependent to renewal" do + subject.dep_adding_to_renewal = true + allow(subject).to receive(:publish_edi).with(amqp_connection, action_helper_result_xml, dependent_add_event.hbx_enrollment_id, dependent_add_event.employer_hbx_id) + allow(EnrollmentAction::ActionPublishHelper).to receive(:new).with(dependent_add_event).and_return(action_publish_helper) + expect(action_publish_helper).to receive(:set_event_action).with("urn:openhbx:terms:v1:enrollment#auto_renew") + subject.publish + end +end + +describe EnrollmentAction::DependentAdd, "given non overlapping coverage" do + let(:amqp_connection) { double } + let(:event_xml) { double } + let(:event_responder) { instance_double(::ExternalEvents::EventResponder, :connection => amqp_connection) } + let(:enrollee_primary) { double(:m_id => 1, :coverage_start => :one_month_ago, :coverage_end => nil) } + let(:enrollee_new) { double(:m_id => 2, :coverage_start => :one_month_ago, :coverage_end => enrollee_new_original_end) } + + let(:enrollee_new_original_end) { Date.new(2024,5,25) } + let(:enrollee_new_xml_start) { Date.new(2024,5,29) } + + let(:plan) { instance_double(Plan, :id => 1) } + let(:carrier) { instance_double(Carrier, :renewal_dependent_add_transmitted_as_renewal => true) } + let(:policy) { instance_double(Policy, :enrollees => [enrollee_primary, enrollee_new], :eg_id => 1, carrier: carrier) } + let(:policy_cv) { instance_double(::Openhbx::Cv2::Policy, enrollees: [enrollee_primary, enrollee_new]) } + + let(:dependent_add_event) { instance_double( + ::ExternalEvents::EnrollmentEventNotification, + :event_xml => event_xml, + :all_member_ids => [1,2], + :hbx_enrollment_id => 2, + employer_hbx_id: 1, + :policy_cv => policy_cv + ) } + let(:termination_event) { instance_double( + ::ExternalEvents::EnrollmentEventNotification, + :existing_policy => policy, + :all_member_ids => [1], + :event_responder => event_responder, + :hbx_enrollment_id => 1, + :employer_hbx_id => 1 + ) } + let(:action_helper_result_xml) { double } + + let(:action_publish_helper) { instance_double( + EnrollmentAction::ActionPublishHelper, + :to_xml => action_helper_result_xml + ) } + + before :each do + allow(EnrollmentAction::ActionPublishHelper).to receive(:new).with(event_xml).and_return(action_publish_helper) + allow(action_publish_helper).to receive(:set_policy_id).with(1).and_return(true) + allow(action_publish_helper).to receive(:filter_affected_members).with([2]).and_return(true) + allow(action_publish_helper).to receive(:set_event_action).with("urn:openhbx:terms:v1:enrollment#change_member_add") + allow(action_publish_helper).to receive(:set_member_starts).with({ 1 => :one_month_ago }).and_return(true) + allow(action_publish_helper).to receive(:keep_member_ends).with([]) + allow(subject).to receive(:publish_edi).with(amqp_connection, action_helper_result_xml, termination_event.hbx_enrollment_id, termination_event.employer_hbx_id) + allow(subject).to receive(:same_carrier_renewal_candidates).with(dependent_add_event).and_return([]) + allow(dependent_add_event).to receive(:enrollee_start_for_member_id).with(policy, 2).and_return(enrollee_new_xml_start) + allow(dependent_add_event).to receive(:policy_cv).and_return(policy) + end + + subject do + EnrollmentAction::DependentAdd.new(termination_event, dependent_add_event) + end + + it "publishes an event of type add dependents" do + expect(action_publish_helper).to receive(:set_event_action).with("urn:openhbx:terms:v1:enrollment#change_member_add") + subject.publish + end + + it "sets member start dates" do + expect(action_publish_helper).to receive(:set_member_starts).with({ 1 => :one_month_ago }) + subject.publish + end + + it "clears all member end dates before publishing" do + expect(action_publish_helper).to receive(:keep_member_ends).with([]) + subject.publish + end +end \ No newline at end of file