Skip to content

Commit

Permalink
Get rid of superfluous Structs; pre-calculate computed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
allthesignals committed Feb 14, 2025
1 parent d317f0b commit 03a8853
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
9 changes: 2 additions & 7 deletions app/app/models/response_objects/employment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ def self.from_pinwheel(response_body)
start_date: response_body["start_date"],
termination_date: response_body["termination_date"],
status: response_body["status"],
employer_phone_number: OpenStruct.new(
value: response_body["employer_phone_number"]["value"],
type: response_body["employer_phone_number"]["type"],
),
employer_address: OpenStruct.new(
raw: response_body["employer_address"]["raw"],
),
employer_phone_number: response_body["employer_phone_number"]["value"],
employer_address: response_body["employer_address"]["raw"]
)
end
end
Expand Down
59 changes: 30 additions & 29 deletions app/app/models/response_objects/paystub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
pay_period_start
pay_period_end
pay_date
earnings
deductions
hours_by_earning_category
hours
]

module ResponseObjects
Expand All @@ -21,12 +22,8 @@ def self.from_pinwheel(response_body)
pay_period_start: response_body["pay_period_start"],
pay_period_end: response_body["pay_period_end"],
pay_date: response_body["pay_date"],
earnings: response_body["earnings"].map do |earning|
PaystubEarning.new(
category: earning["category"],
hours: earning["hours"],
)
end,
hours: self.hours_from_pinwheel(response_body["earnings"]),
hours_by_earning_category: self.hours_by_earning_category_from_pinwheel(response_body["earnings"]),
deductions: response_body["deductions"].map do |deduction|
PaystubDeduction.new(
category: deduction["category"],
Expand All @@ -39,31 +36,35 @@ def self.from_pinwheel(response_body)
alias_attribute :start, :pay_period_start
alias_attribute :end, :pay_period_end

def hours
base_hours = earnings
.filter { |e| e.category != "overtime" }
.map { |e| e.hours }
.compact
.max
return unless base_hours
class << self
private

# Add overtime hours to the base hours, because they tend to be additional
# work beyond the other entries. (As opposed to category="premium", which
# often duplicates other earnings' hours.)
#
# See FFS-1773.
overtime_hours = earnings
.filter { |e| e.category == "overtime" }
.sum { |e| e.hours || 0.0 }
def hours_from_pinwheel(earnings)
base_hours = earnings
.filter { |e| e["category"] != "overtime" }
.map { |e| e["hours"] }
.compact
.max
return unless base_hours

base_hours + overtime_hours
end
# Add overtime hours to the base hours, because they tend to be additional
# work beyond the other entries. (As opposed to category="premium", which
# often duplicates other earnings' hours.)
#
# See FFS-1773.
overtime_hours = earnings
.filter { |e| e["category"] == "overtime" }
.sum { |e| e["hours"] || 0.0 }

base_hours + overtime_hours
end

def hours_by_earning_category
earnings
.filter { |e| e.hours && e.hours > 0 }
.group_by { |e| e.category }
.transform_values { |earnings| earnings.sum { |e| e.hours } }
def hours_by_earning_category_from_pinwheel(earnings)
earnings
.filter { |e| e["hours"] && e["hours"] > 0 }
.group_by { |e| e["category"] }
.transform_values { |earnings| earnings.sum { |e| e["hours"] } }
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/app/views/cbv/summaries/show.pdf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
<% end %>
<% if summary[:has_employment_data] %>
<% employment = summary[:employment] %>
<%= table.with_data_point(:employer_phone, employment.employer_phone_number.value) %>
<%= table.with_data_point(:employer_address, employment.employer_address.raw) %>
<%= table.with_data_point(:employer_phone, employment.employer_phone_number) %>
<%= table.with_data_point(:employer_address, employment.employer_address) %>
<%= table.with_data_point(:employment_status, employment.status) %>
<%= table.with_data_point(:employment_start_date, employment.start_date) %>
<%= table.with_data_point(:employment_end_date, employment.termination_date) %>
Expand Down
3 changes: 1 addition & 2 deletions app/spec/services/pinwheel_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
employment = service.fetch_employment(account_id: account_id)

expect(employment).to be_a(ResponseObjects::Employment)
expect(employment).to have_attributes(status: "employed", start_date: "2010-01-01")
expect(employment.employer_phone_number).to have_attributes(value: "+16126597057", type: "work")
expect(employment).to have_attributes(status: "employed", start_date: "2010-01-01", employer_phone_number: "+16126597057")
end
end

Expand Down

0 comments on commit 03a8853

Please sign in to comment.