Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include plan year collection for last four years #11

Open
wants to merge 1 commit into
base: update_nfp_gem
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions app/models/listeners/employer_digest_drop_listener.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
require 'date'
require 'nokogiri'

module Listeners
class EmployerDigestDropListener < Amqp::RetryClient

NODE_NS = {
terms: "http://openhbx.org/api/terms/1.0"
}

def log_response(key, code, headers, body)
broadcaster = Amqp::EventBroadcaster.new(connection)
response_properties = {
Expand All @@ -14,13 +22,27 @@ def log_response(key, code, headers, body)

def on_message(delivery_info, properties, payload)
digest_xml = payload

# Parse the XML payload
xml = Nokogiri::XML(digest_xml)

# Get the current year and dynamically calculate the threshold year
current_year = Date.today.year
threshold_year = current_year - 4

# Method to remove plan years older than the threshold year
kill_years_older_than(xml, threshold_year)

# Convert XML back to string
digest_xml = xml.to_xml

headers = properties.headers || {}
v2_proxy = ::Proxies::EmployerXmlDropRequest.new
r_code, r_payload = v2_proxy.request(payload, 180)
r_code, r_payload = v2_proxy.request(digest_xml, 180)
case r_code.to_s
when "200"
# ALL GOOD
log_response("info.application.hbx_enterprise.employer_digest_drop_listener.digest_published",r_code, headers, digest_xml)
log_response("info.application.hbx_enterprise.employer_digest_drop_listener.digest_published", r_code, headers, digest_xml)
log_response("info.application.hbx_enterprise.employer_digest_drop_listener.service_response",
r_code,
headers,
Expand All @@ -30,10 +52,10 @@ def on_message(delivery_info, properties, payload)
}.to_json)
channel.acknowledge(delivery_info.delivery_tag, false)
when "503"
log_response("error.application.hbx_enterprise.employer_digest_drop_listener.timeout",r_code, headers, digest_xml)
channel.basic_reject(delivery_info.delivery_tag,true)
log_response("error.application.hbx_enterprise.employer_digest_drop_listener.timeout", r_code, headers, digest_xml)
channel.basic_reject(delivery_info.delivery_tag, true)
else
log_response("error.application.hbx_enterprise.employer_digest_drop_listener.failure",r_code, headers, digest_xml)
log_response("error.application.hbx_enterprise.employer_digest_drop_listener.failure", r_code, headers, digest_xml)
log_response("error.application.hbx_enterprise.employer_digest_drop_listener.service_response",
r_code,
headers,
Expand All @@ -45,6 +67,15 @@ def on_message(delivery_info, properties, payload)
end
end

# Method to remove plan years older than the threshold year
def kill_years_older_than(xml_doc, threshold_year)
xml_doc.xpath("//terms:plan_years/terms:plan_year", NODE_NS).each do |node|
plan_year_start = node.at_xpath("terms:plan_year_start", NODE_NS).text
plan_year_start_year = plan_year_start[0..3].to_i
node.unlink if plan_year_start_year < threshold_year
end
end

def self.queue_name
ec = ExchangeInformation
"#{ec.hbx_id}.#{ec.environment}.q.hbx_enterprise.employer_digest_drop_listener"
Expand Down