Class: Forms::SubmissionStatuses::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/forms/submission_statuses/formatter.rb

Instance Method Summary collapse

Instance Method Details

#build_submissions_map(submissions) ⇒ Object (private)



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/forms/submission_statuses/formatter.rb', line 40

def build_submissions_map(submissions)
  submissions.each_with_object({}) do |submission, hash|
    hash[submission.benefits_intake_uuid] = OpenStruct.new(
      id: submission.benefits_intake_uuid,
      detail: nil,
      form_type: submission.form_type,
      message: nil,
      status: nil,
      created_at: submission.created_at,
      updated_at: nil
    )
  end
end

#format_data(dataset) ⇒ Object



6
7
8
9
10
11
# File 'lib/forms/submission_statuses/formatter.rb', line 6

def format_data(dataset)
  return [] unless dataset.submissions?

  results = merge_records_from(dataset)
  dataset.intake_statuses? ? sort_results(results) : results
end

#merge_record(submission_map, status) ⇒ Object (private)



30
31
32
33
34
35
36
37
38
# File 'lib/forms/submission_statuses/formatter.rb', line 30

def merge_record(submission_map, status)
  submission = submission_map[status['attributes']['guid']]
  if submission
    submission.detail = status['attributes']['detail']
    submission.message = status['attributes']['message']
    submission.status = status['attributes']['status']
    submission.updated_at = status['attributes']['updated_at']
  end
end

#merge_records(submissions, statuses) ⇒ Object (private)



23
24
25
26
27
28
# File 'lib/forms/submission_statuses/formatter.rb', line 23

def merge_records(submissions, statuses)
  submission_map = build_submissions_map(submissions)
  statuses&.each { |status| merge_record(submission_map, status) }

  submission_map.values
end

#merge_records_from(dataset) ⇒ Object (private)



19
20
21
# File 'lib/forms/submission_statuses/formatter.rb', line 19

def merge_records_from(dataset)
  merge_records(dataset.submissions, dataset.intake_statuses)
end

#sort_results(results) ⇒ Object (private)



15
16
17
# File 'lib/forms/submission_statuses/formatter.rb', line 15

def sort_results(results)
  results&.sort_by { |record| [record.updated_at ? 1 : 0, record.updated_at] }
end