Skip to content

Commit

Permalink
STI-ify PayrollAccounts (#473)
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/app/models/payroll_account.rb
#	app/db/schema.rb
#	docs/app/rendered/database-schema.pdf
  • Loading branch information
allthesignals authored Feb 28, 2025
1 parent cf539d0 commit f3469b0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 59 deletions.
6 changes: 3 additions & 3 deletions app/app/controllers/webhooks/pinwheel/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def create
track_account_created_event(@cbv_flow, params["payload"]["platform_name"])
end

if PayrollAccount::EVENTS_MAP.keys.include?(params["event"])
if PayrollAccount::Pinwheel::EVENTS_MAP.keys.include?(params["event"])
pinwheel_account = PayrollAccount.find_by_pinwheel_account_id(params["payload"]["account_id"])

if pinwheel_account.present?
pinwheel_account.update!(PayrollAccount::EVENTS_MAP[params["event"]] => Time.now)
pinwheel_account.update!(PayrollAccount::Pinwheel::EVENTS_MAP[params["event"]] => Time.now)

if params.dig("payload", "outcome") == "error" || params.dig("payload", "outcome") == "pending"
pinwheel_account.update!(PayrollAccount::EVENTS_ERRORS_MAP[params["event"]] => Time.now)
pinwheel_account.update!(PayrollAccount::Pinwheel::EVENTS_ERRORS_MAP[params["event"]] => Time.now)
end

if pinwheel_account.has_fully_synced?
Expand Down
66 changes: 10 additions & 56 deletions app/app/models/payroll_account.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,19 @@
class PayrollAccount < ApplicationRecord
def self.sti_name
# "PayrollAccount::Pinwheel" => "pinwheel"
name.demodulize.downcase
end

def self.sti_class_for(type_name)
# "pinwheel" => PayrollAccount::Pinwheel
PayrollAccount.const_get(type_name.capitalize)
end

belongs_to :cbv_flow

after_update_commit {
I18n.with_locale(cbv_flow.cbv_flow_invitation.language) do
broadcast_replace target: self, partial: "cbv/synchronizations/indicators", locals: { pinwheel_account: self }
end
}

EVENTS_MAP = {
"employment.added" => :employment_synced_at,
"income.added" => :income_synced_at,
"identity.added" => :identity_synced_at,
"paystubs.fully_synced" => :paystubs_synced_at
}

EVENTS_ERRORS_MAP = {
"employment.added" => :employment_errored_at,
"income.added" => :income_errored_at,
"identity.added" => :identity_errored_at,
"paystubs.fully_synced" => :paystubs_errored_at
}

def has_fully_synced?
(supported_jobs.exclude?("paystubs") || paystubs_synced_at.present?) &&
(supported_jobs.exclude?("employment") || employment_synced_at.present?) &&
(supported_jobs.exclude?("income") || income_synced_at.present?) &&
(supported_jobs.exclude?("identity") || identity_synced_at.present?)
end

def job_succeeded?(job)
error_column, sync_column = event_columns_for(job)
return nil unless error_column.present?

supported_jobs.include?(job) && send(sync_column).present? && send(error_column).blank?
end

def synchronization_status(job)
error_column, sync_column = event_columns_for(job)
return nil unless error_column.present?

if supported_jobs.exclude?(job)
:unsupported
elsif job_succeeded?(job)
:succeeded
elsif supported_jobs.include?(job) && (send(sync_column).blank? && send(error_column).blank?)
:in_progress
elsif supported_jobs.include?(job) && (send(error_column).present?)
:failed
end
end

def has_required_data?
job_succeeded?("paystubs")
end

private

def event_columns_for(job)
error_column = EVENTS_ERRORS_MAP.select { |key| key.start_with? job }&.values.last
sync_column = EVENTS_MAP.select { |key| key.start_with? job }&.values.last

[ error_column, sync_column ]
end
end
57 changes: 57 additions & 0 deletions app/app/models/payroll_account/pinwheel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class PayrollAccount::Pinwheel < PayrollAccount
EVENTS_MAP = {
"employment.added" => :employment_synced_at,
"income.added" => :income_synced_at,
"identity.added" => :identity_synced_at,
"paystubs.fully_synced" => :paystubs_synced_at
}

EVENTS_ERRORS_MAP = {
"employment.added" => :employment_errored_at,
"income.added" => :income_errored_at,
"identity.added" => :identity_errored_at,
"paystubs.fully_synced" => :paystubs_errored_at
}

def has_fully_synced?
(supported_jobs.exclude?("paystubs") || paystubs_synced_at.present?) &&
(supported_jobs.exclude?("employment") || employment_synced_at.present?) &&
(supported_jobs.exclude?("income") || income_synced_at.present?) &&
(supported_jobs.exclude?("identity") || identity_synced_at.present?)
end

def job_succeeded?(job)
error_column, sync_column = event_columns_for(job)
return nil unless error_column.present?

supported_jobs.include?(job) && send(sync_column).present? && send(error_column).blank?
end

def synchronization_status(job)
error_column, sync_column = event_columns_for(job)
return nil unless error_column.present?

if supported_jobs.exclude?(job)
:unsupported
elsif job_succeeded?(job)
:succeeded
elsif supported_jobs.include?(job) && (send(sync_column).blank? && send(error_column).blank?)
:in_progress
elsif supported_jobs.include?(job) && (send(error_column).present?)
:failed
end
end

def has_required_data?
job_succeeded?("paystubs")
end

private

def event_columns_for(job)
error_column = EVENTS_ERRORS_MAP.select { |key| key.start_with? job }&.values.last
sync_column = EVENTS_MAP.select { |key| key.start_with? job }&.values.last

[ error_column, sync_column ]
end
end
5 changes: 5 additions & 0 deletions app/db/migrate/20250225204333_add_type_to_payroll_accounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTypeToPayrollAccounts < ActiveRecord::Migration[7.1]
def change
add_column :pinwheel_accounts, :type, :string, null: false, default: "pinwheel"
end
end
1 change: 1 addition & 0 deletions app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
t.datetime "paystubs_errored_at", precision: nil
t.datetime "identity_errored_at", precision: nil
t.datetime "identity_synced_at", precision: nil
t.string "type", default: "pinwheel", null: false
t.index ["cbv_flow_id"], name: "index_payroll_accounts_on_cbv_flow_id"
end

Expand Down
Binary file modified docs/app/rendered/database-schema.pdf
Binary file not shown.

0 comments on commit f3469b0

Please sign in to comment.