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

WIP Building Error Publishing #108

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions app/event_source/publishers/mitc_error_publisher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Publishers
# Publisher will send MEC check results payload to EA
class MecCheckPublisher
include ::EventSource::Publisher[amqp: 'magi_medicaid.mec_error']
register_event 'mec_error'
end
end
6 changes: 4 additions & 2 deletions app/event_source/subscribers/application_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class ApplicationSubscriber
ack(delivery_info.delivery_tag)
logger.debug "application_submitted_subscriber_message; acked"
else
errors = result.failure.errors.to_h
failure = result.failure
::Eligibilities::Medicaid::DeterminationError.new.call(failure, payload)
ack(delivery_info.delivery_tag)
logger.debug "application_submitted_subscriber_message; acked (nacked) due to:#{errors}"
logger.debug "application_submitted_subscriber_message; acked (nacked) due to:#{failure.errors.to_h}"
end
rescue StandardError => e
::Eligibilities::Medicaid::DeterminationError.new.call(e, payload)
ack(delivery_info.delivery_tag)
logger.debug "application_submitted_subscriber_error: baacktrace: #{e.backtrace}; acked (nacked)"
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/medicaid/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ class Application
# For Example: In DC's case the external system is MitC
field :medicaid_response_payload, type: String

# Stores error messages from failed operations
field :failure, type: String, default: nil

# Stores exceptions from failed operations
field :exception, type: Hash, default: nil

embeds_many :aptc_households, class_name: '::Medicaid::AptcHousehold'
accepts_nested_attributes_for :aptc_households

def successful?
# TODO: Update this to account for new failure and exception fields
return true unless application_response_payload.blank?
end

Expand Down
30 changes: 30 additions & 0 deletions app/operations/applications/find.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'dry/monads'
require 'dry/monads/do'

module Applications
# Operation creates persitance application object with
# application_identifier, application_request_payload & medicaid_request_payload
# params only.
class Find
include Dry::Monads[:result, :do]

# @param [Hash] opts The options to create application object
# @option opts [Hash] :params Medicaid Application params
# @return [Dry::Monads::Result]
def call(application_identifier)
application = yield find(application_identifier)

Success(application)
end

private

def find(application_identifier)
Success(::Medicaid::Application.find_by!(application_identifier: application_identifier))
rescue StandardError
Failure("Application not found for #{application_identifier}")
end
end
end
92 changes: 92 additions & 0 deletions app/operations/eligibilities/medicaid/determination_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require 'dry/monads'
require 'dry/monads/do'

module Eligibilities
module Medicaid
# This class is for requesting the medicaid determination for all the Applicants.
class DeterminationError
include Dry::Monads[:result, :do]
include EventSource::Command

# @param [Hash] opts The options to request medicaid determination
# @option opts [Hash] :params MagiMedicaid Application request payload
# @return [Dry::Monads::Result]
def call(failure, payload)
@application = yield init_or_find_magi_medicaid_application(payload)
error_message = yield build_and_log_error_message(failure)
yield publish_error(error_message)

Success(@application)
end

private

def publish_event(error_message)
event("events.determinations.error", attributes: { error: error_message }).publish
end

# We need to find the app that may have already been created or
# if the error occured prior to creation we'll create a new one ourselves
def init_or_find_magi_medicaid_application(payload)
application_identifier = payload[:hbx_id]
existing_application = ::Applications::Find.new.call(application_identifier)

if existing_application
Success(existing_application)
else
Success(::AcaEntities::MagiMedicaid::Operations::InitializeApplication.new.call(payload))
end
end

def build_and_log_error_message(failure)
case failure
when Dry::Validation::Result
error = validation_errors_parser(failure)
log_failure(error)
error
when Exception
log_exception(failure)
failure.message
else
log_failure(failure)
"Submission Error: #{failure}"
end
end

def log_exception(exception)
@application.update exception: {
message: exception.message,
backtrace: exception.backtrace
} || Rails.logger.warn("Couldn't log exception #{exception} with #{@application}")
end

def log_failure(failure)
@application.update(failure: failure) ||
Rails.logger.warn("Couldn't log failure #{failure} with #{@application}")
end

# TODO: this is not a great parser and could use improvements
def validation_errors_parser(failure)
failure.errors.each_with_object([]) do |error, collect|
collect << if error.is_a?(Dry::Schema::Message)
message = error.path.reduce("The ") do |attribute_message, path|
next_element = error.path[(error.path.index(path) + 1)]
attribute_message + if next_element.is_a?(Integer)
"#{(next_element + 1).ordinalize} #{path.to_s.humanize.downcase}'s "
elsif path.is_a? Integer
""
else
"#{path.to_s.humanize.downcase}:"
end
end
message + " #{error.text}."
else
error.flatten.flatten.join(',').gsub(",", " ").titleize
end
end
end
end
end
end