1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 04:55:20 +02:00

Plaid sync domain improvements (#2267)
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

Breaks our Plaid sync process out into more manageable classes. Notably, this moves the sync process to a distinct, 2-step flow:

1. Import stage - we first make API calls and import Plaid data to "mirror" tables
2. Processing stage - read the raw data, apply business rules, build internal domain models and sync balances

This provides several benefits:

- Plaid syncs can now be "replayed" without fetching API data again
- Mirror tables provide better audit and debugging capabilities
- Eliminates the "all or nothing" sync behavior that is currently in place, which is brittle
This commit is contained in:
Zach Gollwitzer 2025-05-23 18:58:22 -04:00 committed by GitHub
parent 5c82af0e8c
commit 03a146222d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
72 changed files with 3763 additions and 706 deletions

View file

@ -1,136 +0,0 @@
require "test_helper"
class Provider::Plaid::CategoryAliasMatcherTest < ActiveSupport::TestCase
setup do
@family = families(:empty)
# User income categories
@income = @family.categories.create!(name: "Income", classification: "income")
@dividend_income = @family.categories.create!(name: "Dividend Income", parent: @income, classification: "income")
@interest_income = @family.categories.create!(name: "Interest Income", parent: @income, classification: "income")
# User expense categories
@loan_payments = @family.categories.create!(name: "Loan Payments")
@fees = @family.categories.create!(name: "Fees")
@entertainment = @family.categories.create!(name: "Entertainment")
@food_and_drink = @family.categories.create!(name: "Food & Drink")
@groceries = @family.categories.create!(name: "Groceries", parent: @food_and_drink)
@restaurant = @family.categories.create!(name: "Restaurant", parent: @food_and_drink)
@shopping = @family.categories.create!(name: "Shopping")
@clothing = @family.categories.create!(name: "Clothing", parent: @shopping)
@home = @family.categories.create!(name: "Home")
@medical = @family.categories.create!(name: "Medical")
@personal_care = @family.categories.create!(name: "Personal Care")
@transportation = @family.categories.create!(name: "Transportation")
@trips = @family.categories.create!(name: "Trips")
@services = @family.categories.create!(name: "Services")
@car = @family.categories.create!(name: "Car", parent: @services)
@giving = @family.categories.create!(name: "Giving")
@matcher = Provider::Plaid::CategoryAliasMatcher.new(@family.categories)
end
test "matches expense categories" do
assert_equal @loan_payments, @matcher.match("loan_payments_car_payment")
assert_equal @loan_payments, @matcher.match("loan_payments_credit_card_payment")
assert_equal @loan_payments, @matcher.match("loan_payments_personal_loan_payment")
assert_equal @loan_payments, @matcher.match("loan_payments_mortgage_payment")
assert_equal @loan_payments, @matcher.match("loan_payments_student_loan_payment")
assert_equal @loan_payments, @matcher.match("loan_payments_other_payment")
assert_equal @fees, @matcher.match("bank_fees_atm_fees")
assert_equal @fees, @matcher.match("bank_fees_foreign_transaction_fees")
assert_equal @fees, @matcher.match("bank_fees_insufficient_funds")
assert_equal @fees, @matcher.match("bank_fees_interest_charge")
assert_equal @fees, @matcher.match("bank_fees_overdraft_fees")
assert_equal @fees, @matcher.match("bank_fees_other_bank_fees")
assert_equal @entertainment, @matcher.match("entertainment_casinos_and_gambling")
assert_equal @entertainment, @matcher.match("entertainment_music_and_audio")
assert_equal @entertainment, @matcher.match("entertainment_sporting_events_amusement_parks_and_museums")
assert_equal @entertainment, @matcher.match("entertainment_tv_and_movies")
assert_equal @entertainment, @matcher.match("entertainment_video_games")
assert_equal @entertainment, @matcher.match("entertainment_other_entertainment")
assert_equal @food_and_drink, @matcher.match("food_and_drink_beer_wine_and_liquor")
assert_equal @food_and_drink, @matcher.match("food_and_drink_coffee")
assert_equal @food_and_drink, @matcher.match("food_and_drink_fast_food")
assert_equal @groceries, @matcher.match("food_and_drink_groceries")
assert_equal @restaurant, @matcher.match("food_and_drink_restaurant")
assert_equal @food_and_drink, @matcher.match("food_and_drink_vending_machines")
assert_equal @food_and_drink, @matcher.match("food_and_drink_other_food_and_drink")
assert_equal @shopping, @matcher.match("general_merchandise_bookstores_and_newsstands")
assert_equal @clothing, @matcher.match("general_merchandise_clothing_and_accessories")
assert_equal @shopping, @matcher.match("general_merchandise_convenience_stores")
assert_equal @shopping, @matcher.match("general_merchandise_department_stores")
assert_equal @shopping, @matcher.match("general_merchandise_discount_stores")
assert_equal @shopping, @matcher.match("general_merchandise_electronics")
assert_equal @shopping, @matcher.match("general_merchandise_gifts_and_novelties")
assert_equal @shopping, @matcher.match("general_merchandise_office_supplies")
assert_equal @shopping, @matcher.match("general_merchandise_online_marketplaces")
assert_equal @shopping, @matcher.match("general_merchandise_pet_supplies")
assert_equal @shopping, @matcher.match("general_merchandise_sporting_goods")
assert_equal @shopping, @matcher.match("general_merchandise_superstores")
assert_equal @shopping, @matcher.match("general_merchandise_tobacco_and_vape")
assert_equal @shopping, @matcher.match("general_merchandise_other_general_merchandise")
assert_equal @home, @matcher.match("home_improvement_furniture")
assert_equal @home, @matcher.match("home_improvement_hardware")
assert_equal @home, @matcher.match("home_improvement_repair_and_maintenance")
assert_equal @home, @matcher.match("home_improvement_security")
assert_equal @home, @matcher.match("home_improvement_other_home_improvement")
assert_equal @medical, @matcher.match("medical_dental_care")
assert_equal @medical, @matcher.match("medical_eye_care")
assert_equal @medical, @matcher.match("medical_nursing_care")
assert_equal @medical, @matcher.match("medical_pharmacies_and_supplements")
assert_equal @medical, @matcher.match("medical_primary_care")
assert_equal @medical, @matcher.match("medical_veterinary_services")
assert_equal @medical, @matcher.match("medical_other_medical")
assert_equal @personal_care, @matcher.match("personal_care_gyms_and_fitness_centers")
assert_equal @personal_care, @matcher.match("personal_care_hair_and_beauty")
assert_equal @personal_care, @matcher.match("personal_care_laundry_and_dry_cleaning")
assert_equal @personal_care, @matcher.match("personal_care_other_personal_care")
assert_equal @services, @matcher.match("general_services_accounting_and_financial_planning")
assert_equal @car, @matcher.match("general_services_automotive")
assert_equal @services, @matcher.match("general_services_childcare")
assert_equal @services, @matcher.match("general_services_consulting_and_legal")
assert_equal @services, @matcher.match("general_services_education")
assert_equal @services, @matcher.match("general_services_insurance")
assert_equal @services, @matcher.match("general_services_postage_and_shipping")
assert_equal @services, @matcher.match("general_services_storage")
assert_equal @services, @matcher.match("general_services_other_general_services")
assert_equal @giving, @matcher.match("government_and_non_profit_donations")
assert_nil @matcher.match("government_and_non_profit_government_departments_and_agencies")
assert_nil @matcher.match("government_and_non_profit_tax_payment")
assert_nil @matcher.match("government_and_non_profit_other_government_and_non_profit")
assert_equal @transportation, @matcher.match("transportation_bikes_and_scooters")
assert_equal @transportation, @matcher.match("transportation_gas")
assert_equal @transportation, @matcher.match("transportation_parking")
assert_equal @transportation, @matcher.match("transportation_public_transit")
assert_equal @transportation, @matcher.match("transportation_taxis_and_ride_shares")
assert_equal @transportation, @matcher.match("transportation_tolls")
assert_equal @transportation, @matcher.match("transportation_other_transportation")
assert_equal @trips, @matcher.match("travel_flights")
assert_equal @trips, @matcher.match("travel_lodging")
assert_equal @trips, @matcher.match("travel_rental_cars")
assert_equal @trips, @matcher.match("travel_other_travel")
assert_equal @home, @matcher.match("rent_and_utilities_gas_and_electricity")
assert_equal @home, @matcher.match("rent_and_utilities_internet_and_cable")
assert_equal @home, @matcher.match("rent_and_utilities_rent")
assert_equal @home, @matcher.match("rent_and_utilities_sewage_and_waste_management")
assert_equal @home, @matcher.match("rent_and_utilities_telephone")
assert_equal @home, @matcher.match("rent_and_utilities_water")
assert_equal @home, @matcher.match("rent_and_utilities_other_utilities")
end
test "matches income categories" do
assert_equal @dividend_income, @matcher.match("income_dividends")
assert_equal @interest_income, @matcher.match("income_interest_earned")
assert_equal @income, @matcher.match("income_tax_refund")
assert_equal @income, @matcher.match("income_retirement_pension")
assert_equal @income, @matcher.match("income_unemployment")
assert_equal @income, @matcher.match("income_wages")
assert_equal @income, @matcher.match("income_other_income")
end
end

View file

@ -0,0 +1,80 @@
require "test_helper"
class Provider::PlaidTest < ActiveSupport::TestCase
setup do
# Do not change, this is whitelisted in the Plaid Dashboard for local dev
@redirect_url = "http://localhost:3000/accounts"
# A specialization of Plaid client with sandbox-only extensions
@plaid = Provider::PlaidSandbox.new
end
test "gets link token" do
VCR.use_cassette("plaid/link_token") do
link_token = @plaid.get_link_token(
user_id: "test-user-id",
webhooks_url: "https://example.com/webhooks",
redirect_url: @redirect_url
)
assert_match /link-sandbox-.*/, link_token.link_token
end
end
test "exchanges public token" do
VCR.use_cassette("plaid/exchange_public_token") do
public_token = @plaid.create_public_token
exchange_response = @plaid.exchange_public_token(public_token)
assert_match /access-sandbox-.*/, exchange_response.access_token
end
end
test "gets item" do
VCR.use_cassette("plaid/get_item") do
access_token = get_access_token
item = @plaid.get_item(access_token).item
assert_equal "ins_109508", item.institution_id
assert_equal "First Platypus Bank", item.institution_name
end
end
test "gets item accounts" do
VCR.use_cassette("plaid/get_item_accounts") do
access_token = get_access_token
accounts_response = @plaid.get_item_accounts(access_token)
assert_equal 4, accounts_response.accounts.size
end
end
test "gets item investments" do
VCR.use_cassette("plaid/get_item_investments") do
access_token = get_access_token
investments_response = @plaid.get_item_investments(access_token)
assert_equal 3, investments_response.holdings.size
assert_equal 4, investments_response.transactions.size
end
end
test "gets item liabilities" do
VCR.use_cassette("plaid/get_item_liabilities") do
access_token = get_access_token
liabilities_response = @plaid.get_item_liabilities(access_token)
assert liabilities_response.credit.count > 0
assert liabilities_response.student.count > 0
end
end
private
def get_access_token
VCR.use_cassette("plaid/access_token") do
public_token = @plaid.create_public_token
exchange_response = @plaid.exchange_public_token(public_token)
exchange_response.access_token
end
end
end