Tax Treatment Feature Implementation
Overview
This feature adds tax treatment tracking to all account types (Plaid, Manual, and SnapTrade) to enable better tax estimates, financial planning, and LLM-powered advice.Changes Made
1. Database Schema (sql-queries/add-tax-treatment-support.sql)
- Added
tax_treatmentcolumn toaccounts,manual_accounts, andsnaptrade_accountstables - Added CHECK constraint to ensure values are:
'taxable','tax_deferred', or'tax_free' - Backfilled existing accounts with smart defaults based on account type/subtype
- Verification queries included at bottom of file
2. Utility Library (lib/tax-treatment-utils.ts)
getDefaultTaxTreatment()- Determines smart default based on account type/subtypegetTaxTreatmentLabel()- Human-readable labels for UI displaygetTaxTreatmentDescription()- Descriptions for UI tooltips/help text
- Tax Free: Roth IRA, Roth 401k, HSA, 529A
- Tax Deferred: Traditional IRA, 401k, 403b, SEP IRA, Pension
- Taxable: Brokerage, Checking, Savings, Money Market
- Null: Loans and Credit (not applicable)
3. TypeScript Interfaces Updated
hooks/queries/use-accounts.ts- Addedtax_treatmenttoAccountinterfacehooks/queries/use-manual-accounts.ts- Added to all manual account interfaces- Updated account mapping in
fetchAccounts()to include tax_treatment field
4. UI Components
RadioGroup Component (components/ui/radio-group.tsx)
- Created new shadcn-style RadioGroup component using Radix UI
components/edit-manual-account-dialog.tsx)
- Added tax treatment selector (radio buttons)
- Only shows for non-loan/credit accounts
- Pre-selects smart default on account load
- Saves tax_treatment on update
Installation Required
You need to install the Radix UI radio-group package:Next Steps
1. Run Database Migration
2. Install Dependencies
3. Add Tax Treatment to Other Account Edit Dialogs
For Plaid Accounts: You’ll need to create/update an edit dialog for Plaid accounts similar to the manual account dialog. For SnapTrade Accounts: Similar edit dialog would be needed for SnapTrade accounts.4. Add Tax Treatment to Account Creation Flows
- Update manual account creation dialogs
- Add to SnapTrade connection flow (if applicable)
- Consider adding to Plaid connection confirmation
5. LLM Integration
Update the system prompt for Silvia (your AI assistant) to include:6. Tax Estimate Feature (Future)
The tax_treatment field enables features like:- Estimated tax on liquidation
- Optimal withdrawal order calculator
- Asset location analyzer (which assets belong in which account types)
- Tax projection for retirement planning
Usage Examples
Get Default Tax Treatment
Get Human-Readable Labels
Calculate Liquidation Tax Estimate
Use in Financial Data Cache
ThegetFinancialMetrics() function now returns tax treatment breakdowns:
Benefits
- Tax Estimates: Calculate potential tax liability on account liquidation
- Better LLM Context: Silvia can provide tax-aware advice and recommendations
- Financial Planning: Proper asset location and tax-efficient strategies
- Withdrawal Optimization: Determine optimal order to withdraw from accounts
- User Education: Help users understand tax implications of their accounts
Files Modified/Created
Created:
sql-queries/add-tax-treatment-support.sql- Database migrationlib/tax-treatment-utils.ts- Smart default logic and utilitieslib/tax-estimate-utils.ts- Tax estimate calculationscomponents/ui/radio-group.tsx- RadioGroup componentdocs/TAX-TREATMENT-IMPLEMENTATION.md- This documentation
Modified:
hooks/queries/use-accounts.ts- Added tax_treatment to Account interface and querieshooks/queries/use-manual-accounts.ts- Added tax_treatment to manual account interfacescomponents/edit-manual-account-dialog.tsx- Added tax treatment UI selector with PostHog trackingapp/accounts/page.tsx- Added tax treatment UI selector with PostHog tracking, optimized save logicapp/api/accounts/update-tax-treatment/route.ts- API endpoint to save tax_treatment (updates updated_at timestamp)app/api/accounts/update-type/route.ts- Updated to set updated_at timestamp on type/subtype changesapp/api/connect-bank/route.ts- Set tax_treatment defaults on Plaid account creationapp/api/relink/route.ts- Removed duplicate tax_treatment logic (uses migration defaults instead)app/api/snaptrade/process-connection/route.ts- Set tax_treatment defaults on SnapTrade account creationapp/api/cron/process-daily-snapshot/route.ts- Save tax_treatment in daily snapshots for historical trackinglib/financial-data-cache.ts- Added tax_treatment to all queries, refactored getUnrealizedGainsTaxDetails() to use tax_treatment column (removed 30 lines of hardcoded logic)hooks/queries/use-unrealized-gains.ts- Refactored to use centralized getDefaultTaxTreatment() utility (removed 70 lines of duplicate logic)components/tax-estimate-card.tsx- Updated dialog description to reflect tax_treatment usage
Testing Checklist
- Install @radix-ui/react-radio-group dependency
- Run database migration
- Verify existing accounts have tax_treatment backfilled
- Test editing manual account and updating tax treatment
- Verify smart defaults are working correctly
- Test that loan/credit accounts don’t show tax treatment selector
- Test Plaid accounts include tax_treatment in queries
- Test SnapTrade accounts include tax_treatment in queries