-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnsc_data_fetcher_service.rb
More file actions
107 lines (91 loc) · 3.78 KB
/
Copy pathnsc_data_fetcher_service.rb
File metadata and controls
107 lines (91 loc) · 3.78 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
class NscDataFetcherService
CURRENTLY_ENROLLED = "CC"
ENROLLMENT_STATUSES = {
# Ordered in descending preference if there are multiple active enrollments.
# The key is the API response value
# The value is the enum value for EducationActivity.
"F" => :full_time,
"Q" => :three_quarter_time,
"H" => :half_time,
"L" => :less_than_half_time,
"Y" => :enrolled
}
def initialize(
education_activity:,
logger: Rails.logger,
environment: ENV.fetch("NSC_ENVIRONMENT", "sandbox"),
response_transformer: nil,
as_of_date: nil
)
@education_activity = education_activity
@logger = logger
@service = Aggregators::Sdk::NscService.new(environment: environment, logger: logger)
@response_transformer = response_transformer
@as_of_date = as_of_date
end
# Main entry pont to submit an enrollment request to NSC
def fetch
identity = @education_activity.activity_flow.identity
response = @service.fetch_enrollment_data(
first_name: identity.first_name,
last_name: identity.last_name,
date_of_birth: identity.date_of_birth,
as_of_date: @as_of_date || @education_activity.activity_flow.reporting_window_range.max
)
response = transform_response(response)
update_education_activity(@education_activity, response)
if @education_activity.sync_succeeded?
save_enrollment_terms(response["enrollmentDetails"])
@education_activity.update!(
data_source: EducationActivity.data_source_from_nsc_results(
@education_activity.nsc_enrollment_terms,
reporting_months: @education_activity.activity_flow.reporting_months
)
)
end
end
private
def update_education_activity(education_activity, response_data)
enrollments = Array(response_data["enrollmentDetails"])
current_enrollments = enrollments.find_all { |enrollment_detail| enrollment_detail["currentEnrollmentStatus"] == "CC" }
if current_enrollments.any?
@logger.info "Found #{current_enrollments.length} current enrollments (total enrollments: #{enrollments.length})"
@education_activity.update(status: :succeeded)
else
@logger.info("No enrollments found for EducationActivity ID #{education_activity.id}")
@education_activity.update(status: :no_enrollments)
end
end
def save_enrollment_terms(enrollment_details)
activity_flow = @education_activity.activity_flow
identity = activity_flow.identity
enrollment_details.each do |enrollment_detail|
next unless enrollment_detail["currentEnrollmentStatus"] == CURRENTLY_ENROLLED
name_on_school_record = enrollment_detail["nameOnSchoolRecord"] || {}
enrollment_detail["enrollmentData"].each do |enrollment_data|
term_begin = Date.parse(enrollment_data["termBeginDate"])
term_end = Date.parse(enrollment_data["termEndDate"])
next unless activity_flow.within_reporting_window?(term_begin, term_end)
@education_activity.nsc_enrollment_terms.create!(
school_name: enrollment_detail["officialSchoolName"],
first_name: name_on_school_record["firstName"] || identity&.first_name,
middle_name: name_on_school_record["middleName"],
last_name: name_on_school_record["lastName"] || identity&.last_name,
enrollment_status: enrollment_status(enrollment_data),
term_begin: term_begin,
term_end: term_end,
)
end
end
end
def transform_response(response)
return response unless @response_transformer
@response_transformer.call(response)
end
def enrollment_status(enrollment_data)
ENROLLMENT_STATUSES.fetch(enrollment_data["enrollmentStatus"]) do
@logger.error "Unknown enrollmentStatus (add to ENROLLMENT_STATUSES): #{enrollment_data["enrollmentStatus"]}"
"unknown"
end
end
end