From 4f1218262eb1ecc7f30d0de8165366e967c2d59a Mon Sep 17 00:00:00 2001 From: allthesignals Date: Thu, 27 Feb 2025 14:06:44 -0500 Subject: [PATCH] Fetch supported jobs from within model --- .../webhooks/pinwheel/events_controller.rb | 7 +------ app/app/models/payroll_account/pinwheel.rb | 17 +++++++++++++++++ app/app/services/pinwheel_service.rb | 4 ++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/app/controllers/webhooks/pinwheel/events_controller.rb b/app/app/controllers/webhooks/pinwheel/events_controller.rb index 65c0f0fe..5777587a 100644 --- a/app/app/controllers/webhooks/pinwheel/events_controller.rb +++ b/app/app/controllers/webhooks/pinwheel/events_controller.rb @@ -11,9 +11,8 @@ def create end if params["event"] == "account.added" - supported_jobs = get_supported_jobs(params["payload"]["platform_id"]) PayrollAccount - .create_with(cbv_flow: @cbv_flow, supported_jobs: supported_jobs) + .create_with(cbv_flow: @cbv_flow) .find_or_create_by(pinwheel_account_id: params["payload"]["account_id"]) track_account_created_event(@cbv_flow, params["payload"]["platform_name"]) end @@ -92,8 +91,4 @@ def set_cbv_flow def set_pinwheel @pinwheel = @cbv_flow.present? ? pinwheel_for(@cbv_flow) : PinwheelService.new("sandbox", DUMMY_API_KEY) end - - def get_supported_jobs(platform_id) - @pinwheel.fetch_platform(platform_id: platform_id)["data"]["supported_jobs"] - end end diff --git a/app/app/models/payroll_account/pinwheel.rb b/app/app/models/payroll_account/pinwheel.rb index 37eb520d..f6c697ed 100644 --- a/app/app/models/payroll_account/pinwheel.rb +++ b/app/app/models/payroll_account/pinwheel.rb @@ -1,4 +1,6 @@ class PayrollAccount::Pinwheel < PayrollAccount + after_create :lookup_supported_jobs + EVENTS_MAP = { "employment.added" => :employment_synced_at, "income.added" => :income_synced_at, @@ -54,4 +56,19 @@ def event_columns_for(job) [ error_column, sync_column ] end + + def lookup_supported_jobs + platform_id = pinwheel.fetch_account(account_id: pinwheel_account_id)["data"]["platform_id"] + supported_jobs = pinwheel.fetch_platform(platform_id: platform_id)["data"]["supported_jobs"] + + update(supported_jobs: supported_jobs) + end + + def pinwheel + PinwheelService.new(site_config[cbv_flow.client_agency_id].pinwheel_environment) + end + + def site_config + Rails.application.config.client_agencies + end end diff --git a/app/app/services/pinwheel_service.rb b/app/app/services/pinwheel_service.rb index 25746874..dd15b104 100644 --- a/app/app/services/pinwheel_service.rb +++ b/app/app/services/pinwheel_service.rb @@ -141,6 +141,10 @@ def fetch_items(options) @http.get(build_url(ITEMS_ENDPOINT), options).body end + def fetch_account(account_id:) + json = @http.get(build_url("#{ACCOUNTS_ENDPOINT}/#{account_id}")).body + end + def fetch_accounts(end_user_id:) @http.get(build_url("#{END_USERS}/#{end_user_id}/accounts")).body end