-
Notifications
You must be signed in to change notification settings - Fork 13
/
submission_error_parser.rb
95 lines (77 loc) · 3.52 KB
/
submission_error_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module Efile
class SubmissionErrorParser
attr_accessor :xml
def initialize(transition)
@transition = transition
@raw_response = transition.metadata["raw_response"]
end
def persist_errors
metadata = @transition.metadata
# these errors are mostly failures on our side of things, like issues with bundling and addresses
if metadata["error_code"].present?
persist_errors_from_error_metadata
end
if @transition.to_state == "failed" && metadata["raw_response"].present?
persist_bundle_errors_from_raw_response_metadata
end
# there errors are responses from the IRS, like rejections or exceptions
if @transition.to_state == "rejected" && metadata["raw_response"].present?
persist_errors_from_raw_response_metadata
end
end
private
def persist_errors_from_error_metadata
efile_submission = @transition.efile_submission
metadata = @transition.metadata
attrs = { code: metadata["error_code"], service_type: error_service_type(efile_submission) }
attrs[:source] = metadata["error_source"] if metadata["error_source"].present?
efile_error = EfileError.find_or_create_by!(attrs)
efile_error.update(message: metadata["error_message"]) if metadata["error_message"].present? && efile_error.message != metadata["error_message"]
@transition.efile_submission_transition_errors.create(efile_submission_id: efile_submission.id, efile_error: efile_error)
end
def persist_errors_from_raw_response_metadata
as_xml = Nokogiri::XML(@raw_response)
raise UnexpectedFormatError, "raw_response on transition is not in expected format" unless as_xml.at("ValidationErrorGrp").present?
as_xml.search("ValidationErrorGrp").each do |error_group|
error = EfileError.find_or_create_by!(
code: error_group.at("RuleNum")&.text,
category: error_group.at("ErrorCategoryCd")&.text,
severity: error_group.at("SeverityCd")&.text,
source: "irs",
service_type: error_service_type(@transition.efile_submission)
)
message = error_group.at("ErrorMessageTxt")&.text
error.update(message: message) if message.present? && error.message != message
identifier = error_group.at("FieldValueTxt")&.text
dependent_id = nil
if identifier.present?
@transition.efile_submission.qualifying_dependents.each do |submission_dependent|
dependent_id = submission_dependent.dependent_id if submission_dependent.dependent&.ssn == identifier
end
end
@transition.efile_submission_transition_errors.create(efile_submission_id: @transition.efile_submission.id, efile_error_id: error.id, dependent_id: dependent_id)
end
end
def persist_bundle_errors_from_raw_response_metadata
if @transition.metadata["raw_response"].to_s.match?(/(RoutingTransitNum|DepositorAccountNum).*is not accepted by the pattern/)
error = EfileError.find_by(
code: "BANK-DETAILS"
)
@transition.efile_submission_transition_errors.create(efile_submission_id: @transition.efile_submission.id, efile_error_id: error.id)
end
end
def self.persist_errors(*args)
new(*args).persist_errors
end
private
def error_service_type(submission)
if submission.is_for_state_filing?
state_code = submission.data_source.state_code
"state_file_#{state_code}"
else
"ctc"
end
end
end
class UnexpectedFormatError < StandardError; end
end