24 Commits

Author SHA1 Message Date
siddharthd 5df164e9a5 feat(secrets): add SOPS+age encryption for .env
.env encrypted to .env.sops using shared Unraid age key. .gitignore
updated to allow .env.sops while still blocking plain .env.
2026-05-15 19:28:13 +10:00
siddharthd b8cd1b0f89 fix(reconcile): prevent split/tag double-counting on reconciled transactions
Move splits, tags and overrides from manual to statement side on reconcile
(delete from manual after copying) instead of just copying. Add read-time
filter to exclude reconciled manual transactions from balance and shared
transaction queries. Also adds participant filter to shared expenses page.
2026-05-11 19:15:41 +10:00
siddharthd ce67e38d77 docs: update CLAUDE.md and README to reflect recent changes
- Add reconciled_with_id and created_at columns to transactions table docs
- Document split_payments, expense_metadata, rule_apply_runs tables
- Update /api/transactions route docs with has_split filter and all sort options
- Add /api/transactions/reconcile and /api/split-payments to API table
- Document import date (created_at) behaviour and reconciliation caveat
- Add Prisma regeneration note to CLAUDE.md
- Note schema drift for tables added without migration files
2026-05-10 16:52:55 +10:00
siddharthd b8296b6e29 fix(prisma): sync schema with DB
Add missing models: statements, transactions, expense_metadata, rule_apply_runs.
Fixes pre-existing type errors on split_payments and my_share_percent which
were caused by a stale generated client (regenerate with npx prisma generate).
2026-05-10 16:13:40 +10:00
siddharthd 0c1f88ed9c feat(transactions): add imported date column, split filter, and sortable columns
- Show created_at as "Imported" column in transactions and shared views
- For reconciled transactions, show original CSV import date (not statement processing date) via LEFT JOIN on reconciled_with_id
- Add has_split filter (all/split only/unsplit only) to transactions page
- Transactions table: sortable by imported date; split filter dropdown
- Shared table: client-side sort by date, imported, and amount
2026-05-10 16:04:54 +10:00
siddharthd 4a49add277 feat: CSV import and batch reconciliation UI
- Add reconciled_with_id column to transactions (links manual → statement tx)
- CSV import wizard: 4-step modal (upload → map columns → review → done)
  - Handles any bank format via column mapping with localStorage presets
  - Single signed or separate debit/credit column modes
  - Editable preview table before committing
  - Auto-tags all imported rows with 'csv-import'
- Batch reconcile page: shows all unreconciled manual transactions with
  potential statement matches (date ±3 days, amount ±1%) pre-fetched
  - Select matches across multiple rows, apply all at once
  - Copies overrides/tags/splits from manual → statement tx atomically
  - Manual tx marked reconciled (linked), hidden from main transactions view
  - Transactions with no matches shown separately
- Import CSV button on transactions page
- Reconcile nav item in sidebar
2026-04-13 06:23:08 +10:00
siddharthd 07b8c1ef16 fix(shared): exclude payments from balance when tag filter is active 2026-04-13 05:41:00 +10:00
siddharthd 1b561af9e9 feat(filters): add No tags filter to transactions and shared pages 2026-04-13 05:20:49 +10:00
siddharthd 1296555f17 test: add unit and integration test suites
- Extract evaluateCondition + rule types into src/lib/rules.ts for testability
- 48 unit tests for evaluateCondition (all fields/operators) and formatCategory
- 21 integration tests for getTransactions filters and getParticipantBalances
- Vitest configs for unit (vitest.config.ts) and integration (vitest.integration.config.ts)
- setup-test-db.sh creates personal_test DB from production schema via pg_dump
- Use vi.doMock + dynamic import pattern to isolate test DB from Prisma singleton
2026-04-01 19:59:29 +11:00
siddharthd 7491e70a15 fix(participants): show Me contextually per logged-in user
Participant id=1 was named "Me" in the DB, causing Sonu and other users
to see "Me" referring to Siddharth when viewing splits and shared expenses.

- Rename participant id=1 from "Me" to "Siddharth" in the DB
- /api/participants now substitutes "Me" for whichever participant matches
  the current user, so the label is always relative to the viewer
- split-modal: default split uses currentUser.id instead of name === "Me"
- transactions/page: filter and display logic uses participant ID not name
- shared/page: split chips show "Me" when participant_id === current user

Also includes add-transaction-modal tags support (pre-existing staged change).
2026-04-01 18:36:29 +11:00
siddharthd 0a1f6b48a2 feat(ui): mobile-responsive sidebar + rules improvements
- Sidebar: hidden on mobile, opens as slide-out drawer with hamburger
  toggle; auto-closes on navigation; desktop layout unchanged
- Layout: responsive padding accounting for mobile header bar
- Rules: add tag as a condition field (has/not-has tag)
- Rules: apply a single rule via per-rule Apply button
- Rules: splits-from defaults to 2026-01-09
2026-03-21 08:33:08 +11:00
siddharthd ef73a9cea0 fix(payment): participantId stuck as empty string when participants load async 2026-03-14 21:33:25 +11:00
siddharthd d53d3106f2 fix(shared): tag filter SQL precedence, balance cards filter by tag 2026-03-14 21:30:33 +11:00
siddharthd 02ac136e19 fix(payment): crash on open due to amount.toFixed on numeric string
feat(shared): tag filter on shared transactions list
2026-03-14 21:27:08 +11:00
siddharthd 084b8764e3 feat(transactions): Payment button to record existing transaction as debt payment 2026-03-14 21:20:25 +11:00
siddharthd 281f0d3782 fix(shared): show full description and notes in split transactions table 2026-03-14 21:11:34 +11:00
siddharthd 85e7801407 feat(shared): replace settle buttons with payment ledger
- New split_payments table records actual payments between participants
- Balance = total split obligations - total payments (splits never marked settled)
- Record Payment modal per participant: direction toggle, amount pre-filled with balance, date, notes
- Payment history inline on each balance card with +/- display and delete
- Per-transaction Settle button removed; Action column removed from shared table
- Splits always show the true cost breakdown regardless of payment state
2026-03-14 21:09:00 +11:00
siddharthd 5206388958 feat(filters): smart query bar with amount operators and multi-select dropdowns
- Query bar parses >500, >=500, <500, <=500, 500-1500 into amount_min/max filters
- Parsed tokens shown as dismissable chips below the query bar
- Category, Bank, Tag, Type filters upgraded from single-select to multi-select
- MultiSelect dropdown component with checkbox list and active-state border
- Backend: TransactionFilters uses string[] for categories/bank_names/tag_ids/transaction_types
- SQL: ANY($n::text[]) / ANY($n::int[]) for array filters
2026-03-14 20:39:28 +11:00
siddharthd 8076d1a949 docs: update README and add CLAUDE.md for finance app 2026-03-14 20:06:37 +11:00
siddharthd aeaca84cc7 feat(edit-transaction): edit modal with notes, inline tags, and split management
- New EditTransactionModal with scrollable body (sticky header/footer)
- Statement transactions: read-only core fields; manual transactions: editable date/amount/description
- Override fields for all: merchant, category, type, notes (textarea)
- InlineTags sub-component: add/remove tags without dropdown clipping issues
- Live split display via useTransactionSplits, opens SplitModal for editing
- PATCH /api/transactions/:id extended for description/amount/transaction_date (manual only)
- Transactions page: edit button per row, notes shown below description in italic
2026-03-14 20:06:32 +11:00
siddharthd 278e57354c feat(insights): analytics drill-down, fee tracking, and category improvements
- Monthly spend chart with category breakdown drill-down
- Merchant frequency and spend analytics with per-merchant history
- Subscription detection and recurring charge tracking
- Fee and interest analytics endpoint
- Expanded category list with formatCategory display helper
2026-03-14 20:06:24 +11:00
siddharthd 9f90d8726f feat(rules): apply_split rules with run history and revert
- POST /api/rules/apply — run all enabled rules against unmatched transactions
- POST /api/rules/apply/:id — apply a single rule by id
- DELETE /api/rules/apply/:id — revert a rule run (remove applied splits)
- Rules page: show run history with revert button, apply individual rules
2026-03-14 20:06:19 +11:00
siddharthd 859043f5a5 feat(shared): bidirectional split balance, credit direction, and multi-user view
- Rewrite participant balance to UNION both directions (they owe me + I owe them)
- Credits/refunds subtract from owed amount for correct net balance
- Allow secondary users to see transactions split with them
- Add participant balance cards with colour-coded owe direction
- Add inline AddParticipantForm with name + optional email
2026-03-14 20:06:13 +11:00
siddharthd fc22a61a43 feat(transactions): manual transaction support and multi-owner query infrastructure
- Add POST /api/transactions to create manual transactions (statement_id=NULL, owner_id set directly)
- Queries switch from JOIN to LEFT JOIN statements so manual transactions are visible
- COALESCE(t.owner_id, s.owner_id) throughout for owner resolution
- Add "Manual" bank filter option in getTransactions
- Search extended to include merchant_normalized override
- Split data fetched via lateral subquery on every transaction row
- getParticipantBalances rewritten as UNION for bidirectional net balances
  (credits/refunds negate, split from either side of the relationship)
- getSharedTransactions: remove my_share_percent from SELECT (fixes GROUP BY error),
  WHERE rewritten as two distinct cases (owner with others split vs participant on others' txn)
- getTransactions: OR EXISTS condition so split participants see shared transactions
- add-transaction-modal component for creating manual transactions with splits
- 0008_my_share_percent migration adds my_share_percent to transaction_overrides
2026-03-14 20:04:00 +11:00
54 changed files with 6478 additions and 390 deletions
+15
View File
@@ -0,0 +1,15 @@
#ENC[AES256_GCM,data:8Y6694wDaHKDf03nhurNnRYhbMv4TXBNI7HJnh0I4jM0tq1W4iq0NLgQ4W0wDx/cVat0szyrq/tAYsUj+LuRMMdHEY3Fcdp51MT2KtGXEa9kw/Wp,iv:jJTLMVvpbW7cKOGWV6x2JuAqkx6QgOKlw4K9NXA9fa4=,tag:9DhD7Zm9BtJPPH+mXjzZuQ==,type:comment]
#ENC[AES256_GCM,data:HMMVGfE8jPTsDyz9y0d+vfyRaf0B3segW04TFPIZDtJBlDD/RUMbv3KylOm0e+iBkJQFAIeAgBrvxr9+myD7XwYWJtIjVYrcgktHlcC8GoH4cotGDQni58uhMknlF33TdOVxTxs=,iv:4FH4Qbe4LMdUiIRLhhr+0Dnp7tT85EwQ0mcHqfz3T+Y=,tag:DT4muDzq/cKeGhgriX+Rbw==,type:comment]
#ENC[AES256_GCM,data:L39MkIKSEcj9C/aXvlz9zPDvR0Awgp4OYVXYX0ctIuwCKq0qmbApFv+fj6YbcnS7vlWttlDcyypnF8KbJe+0bDFRLN+MBzmWjMUhjKPz8eYe+v6gp9Mz,iv:1H88VnLGt6P81KP3e3lonvpL/OhtBCardjhPT0hCOag=,tag:jJmHguBHWp0Kl3Je0bEG3w==,type:comment]
#ENC[AES256_GCM,data:BBpTe55+cpatCSGS4g7YaWb0PRS4QjbrUE+mG2CzVmtS63s5onN22swVYtQw/NaMcv8qkrR2imww/vHnH/Z98IuJpAZIy8CXTMGNzXvFU82LNFIcb2QPZ1bWHHWLJfj7EHoDRgCBvuDkO8cAORgmI1U3m1ekZe82,iv:fI7a+edL5LpLVt+2vD75YwcxzHaO/Hz6OfPylSCTYEs=,tag:leDqySg/m9G8vlddrhwWhg==,type:comment]
#ENC[AES256_GCM,data:hKjWUHaA7rYY/VjDprmcQ++mphIBzPyFJVylFGyDEfRu4pSoFN3ZGD3B0H8P2wiN8IarrJz4p6hQzfrB248lEojxKBoUmOU1Dxegv1qhiNO0liIQnZWTtKrenz4zzOdb3sw=,iv:CfydB3duDyuuSphNz5OuZQMbWpJBW0u1Os94KeclgXw=,tag:PAn6S9DCeizHcaxCvS+jdg==,type:comment]
#ENC[AES256_GCM,data:hVUROHUoWJuBofyc4qdzTC1+PrecP8jG/1i/xddsTxthNwhYBiEcvq2aJhfXggbNSC1+PJ8AGg2sCrHL3NfD7KBAX33GgdDx0JAeHsyfsHN9hc583cjeVQDlOmocHGrBkGFTOi1g,iv:uiVFAPKXCQhTUFUSj0yPaX3l+eDmhsrwRrPQkTaksig=,tag:pyW76cx0dPErUWXoxW95+A==,type:comment]
#ENC[AES256_GCM,data:BOynLXwT+rKrTd1dxnVjveABiJaA94Zl2UQ/yPg5It+cbUNUpJ/GMlrR5OLJQMkw5OD+EkeEMqieY0KYTAualEKOoaIG6T+c0zZAjYNrmWBZYrPuOtpLk4Bzwuel0agrFAs3xqvmsk/OuktQNyvmXMgUpwnzD0U=,iv:WZn4CCL0tzzdorI0zl/mqE+/MMkNtkIyo1SBHpboFw4=,tag:kY/+mg9qyLAxC27UwnXacA==,type:comment]
#ENC[AES256_GCM,data:KzC0cK7daY++mskeHmBEjlmDvmC2oncQ18mxwB1tGS0hp2X7lOOYgMYnRwGZRy5Gd0ymfJF8M52GDIV7VGKsOFT2Rt+T3EcCJ8ng1Q1EJpTPk1139g27,iv:ZTg+sbmSuX3oJrf6AWkWFwV2eS+D7j0RNP57d6TGh6A=,tag:bTtnmShFZPxNi9hzI3PVrQ==,type:comment]
DATABASE_URL=ENC[AES256_GCM,data:fnfh3OD0nX7xmdCy74/8uoDxu7JNsplPbJeZ/YUrwzrr5BrdJx5+CUbIc/BDx1I1+sId0tu5G2rx1GiPJUKSVw9fKMLrP2yXk8EUjAKcTl1ozOEAxVsW6S/0fqjdBheK2iLgomcQ6LLhT2CHBUadmPNoOEpuYGRY16akPotafWGBGxZiVmg9zD0DZwgxY0ASJ6NQKn5Fup1+FAkSuLPV5Ok1evDecVol9La0bp3pVT6kX0bWQlP8SrxOpznZ2XQbHjXU6xNXJvaA2mVeP6qjCDxJbXX8BB+I52l1ZR5mQ7RZSpossbwT+q6yXGmxUVKDP54oqq7eMasNQ4AkpWrgTSSivogBATuGQyVvBUEeymRWfPRayliGMoEbiITO3Lf6/Z+u+xcAWDUoThJZ3LemScC/RgRN/PG3Dnx1b/uF0piFRDF59xNela515QlQqUDiwoxAdO7hhY1l/gH/VwZEvp4GYJgMqTIP+85Ta2y0Ph4z9clSSHOc/dkAnknRyQDCpbtDSR+DCppzQ9OgUzPK7Popqa1k/yO6Oo+1OTpov3I18Yyh+Vy00kYx9rR2AAp5C32K2rYkdYrEiK3erIBhKNPrLrYRiT/2LReArMAxpUk1iM9uDPnx/JEhISJRlD+PWcF1R8i8CiBc+OhTFOlhKykEUdpXBv4eBzqB0tSwsEnT/zJzMLvqiVoYX0fnGlS61Lg06ZIRohzQLZAU6A+m0rOOFmRkHS6oZbkOnRxf8c5QfjVUq5WxmPNEFWHsIi2LsbjkDLk9yoDLIXflO+G2sP6JB/CWvCXAK++5MiS0G6ijNBEi+kjjHokE/Snpaq+Uw7JynAFKJ0QVMT7YP9YGfVJu2cYIvEkpWtz8,iv:D2HI2gwY9R7jXFsClkV7qKDDxvBips8BVAGOPKLNZCk=,tag:cY8AgAkGXJQZpbUsQBiRxQ==,type:str]
sops_age__list_0__map_enc=-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBkSGtocXNOZXU0NUZSZlBY\nYmpMbnJVbVVSV2xnWVdwUVBObWtIb05raW1jCkdJRzUyTlBGSjFISks3OHN0clJY\ncmluR0o0aGlXU3VOaVVUc0t5RWk4NVUKLS0tIHdZUVZrd3JxUmpnbWJkUzZBaHRx\nZ2lKK0NZT0RjU1J0andQMVRPMGdoL2sKlgWx9xKOabP5q4cmHPVVD7xuwn6/OV6V\nZR6MXV07XzLfUl3G5NMeOBI4e6s9y+xZGSoDWeWB194euASTlyiwwA==\n-----END AGE ENCRYPTED FILE-----\n
sops_age__list_0__map_recipient=age1tw6wsyxgxa465cc0wx32u7xuw5675pyz35cuzey4huz69hc54v4qp0pvgn
sops_lastmodified=2026-05-15T09:13:11Z
sops_mac=ENC[AES256_GCM,data:BhA6Tbo2bNOYzuoYBd/3nYp9CrZx7Nspl6Us1hpUIXR/EEOLRUCofqbVyDzU+2CNQMNivPYyltQ4nRdapDQwKWlZjTEufSkRKNwafr8eexN3Dwo1j3Njw9bK26ytG/d1Dm3jXOKogRRUZYsQqtHZBmZwuF4JvL49x+ODQ0Hco1o=,iv:6cIK8//MbBoiPSsxJnp7oKiz04B1hxa4vK0QFEBzHP4=,tag:TT5b8KSO+UUZJ/hOJqkMpw==,type:str]
sops_unencrypted_suffix=_unencrypted
sops_version=3.9.4
+1
View File
@@ -32,6 +32,7 @@ yarn-error.log*
# env files (can opt-in for committing if needed)
.env*
!.env.sops
# vercel
.vercel
+3
View File
@@ -0,0 +1,3 @@
creation_rules:
- path_regex: \.env$
age: age1tw6wsyxgxa465cc0wx32u7xuw5675pyz35cuzey4huz69hc54v4qp0pvgn
+137
View File
@@ -0,0 +1,137 @@
# CLAUDE.md
Guidance for Claude Code when working in this repository.
## Project Overview
Personal finance tracker. Bank statements are ingested via an N8N workflow (in the smarthome repo at `docker/automation/workflows/cc-statement-processor-paperless.json`) that sends PDFs to Gemini 2.5 Flash for extraction, then inserts into PostgreSQL.
- **App**: Next.js 16 App Router, TypeScript, Tailwind CSS
- **DB**: PostgreSQL container `postgres-personal`, database `personal`, user `personal`
- **Auth**: `X-Forwarded-User` header (email) set by Traefik → `participants.email`. In dev/fallback: participant id=1 ("Me")
- **Runs at**: port 3000 inside container, exposed on host port 4100, proxied at `https://finance.bosecamp.com`
## Common Commands
```bash
# Build and deploy (from smarthome repo root)
docker compose --env-file docker/common.env --env-file docker/finance/.env \
-f docker/finance/docker-compose.yml up -d --build
# IMPORTANT: docker restart does NOT pick up a new image — always use the compose command above
# DB access
docker exec postgres-personal psql -U personal -d personal
# View logs
docker logs finance -f
```
## Architecture
### Key Files
| File | Purpose |
|------|---------|
| `src/lib/db.ts` | `queryRaw<T>()` — the only DB query function; uses `pg` directly |
| `src/lib/queries.ts` | All SQL query functions (no ORM); import `queryRaw` from `@/lib/db` |
| `src/lib/hooks.ts` | TanStack Query hooks for all API calls |
| `src/lib/auth.ts` | `getCurrentUser()` — reads `X-Forwarded-User` header |
| `src/lib/categories.ts` | Canonical category list (`CATEGORIES` array + `formatCategory()`) |
| `src/app/api/*/route.ts` | API route handlers |
| `src/components/` | Shared UI components |
### Data Flow
- All queries in `src/lib/queries.ts` use raw SQL via `queryRaw` from `src/lib/db.ts`
- API routes call query functions and return `NextResponse.json()`
- Frontend uses hooks from `src/lib/hooks.ts` (TanStack Query) — never fetches directly
- Auth is always checked first in every API route: `const user = await getCurrentUser(req)`
### Owner Scoping
All data is scoped by `owner_id`. The effective owner of a transaction is:
```sql
COALESCE(t.owner_id, s.owner_id)
```
- Statement-linked transactions: owner comes from `statements.owner_id`
- Manual transactions: `statement_id IS NULL`, owner stored directly in `transactions.owner_id`
The effective merchant and category always prefer overrides:
```sql
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) -- merchant
COALESCE(o.category_override, t.category) -- category
```
## Database
```bash
# Schema inspection
docker exec postgres-personal psql -U personal -d personal -c "\d transactions"
# Apply a migration SQL file
docker exec postgres-personal psql -U personal -d personal < prisma/migrations/<name>/migration.sql
```
### Key Tables
- `statements` — one row per billing period per bank account
- `transactions` — line items; `statement_id` is nullable (NULL = manual entry); `reconciled_with_id` links a manual tx to its matched statement tx
- `transaction_overrides` — user corrections to AI-extracted data (category, merchant, notes)
- `transaction_splits` — shared expense tracking (participant, share_percent, settled)
- `split_payments` — recorded cash settlements between participants
- `transaction_tags` — many-to-many join to `tags`
- `rules` — auto-categorisation rules (JSONB conditions + actions)
- `rule_apply_runs` — audit log of bulk rule-apply runs with full snapshot for revert
- `expense_metadata` — enrichment from email receipts; `transaction_id` nullable until reconciled
- `participants` — people; `id=1` is "Me" (the primary user)
- `account_owner_mappings` — persists bank+account → owner assignments
### Import Date (`created_at`)
`transactions.created_at` is the import timestamp (DB default `now()`). In the transactions and shared views, the "Imported" column shows:
- For statement transactions: when the statement was processed by N8N
- For reconciled transactions: the `created_at` of the original manual/CSV transaction (via `LEFT JOIN transactions src ON src.reconciled_with_id = t.id`) — so the original import date is preserved post-reconciliation
Use `created_at` (not `transaction_date`) to answer "what was added since the last settlement?". Sort by `created_at` is supported server-side in `getTransactions` and client-side in the shared view.
### Rules System
Conditions are AND-evaluated. Fields: `merchant_normalized`, `description`, `category`, `bank_name`, `amount`, `transaction_type`. Operators: `contains`, `equals`, `starts_with`, `gt`, `lt`, `not_equals`. Actions: `set_category`, `set_merchant`, `add_tag_ids`, `apply_split`.
`contains` and `equals` operators are case-insensitive (both sides `.toLowerCase()`).
## Development Patterns
### Adding a new API route
1. Create `src/app/api/<resource>/route.ts`
2. Always call `getCurrentUser(req)` first; return 403 if null
3. Write SQL in `src/lib/queries.ts` using `queryRaw`
4. Add a TanStack Query hook in `src/lib/hooks.ts`
### Adding a new condition field to rules
Two files only:
- `src/app/api/rules/apply/route.ts` — add to `Condition.field` union, `TxFields` interface, and `evaluateCondition()` switch
- `src/app/rules/page.tsx` — add to `FIELDS` array; add special rendering if needed (e.g. enum dropdown for `transaction_type`)
### Modifying queries
- All JOINs to `statements` must be `LEFT JOIN` (manual transactions have no statement)
- Owner filter pattern: `WHERE COALESCE(t.owner_id, s.owner_id) = $1`
- Bank name pattern: `COALESCE(s.bank_name, 'Manual') as bank_name`
### Prisma
The schema at `prisma/schema.prisma` covers all tables. The generated client (gitignored) must be regenerated after schema changes:
```bash
cd /mnt/m2cache/appdata/finance-app && npx prisma generate
```
Docker builds run `npx prisma generate` automatically. Do not commit `src/generated/prisma/` — it is gitignored.
## Known Gaps / TODOs
See `README.md`**Known Gaps / TODOs** for full details.
**Payment provider tracking**: `merchant_normalized` currently conflates payment provider (PayPal, Afterpay, Zip) with the actual merchant. Plan: add `payment_provider` column, update Gemini prompt to extract it separately, backfill from `merchant_name` patterns, surface in UI filters.
+81 -4
View File
@@ -58,7 +58,8 @@ One row per line item within a statement. Cascade-deleted when the parent statem
| Column | Type | Description |
|--------|------|-------------|
| `id` | int | Primary key |
| `statement_id` | int FK → `statements` | Parent statement |
| `statement_id` | int FK → `statements` (nullable) | Parent statement; NULL for manually-entered transactions |
| `owner_id` | int FK → `participants` (nullable) | Owner for manual transactions (no statement); statement-linked transactions derive owner from `statements.owner_id` |
| `transaction_date` | date | Date of transaction |
| `description` | text | Raw description from the statement |
| `amount` | numeric | Original amount in statement currency |
@@ -71,6 +72,8 @@ One row per line item within a statement. Cascade-deleted when the parent statem
| `foreign_currency_code` | text | Foreign currency code (e.g. `USD`) |
| `category` | text | AI-assigned category (see category taxonomy below) |
| `row_index` | int | Position in statement — used for deduplication |
| `reconciled_with_id` | int FK → `transactions` (nullable) | Links a manually-entered transaction to its matching statement transaction after reconciliation |
| `created_at` | timestamptz | When the row was inserted — the "import date". For reconciled transactions the UI shows the original manual/CSV `created_at`, not the statement's |
**Deduplication**: unique index on `(statement_id, transaction_date, description, amount, row_index)`.
@@ -164,8 +167,58 @@ Saved auto-categorisation rules. Applied in bulk via the Rules page.
| `enabled` | bool | |
| `priority` | int | Higher priority rules run first |
**Condition fields**: `merchant_normalized`, `description`, `category`, `bank_name`, `amount`
**Condition fields**: `merchant_normalized`, `description`, `category`, `bank_name`, `amount`, `transaction_type`
**Condition operators**: `contains`, `equals`, `starts_with`, `gt`, `lt`, `not_equals`
**Actions**: `set_category`, `set_merchant`, `add_tag_ids`, `apply_split`
---
### `split_payments`
Records of actual cash settlements between participants.
| Column | Type | Description |
|--------|------|-------------|
| `from_participant_id` | int FK → `participants` | Who paid |
| `to_participant_id` | int FK → `participants` | Who received |
| `amount` | numeric | Amount settled |
| `payment_date` | date | Date of settlement |
| `notes` | text | Optional note (e.g. "bank transfer") |
| `linked_transaction_id` | int FK → `transactions` (nullable) | If the payment was itself a transaction |
---
### `expense_metadata`
Enrichment records for non-statement expenses (email receipts, manual entries). Linked to a `transaction` if one exists; otherwise a standalone record awaiting reconciliation.
| Column | Type | Description |
|--------|------|-------------|
| `transaction_id` | int FK → `transactions` (unique, nullable) | Linked transaction; NULL until reconciled |
| `source` | text | Origin: `email`, `manual` |
| `paperless_doc_id` | int | Paperless-NGX document ID |
| `payment_method` | text | `credit_card`, `debit_card`, `paypal`, `afterpay`, `cash`, etc. |
| `payment_method_detail` | text | Card last-4 or provider detail |
| `order_reference` | text | Order/confirmation number |
| `line_items` | jsonb | Array of `{description, qty, unit_price, total}` |
| `merchant_normalized` | text | Canonical merchant for matching |
| `amount` / `transaction_date` | numeric / date | Used for reconciliation matching when `transaction_id IS NULL` |
| `extraction_model` | text | AI model used (`gemini-2.5-flash`) |
Partial index on `(merchant_normalized, transaction_date) WHERE transaction_id IS NULL` powers reconciliation queries.
---
### `rule_apply_runs`
Audit log of bulk rule-apply operations. Each run captures which transactions were affected and a full snapshot for revert support.
| Column | Type | Description |
|--------|------|-------------|
| `owner_id` | int FK → `participants` | |
| `applied_at` | timestamptz | When the run executed |
| `split_from` | date | Optional date filter used for this run |
| `matched` | int | Number of rules matched |
| `transactions_affected` | int | Number of transactions changed |
| `reverted_at` | timestamptz | Set when run was reverted |
| `snapshot` | jsonb | Pre-run state of all affected transactions |
---
@@ -203,16 +256,19 @@ All routes require authentication via `X-Forwarded-User` header (set by Traefik)
|--------|-------|-------------|
| GET | `/api/statements` | All statements for current user |
| GET / PATCH | `/api/statements/[id]` | Get statement; PATCH to reassign owner (also writes `account_owner_mappings`) |
| GET | `/api/transactions` | Paginated transactions with filters: `from`, `to`, `category`, `merchant`, `statement_id`, `search`, `sort`, `dir` |
| GET | `/api/transactions` | Paginated transactions. Filters: `from`, `to`, `categories`, `bank_names`, `tag_ids`, `transaction_types`, `search`, `statement_id`, `amount_min`, `amount_max`, `has_split` (`yes`/`no`). Sort: `sort_by` (`transaction_date`\|`amount`\|`created_at`), `sort_dir` (`asc`\|`desc`) |
| POST | `/api/transactions` | Create a manual transaction (no statement) |
| GET / PATCH | `/api/transactions/[id]` | Get transaction; PATCH to upsert override (category, merchant, notes) |
| GET / POST | `/api/transactions/[id]/splits` | List or create splits on a transaction |
| GET / POST | `/api/transactions/[id]/tags` | List or apply tags to a transaction |
| POST | `/api/transactions/bulk` | Bulk update category/merchant across multiple transactions |
| POST | `/api/transactions/reconcile` | Link manual transactions to statement transactions; copies overrides, tags, splits across |
| GET | `/api/analytics/monthly` | Split-adjusted monthly spend by category + income + investments. Params: `months` (124, default 6) |
| GET | `/api/analytics/subscriptions` | Recurring charge detection — merchants with ≥3 occurrences at consistent intervals |
| GET | `/api/analytics/fees` | Fees and interest from statement summaries + individual fee/interest transactions |
| GET | `/api/shared-transactions` | Transactions that have active splits |
| GET | `/api/shared-transactions` | Transactions with active splits; sorted client-side by date/imported/amount in the UI |
| POST | `/api/splits/settle` | Mark a split as settled |
| GET / POST | `/api/split-payments` | List or record cash settlements between participants |
| GET / POST | `/api/participants` | List participants; POST to create (with optional `email`) |
| GET | `/api/participants/[id]/balance` | Net balance owed by/to a specific participant |
| GET | `/api/participants/balances` | All participant balances |
@@ -289,6 +345,27 @@ docker exec postgres-personal psql -U personal -d personal \
| `0007_cashflow` | `amount_aud`, `exchange_rate_to_aud` on transactions; `exchange_rate_to_aud` on statements |
> `paperless_doc_id` on statements and the `uq_statements_paperless_doc_id` index were added directly (not tracked in a migration file).
> `owner_id` on transactions and `statement_id` made nullable were applied directly (March 2026) to support manual transaction entry without a fake statement.
> `reconciled_with_id` on transactions, `expense_metadata`, `rule_apply_runs`, `split_payments` were added directly and are covered by the Prisma schema but lack individual migration files.
---
## Known Gaps / TODOs
### Payment Provider tracking
Currently `merchant_normalized` conflates the *payment provider* with the *merchant*. Transactions processed through PayPal, Afterpay, Zip, Alipay, etc. end up with the provider as the merchant when the real merchant can't be recovered.
**What's been done so far:**
- PayPal entries that embed the merchant name (e.g. `PAYPAL *BUNNINGSGRO`) were cleaned up — the real merchant was extracted during the March 2026 consolidation pass.
- Pure PayPal/Afterpay/Zip entries where the merchant is unrecoverable were left as-is.
- A one-time SQL consolidation pass normalised ~50 merchant name variant groups (March 2026).
**Remaining work:**
1. **DB migration**: `ALTER TABLE transactions ADD COLUMN payment_provider text` and same on `transaction_overrides`.
2. **Gemini prompt**: add `payment_provider` to the `responseSchema` so the AI extracts it separately (`"PayPal"`, `"Afterpay"`, `"Zip"`, `null`, etc.) — the raw bank description usually contains enough signal.
3. **Backfill**: for existing transactions, derive `payment_provider` from `merchant_name` patterns (`PAYPAL *`, `AFTERPAY`, `ZIP/ZIPPAY`, `BPAY`).
4. **App**: surface `payment_provider` as a filter/column in the transactions view; exclude payment providers from merchant analytics so they don't inflate the merchant list.
---
+1249 -5
View File
File diff suppressed because it is too large Load Diff
+10 -2
View File
@@ -6,7 +6,12 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint"
"lint": "eslint",
"test": "vitest run --config vitest.config.ts",
"test:watch": "vitest --config vitest.config.ts",
"test:setup": "bash scripts/setup-test-db.sh",
"test:integration": "vitest run --config vitest.integration.config.ts",
"test:all": "npm test && npm run test:integration"
},
"dependencies": {
"@prisma/adapter-pg": "^7.4.2",
@@ -25,9 +30,12 @@
"@types/pg": "^8.18.0",
"@types/react": "^19",
"@types/react-dom": "^19",
"@vitest/coverage-v8": "^4.1.2",
"eslint": "^9",
"eslint-config-next": "16.1.6",
"tailwindcss": "^4",
"typescript": "^5"
"typescript": "^5",
"vite-tsconfig-paths": "^6.1.1",
"vitest": "^4.1.2"
}
}
@@ -0,0 +1 @@
ALTER TABLE "transaction_overrides" ADD COLUMN "my_share_percent" DECIMAL(5,2);
@@ -0,0 +1,13 @@
CREATE TABLE split_payments (
id SERIAL PRIMARY KEY,
from_participant_id INTEGER NOT NULL REFERENCES participants(id),
to_participant_id INTEGER NOT NULL REFERENCES participants(id),
amount DECIMAL(10,2) NOT NULL CHECK (amount > 0),
payment_date DATE NOT NULL,
notes TEXT,
linked_transaction_id INTEGER REFERENCES transactions(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_split_payments_from ON split_payments(from_participant_id);
CREATE INDEX idx_split_payments_to ON split_payments(to_participant_id);
@@ -0,0 +1,2 @@
ALTER TABLE transactions ADD COLUMN reconciled_with_id INTEGER REFERENCES transactions(id) ON DELETE SET NULL;
CREATE INDEX idx_transactions_reconciled ON transactions(reconciled_with_id) WHERE reconciled_with_id IS NOT NULL;
+104
View File
@@ -13,6 +13,7 @@ model transaction_overrides {
merchant_normalized String?
category_override String?
notes String?
my_share_percent Decimal? @db.Decimal(5, 2)
updated_at DateTime @default(now()) @updatedAt
}
@@ -23,6 +24,8 @@ model participants {
created_at DateTime @default(now())
splits transaction_splits[]
account_owner_mappings account_owner_mappings[]
payments_sent split_payments[] @relation("payments_from")
payments_received split_payments[] @relation("payments_to")
}
model account_owner_mappings {
@@ -49,6 +52,19 @@ model transaction_splits {
@@unique([transaction_id, participant_id])
}
model split_payments {
id Int @id @default(autoincrement())
from_participant_id Int
to_participant_id Int
amount Decimal @db.Decimal(10, 2)
payment_date DateTime @db.Date
notes String?
linked_transaction_id Int?
created_at DateTime @default(now())
from_participant participants @relation("payments_from", fields: [from_participant_id], references: [id])
to_participant participants @relation("payments_to", fields: [to_participant_id], references: [id])
}
model tags {
id Int @id @default(autoincrement())
name String @unique
@@ -89,3 +105,91 @@ model budgets {
@@unique([owner_id, category, month])
}
model statements {
id Int @id @default(autoincrement())
filename String
bank_name String?
card_name String?
account_type String?
account_number String
billing_start_date DateTime? @db.Date
billing_end_date DateTime? @db.Date
total_amount_due Decimal? @db.Decimal(12, 2)
minimum_amount_due Decimal? @db.Decimal(12, 2)
payment_due_date DateTime? @db.Date
event_created Boolean? @default(false)
tier_used String?
created_at DateTime? @default(now())
statement_type String @default("credit_card")
currency String? @default("AUD")
opening_balance Decimal? @db.Decimal(12, 2)
closing_balance Decimal? @db.Decimal(12, 2)
total_credits Decimal? @db.Decimal(12, 2)
total_debits Decimal? @db.Decimal(12, 2)
interest_charged Decimal? @db.Decimal(12, 2)
fees_charged Decimal? @db.Decimal(12, 2)
credit_limit Decimal? @db.Decimal(12, 2)
available_credit Decimal? @db.Decimal(12, 2)
owner_id Int @default(1)
account_holder_name String?
exchange_rate_to_aud Decimal? @db.Decimal(10, 6)
paperless_doc_id Int? @unique
transactions transactions[]
}
model transactions {
id Int @id @default(autoincrement())
statement_id Int?
transaction_date DateTime @db.Date
description String?
amount Decimal @db.Decimal(12, 2)
created_at DateTime? @default(now())
transaction_type String? @default("debit")
merchant_name String?
location String?
foreign_currency_amount Decimal? @db.Decimal(12, 2)
foreign_currency_code String?
category String?
row_index Int?
merchant_normalized String?
amount_aud Decimal? @db.Decimal(12, 2)
owner_id Int?
reconciled_with_id Int?
statement statements? @relation(fields: [statement_id], references: [id], onDelete: Cascade)
reconciled_with transactions? @relation("reconciled", fields: [reconciled_with_id], references: [id], onDelete: SetNull)
reconciled_by transactions[] @relation("reconciled")
expense_metadata expense_metadata?
}
model expense_metadata {
id Int @id @default(autoincrement())
transaction_id Int? @unique
source String @default("email")
paperless_doc_id Int? @unique
source_email_subject String?
source_email_from String?
payment_method String?
payment_method_detail String?
order_reference String?
line_items Json @default("[]")
tax_amount Decimal? @db.Decimal(12, 2)
subtotal Decimal? @db.Decimal(12, 2)
merchant_normalized String?
amount Decimal? @db.Decimal(12, 2)
transaction_date DateTime? @db.Date
extraction_model String? @default("gemini-2.5-flash")
created_at DateTime? @default(now())
transaction transactions? @relation(fields: [transaction_id], references: [id], onDelete: Cascade)
}
model rule_apply_runs {
id Int @id @default(autoincrement())
owner_id Int
applied_at DateTime @default(now())
split_from DateTime? @db.Date
matched Int @default(0)
transactions_affected Int @default(0)
reverted_at DateTime?
snapshot Json @default("[]")
}
+39
View File
@@ -0,0 +1,39 @@
#!/bin/bash
# Creates the personal_test database and writes .env.test
# Run once before integration tests: npm run test:setup
set -e
PG_CONTAINER=postgres-personal
PG_USER=personal
PG_PASS=personalpassword123
TEST_DB=personal_test
# Discover the container's bridge IP (accessible from the host on Linux)
PG_HOST=$(docker inspect "$PG_CONTAINER" \
--format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 2>/dev/null | head -1)
if [ -z "$PG_HOST" ]; then
echo "ERROR: Could not find container '$PG_CONTAINER'"
exit 1
fi
echo "Postgres container at $PG_HOST:5432"
# Create test database (ignore error if it already exists)
docker exec "$PG_CONTAINER" psql -U "$PG_USER" -d postgres \
-c "CREATE DATABASE $TEST_DB;" 2>/dev/null || true
# Wipe and rebuild schema from production (schema only, no data)
echo "Copying schema from $PG_USER to $TEST_DB..."
docker exec "$PG_CONTAINER" pg_dump -U "$PG_USER" --schema-only "$PG_USER" \
| docker exec -i "$PG_CONTAINER" psql -U "$PG_USER" -d "$TEST_DB" -q
echo "Schema ready."
# Write .env.test
cat > "$(dirname "$0")/../.env.test" << EOF
DATABASE_URL=postgresql://$PG_USER:$PG_PASS@$PG_HOST:5432/$TEST_DB
EOF
echo ".env.test written — integration tests can now run with: npm run test:integration"
+83
View File
@@ -0,0 +1,83 @@
import { Pool } from "pg";
import { vi } from "vitest";
export function createPool() {
return new Pool({ connectionString: process.env.DATABASE_URL });
}
// Replace the app's Prisma-based queryRaw with a direct pg call so that
// tests don't depend on Prisma's singleton picking up the right DATABASE_URL.
// Must be called BEFORE dynamically importing any module that uses @/lib/db.
// Uses vi.doMock (not vi.mock) so it is NOT hoisted and CAN close over `p`.
export function mockDbWithPool(p: Pool) {
vi.resetModules(); // clear module cache so fresh imports pick up the mock
vi.doMock("@/lib/db", () => ({
queryRaw: async (sql: string, params: unknown[] = []) => {
const result = await p.query(sql, params);
return result.rows;
},
prisma: p,
}));
}
/** Wipe all data tables and restart sequences between tests. */
export async function resetDB(pool: Pool) {
await pool.query(`
TRUNCATE
split_payments,
transaction_splits,
transaction_tags,
transaction_overrides,
rule_apply_runs,
rules,
budgets,
account_owner_mappings,
transactions,
statements,
tags,
participants
RESTART IDENTITY CASCADE
`);
}
/** Seed two participants and return their IDs. */
export async function seedParticipants(pool: Pool, names: [string, string] = ["Alice", "Bob"]) {
const r1 = await pool.query(
`INSERT INTO participants (name) VALUES ($1) RETURNING id`,
[names[0]]
);
const r2 = await pool.query(
`INSERT INTO participants (name) VALUES ($1) RETURNING id`,
[names[1]]
);
return { ownerId: r1.rows[0].id as number, otherId: r2.rows[0].id as number };
}
/** Insert a manual transaction (no statement) and return its id. */
export async function insertTransaction(
pool: Pool,
ownerId: number,
overrides: {
description?: string;
amount?: number;
category?: string;
transaction_type?: string;
transaction_date?: string;
merchant_normalized?: string;
} = {}
): Promise<number> {
const r = await pool.query(
`INSERT INTO transactions
(owner_id, statement_id, transaction_date, description, amount, transaction_type, category, row_index)
VALUES ($1, NULL, $2, $3, $4, $5, $6, 0) RETURNING id`,
[
ownerId,
overrides.transaction_date ?? "2024-06-15",
overrides.description ?? "Test transaction",
overrides.amount ?? 100,
overrides.transaction_type ?? "debit",
overrides.category ?? "other",
]
);
return r.rows[0].id as number;
}
@@ -0,0 +1,76 @@
import { describe, it, expect, beforeEach, afterAll } from "vitest";
import { createPool, resetDB } from "./helpers";
const pool = createPool();
beforeEach(async () => {
await resetDB(pool);
});
afterAll(async () => {
await pool.end();
});
// This tests the name-substitution logic applied in /api/participants GET.
// The rule: the participant matching the current user's ID gets name "Me";
// everyone else keeps their real name.
function substituteMe(
participants: { id: number; name: string }[],
currentUserId: number
) {
return participants.map((p) =>
p.id === currentUserId ? { ...p, name: "Me" } : p
);
}
describe("participant Me substitution", () => {
it("replaces the current user's name with Me", async () => {
const r = await pool.query(
`INSERT INTO participants (name) VALUES ('Siddharth') RETURNING id`
);
const userId = r.rows[0].id;
const participants = [
{ id: userId, name: "Siddharth" },
{ id: userId + 1, name: "Sonu" },
];
const result = substituteMe(participants, userId);
expect(result.find((p) => p.id === userId)?.name).toBe("Me");
expect(result.find((p) => p.id === userId + 1)?.name).toBe("Sonu");
});
it("leaves all names unchanged when currentUserId does not match", () => {
const participants = [
{ id: 1, name: "Siddharth" },
{ id: 2, name: "Sonu" },
];
const result = substituteMe(participants, 999);
expect(result).toEqual(participants);
});
it("Sonu sees Me for herself and Siddharth for the primary user", async () => {
const r1 = await pool.query(
`INSERT INTO participants (name) VALUES ('Siddharth') RETURNING id`
);
const r2 = await pool.query(
`INSERT INTO participants (name, email) VALUES ('Sonu', 'sonu@example.com') RETURNING id`
);
const siddharthId = r1.rows[0].id;
const sonuId = r2.rows[0].id;
const rawParticipants = [
{ id: siddharthId, name: "Siddharth" },
{ id: sonuId, name: "Sonu" },
];
// Siddharth's view
const siddharthView = substituteMe(rawParticipants, siddharthId);
expect(siddharthView.find((p) => p.id === siddharthId)?.name).toBe("Me");
expect(siddharthView.find((p) => p.id === sonuId)?.name).toBe("Sonu");
// Sonu's view
const sonuView = substituteMe(rawParticipants, sonuId);
expect(sonuView.find((p) => p.id === siddharthId)?.name).toBe("Siddharth");
expect(sonuView.find((p) => p.id === sonuId)?.name).toBe("Me");
});
});
+255
View File
@@ -0,0 +1,255 @@
import { describe, it, expect, beforeEach, afterAll, vi } from "vitest";
import { createPool, mockDbWithPool, resetDB, seedParticipants, insertTransaction } from "./helpers";
// Create a pool and mock @/lib/db BEFORE any dynamic imports that use it.
// vi.doMock is NOT hoisted so it can close over the pool instance.
const pool = createPool();
mockDbWithPool(pool);
// Dynamic import AFTER the mock ensures getTransactions / getParticipantBalances
// use the test pool rather than Prisma's singleton.
const { getTransactions, getParticipantBalances } = await import("@/lib/queries");
beforeEach(async () => {
await resetDB(pool);
});
afterAll(async () => {
await pool.end();
vi.restoreAllMocks();
});
// ── getTransactions ───────────────────────────────────────────────────────────
describe("getTransactions — owner scoping", () => {
it("returns only the owner's transactions", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { description: "Alice groceries" });
await insertTransaction(pool, otherId, { description: "Bob petrol" });
const { data } = await getTransactions(ownerId, { limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(data[0].description).toBe("Alice groceries");
});
it("includes transactions where owner is a split participant", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
const txId = await insertTransaction(pool, otherId, { description: "Shared dinner" });
// Add Alice as a split participant
await pool.query(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent) VALUES ($1, $2, 50)`,
[txId, ownerId]
);
const { data } = await getTransactions(ownerId, { limit: 50, offset: 0 });
expect(data.some((t) => t.description === "Shared dinner")).toBe(true);
});
it("returns correct total count", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { description: "tx1" });
await insertTransaction(pool, ownerId, { description: "tx2" });
await insertTransaction(pool, ownerId, { description: "tx3" });
const { total } = await getTransactions(ownerId, { limit: 2, offset: 0 });
expect(total).toBe(3);
});
});
describe("getTransactions — date filters", () => {
it("filters by from date", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { description: "old tx", transaction_date: "2024-01-10" });
await insertTransaction(pool, ownerId, { description: "new tx", transaction_date: "2024-03-01" });
const { data } = await getTransactions(ownerId, { from: "2024-02-01", limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(data[0].description).toBe("new tx");
});
it("filters by to date", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { description: "old tx", transaction_date: "2024-01-10" });
await insertTransaction(pool, ownerId, { description: "new tx", transaction_date: "2024-03-01" });
const { data } = await getTransactions(ownerId, { to: "2024-01-31", limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(data[0].description).toBe("old tx");
});
});
describe("getTransactions — category filter", () => {
it("filters by category", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { description: "Grocery run", category: "groceries" });
await insertTransaction(pool, ownerId, { description: "Dinner out", category: "dining" });
const { data } = await getTransactions(ownerId, { categories: ["groceries"], limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(data[0].description).toBe("Grocery run");
});
it("category override takes precedence over raw category", async () => {
const { ownerId } = await seedParticipants(pool);
const txId = await insertTransaction(pool, ownerId, { category: "dining" });
await pool.query(
`INSERT INTO transaction_overrides (transaction_id, category_override) VALUES ($1, 'groceries')`,
[txId]
);
const { data: dining } = await getTransactions(ownerId, { categories: ["dining"], limit: 50, offset: 0 });
const { data: groceries } = await getTransactions(ownerId, { categories: ["groceries"], limit: 50, offset: 0 });
expect(dining).toHaveLength(0); // override hides original
expect(groceries).toHaveLength(1); // override exposes new category
});
});
describe("getTransactions — search filter", () => {
it("searches description case-insensitively", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { description: "COLES WYNDHAM" });
await insertTransaction(pool, ownerId, { description: "ALDI POINT COOK" });
const { data } = await getTransactions(ownerId, { search: "coles", limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(data[0].description).toBe("COLES WYNDHAM");
});
});
describe("getTransactions — amount filters", () => {
it("filters by amount_min", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { amount: 20 });
await insertTransaction(pool, ownerId, { amount: 200 });
const { data } = await getTransactions(ownerId, { amount_min: 100, limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(Number(data[0].amount)).toBe(200);
});
it("filters by amount_max", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId, { amount: 20 });
await insertTransaction(pool, ownerId, { amount: 200 });
const { data } = await getTransactions(ownerId, { amount_max: 50, limit: 50, offset: 0 });
expect(data).toHaveLength(1);
expect(Number(data[0].amount)).toBe(20);
});
});
describe("getTransactions — pagination", () => {
it("respects limit and offset", async () => {
const { ownerId } = await seedParticipants(pool);
for (let i = 0; i < 5; i++) {
await insertTransaction(pool, ownerId, { description: `tx-${i}`, transaction_date: `2024-0${i + 1}-01` });
}
const page1 = await getTransactions(ownerId, { limit: 2, offset: 0 });
const page2 = await getTransactions(ownerId, { limit: 2, offset: 2 });
expect(page1.data).toHaveLength(2);
expect(page2.data).toHaveLength(2);
expect(page1.data[0].description).not.toBe(page2.data[0].description);
expect(page1.total).toBe(5);
});
});
describe("getTransactions — splits and tags attached", () => {
it("attaches empty arrays when no splits or tags", async () => {
const { ownerId } = await seedParticipants(pool);
await insertTransaction(pool, ownerId);
const { data } = await getTransactions(ownerId, { limit: 50, offset: 0 });
expect(data[0].splits).toEqual([]);
expect(data[0].tags).toEqual([]);
});
it("attaches split participants", async () => {
const { ownerId, otherId } = await seedParticipants(pool, ["Alice", "Bob"]);
const txId = await insertTransaction(pool, ownerId);
await pool.query(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent) VALUES ($1, $2, 50)`,
[txId, otherId]
);
const { data } = await getTransactions(ownerId, { limit: 50, offset: 0 });
expect(data[0].splits).toHaveLength(1);
expect(data[0].splits[0].name).toBe("Bob");
expect(Number(data[0].splits[0].share_percent)).toBe(50);
});
});
// ── getParticipantBalances ────────────────────────────────────────────────────
describe("getParticipantBalances", () => {
it("shows zero balance when no splits", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
void otherId;
const balances = await getParticipantBalances(ownerId);
expect(balances.every((b) => Number(b.total_owed) === 0)).toBe(true);
});
it("calculates positive balance when participant owes owner", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
// Alice pays $100, Bob owes 50%
const txId = await insertTransaction(pool, ownerId, { amount: 100 });
await pool.query(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent) VALUES ($1, $2, 50)`,
[txId, otherId]
);
const balances = await getParticipantBalances(ownerId);
const bobBalance = balances.find((b) => b.id === otherId);
expect(bobBalance).toBeDefined();
expect(Number(bobBalance!.total_owed)).toBeCloseTo(50);
});
it("reduces balance after recording a payment", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
const txId = await insertTransaction(pool, ownerId, { amount: 100 });
await pool.query(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent) VALUES ($1, $2, 50)`,
[txId, otherId]
);
// Bob pays Alice $30
await pool.query(
`INSERT INTO split_payments (from_participant_id, to_participant_id, amount, payment_date)
VALUES ($1, $2, 30, '2024-06-20')`,
[otherId, ownerId]
);
const balances = await getParticipantBalances(ownerId);
const bobBalance = balances.find((b) => b.id === otherId);
expect(Number(bobBalance!.total_owed)).toBeCloseTo(20);
});
it("shows negative balance when owner owes participant", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
// Bob pays $100 for a shared expense, Alice owes 50%
const txId = await insertTransaction(pool, otherId, { amount: 100 });
await pool.query(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent) VALUES ($1, $2, 50)`,
[txId, ownerId]
);
const balances = await getParticipantBalances(ownerId);
const bobBalance = balances.find((b) => b.id === otherId);
expect(Number(bobBalance!.total_owed)).toBeCloseTo(-50);
});
it("unsettled_count reflects open splits", async () => {
const { ownerId, otherId } = await seedParticipants(pool);
const tx1 = await insertTransaction(pool, ownerId, { amount: 100 });
const tx2 = await insertTransaction(pool, ownerId, { amount: 80 });
await pool.query(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent)
VALUES ($1, $2, 50), ($3, $4, 50)`,
[tx1, otherId, tx2, otherId]
);
const balances = await getParticipantBalances(ownerId);
const bobBalance = balances.find((b) => b.id === otherId);
expect(bobBalance!.unsettled_count).toBe(2);
});
});
+49
View File
@@ -0,0 +1,49 @@
import { describe, it, expect } from "vitest";
import { CATEGORIES, formatCategory } from "@/lib/categories";
describe("formatCategory", () => {
it("capitalises single word", () => {
expect(formatCategory("groceries")).toBe("Groceries");
});
it("capitalises and spaces underscore-separated words", () => {
expect(formatCategory("home_goods")).toBe("Home Goods");
});
it("handles three-word categories", () => {
expect(formatCategory("home_maintenance")).toBe("Home Maintenance");
});
it("handles cash_advance", () => {
expect(formatCategory("cash_advance")).toBe("Cash Advance");
});
it("handles personal_care", () => {
expect(formatCategory("personal_care")).toBe("Personal Care");
});
it("handles single-word categories without underscores", () => {
expect(formatCategory("travel")).toBe("Travel");
expect(formatCategory("fees")).toBe("Fees");
expect(formatCategory("other")).toBe("Other");
});
});
describe("CATEGORIES", () => {
it("contains expected core categories", () => {
const cats = CATEGORIES as readonly string[];
expect(cats).toContain("groceries");
expect(cats).toContain("dining");
expect(cats).toContain("transport");
expect(cats).toContain("health");
expect(cats).toContain("other");
});
it("has no duplicates", () => {
const cats = CATEGORIES as readonly string[];
expect(new Set(cats).size).toBe(cats.length);
});
it("all entries are lowercase with only letters and underscores", () => {
for (const cat of CATEGORIES) {
expect(cat).toMatch(/^[a-z_]+$/);
}
});
it("formatCategory produces unique display names", () => {
const formatted = CATEGORIES.map(formatCategory);
expect(new Set(formatted).size).toBe(formatted.length);
});
});
+166
View File
@@ -0,0 +1,166 @@
import { describe, it, expect } from "vitest";
import { evaluateCondition, type Condition, type TxFields } from "@/lib/rules";
function tx(overrides: Partial<TxFields> = {}): TxFields {
return {
effective_category: "groceries",
effective_merchant: "Coles",
description: "COLES WYNDHAM VALE",
bank_name: "ANZ",
amount: 42.5,
transaction_type: "debit",
tags: [],
...overrides,
};
}
function cond(field: Condition["field"], operator: Condition["operator"], value: string): Condition {
return { field, operator, value };
}
// ── String fields ─────────────────────────────────────────────────────────────
describe("merchant_normalized", () => {
it("contains — matches substring", () => {
expect(evaluateCondition(cond("merchant_normalized", "contains", "coles"), tx())).toBe(true);
});
it("contains — case-insensitive", () => {
expect(evaluateCondition(cond("merchant_normalized", "contains", "COLES"), tx())).toBe(true);
});
it("contains — no match", () => {
expect(evaluateCondition(cond("merchant_normalized", "contains", "woolworths"), tx())).toBe(false);
});
it("equals — exact match (case-insensitive)", () => {
expect(evaluateCondition(cond("merchant_normalized", "equals", "coles"), tx())).toBe(true);
});
it("equals — no match", () => {
expect(evaluateCondition(cond("merchant_normalized", "equals", "cole"), tx())).toBe(false);
});
it("starts_with — matches prefix", () => {
expect(evaluateCondition(cond("merchant_normalized", "starts_with", "col"), tx())).toBe(true);
});
it("starts_with — no match", () => {
expect(evaluateCondition(cond("merchant_normalized", "starts_with", "oles"), tx())).toBe(false);
});
it("not_equals — different value", () => {
expect(evaluateCondition(cond("merchant_normalized", "not_equals", "woolworths"), tx())).toBe(true);
});
it("not_equals — same value", () => {
expect(evaluateCondition(cond("merchant_normalized", "not_equals", "coles"), tx())).toBe(false);
});
it("empty merchant falls back to empty string", () => {
expect(evaluateCondition(cond("merchant_normalized", "contains", "coles"), tx({ effective_merchant: "" }))).toBe(false);
});
});
describe("description", () => {
it("contains — matches", () => {
expect(evaluateCondition(cond("description", "contains", "wyndham"), tx())).toBe(true);
});
it("equals — exact (case-insensitive)", () => {
expect(evaluateCondition(cond("description", "equals", "coles wyndham vale"), tx())).toBe(true);
});
it("starts_with", () => {
expect(evaluateCondition(cond("description", "starts_with", "coles"), tx())).toBe(true);
});
});
describe("category", () => {
it("equals category", () => {
expect(evaluateCondition(cond("category", "equals", "groceries"), tx())).toBe(true);
});
it("not_equals different category", () => {
expect(evaluateCondition(cond("category", "not_equals", "dining"), tx())).toBe(true);
});
it("contains partial", () => {
expect(evaluateCondition(cond("category", "contains", "grocer"), tx())).toBe(true);
});
});
describe("bank_name", () => {
it("equals bank", () => {
expect(evaluateCondition(cond("bank_name", "equals", "anz"), tx())).toBe(true);
});
it("not_equals different bank", () => {
expect(evaluateCondition(cond("bank_name", "not_equals", "nab"), tx())).toBe(true);
});
});
describe("transaction_type", () => {
it("equals debit", () => {
expect(evaluateCondition(cond("transaction_type", "equals", "debit"), tx())).toBe(true);
});
it("not_equals credit", () => {
expect(evaluateCondition(cond("transaction_type", "not_equals", "credit"), tx())).toBe(true);
});
it("equals credit — no match on debit tx", () => {
expect(evaluateCondition(cond("transaction_type", "equals", "credit"), tx())).toBe(false);
});
});
// ── Amount field ──────────────────────────────────────────────────────────────
describe("amount", () => {
it("equals exact amount", () => {
expect(evaluateCondition(cond("amount", "equals", "42.5"), tx())).toBe(true);
});
it("equals wrong amount", () => {
expect(evaluateCondition(cond("amount", "equals", "42"), tx())).toBe(false);
});
it("not_equals different amount", () => {
expect(evaluateCondition(cond("amount", "not_equals", "100"), tx())).toBe(true);
});
it("gt — amount is greater", () => {
expect(evaluateCondition(cond("amount", "gt", "40"), tx())).toBe(true);
});
it("gt — amount is equal (not strictly greater)", () => {
expect(evaluateCondition(cond("amount", "gt", "42.5"), tx())).toBe(false);
});
it("gt — amount is less", () => {
expect(evaluateCondition(cond("amount", "gt", "50"), tx())).toBe(false);
});
it("lt — amount is less", () => {
expect(evaluateCondition(cond("amount", "lt", "50"), tx())).toBe(true);
});
it("lt — amount is equal (not strictly less)", () => {
expect(evaluateCondition(cond("amount", "lt", "42.5"), tx())).toBe(false);
});
it("lt — amount is greater", () => {
expect(evaluateCondition(cond("amount", "lt", "40"), tx())).toBe(false);
});
it("unsupported operator (contains) returns false", () => {
expect(evaluateCondition(cond("amount", "contains", "42"), tx())).toBe(false);
});
});
// ── Tag field ────────────────────────────────────────────────────────────────
describe("tag", () => {
it("equals — tag present", () => {
expect(evaluateCondition(cond("tag", "equals", "5"), tx({ tags: [{ id: 5 }] }))).toBe(true);
});
it("equals — tag absent", () => {
expect(evaluateCondition(cond("tag", "equals", "5"), tx({ tags: [] }))).toBe(false);
});
it("equals — different tag", () => {
expect(evaluateCondition(cond("tag", "equals", "5"), tx({ tags: [{ id: 7 }] }))).toBe(false);
});
it("not_equals — tag absent", () => {
expect(evaluateCondition(cond("tag", "not_equals", "5"), tx({ tags: [] }))).toBe(true);
});
it("not_equals — tag present", () => {
expect(evaluateCondition(cond("tag", "not_equals", "5"), tx({ tags: [{ id: 5 }] }))).toBe(false);
});
it("matches one of multiple tags", () => {
expect(evaluateCondition(cond("tag", "equals", "3"), tx({ tags: [{ id: 1 }, { id: 3 }] }))).toBe(true);
});
});
// ── Unknown field ─────────────────────────────────────────────────────────────
describe("unknown field", () => {
it("returns false for unrecognised field", () => {
// @ts-expect-error intentional invalid field for regression guard
expect(evaluateCondition({ field: "nonexistent", operator: "equals", value: "x" }, tx())).toBe(false);
});
});
+2
View File
@@ -42,10 +42,12 @@ export async function GET(req: NextRequest) {
t.transaction_type,
CASE
WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100
WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100
ELSE COALESCE(t.amount_aud, t.amount)
END::numeric(12,2) AS my_amount,
s.bank_name
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
LEFT JOIN transaction_splits ts ON ts.transaction_id = t.id AND ts.participant_id = $1
JOIN statements s ON s.id = t.statement_id
WHERE s.owner_id = $1
@@ -32,9 +32,9 @@ export async function GET(
t.amount_aud,
CASE
WHEN t.transaction_type IN ('refund', 'credit') THEN
-(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
-(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
ELSE
(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
END::numeric(10,2) as my_amount,
t.transaction_type,
COALESCE(o.category_override, t.category) as category,
+6 -7
View File
@@ -3,12 +3,11 @@ import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
// Split-adjusted amount helper (positive for spend, negative for refunds)
const MY_AMOUNT = `CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END`;
const SPEND_EXPR = `
CASE
WHEN t.transaction_type IN ('refund', 'credit') THEN
-(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
ELSE
(CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END)
WHEN t.transaction_type IN ('refund', 'credit') THEN -(${MY_AMOUNT})
ELSE (${MY_AMOUNT})
END
`;
@@ -44,18 +43,18 @@ export async function GET(req: NextRequest) {
COUNT(*) FILTER (WHERE t.transaction_type IN ('refund', 'credit'))::int as refund_count,
COALESCE(SUM(
CASE WHEN t.transaction_type IN ('debit', 'fee', 'interest') THEN
CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END
${MY_AMOUNT}
ELSE 0 END
), 0)::numeric(12,2) as gross_spend,
COALESCE(SUM(
CASE WHEN t.transaction_type IN ('refund', 'credit') THEN
CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END
${MY_AMOUNT}
ELSE 0 END
), 0)::numeric(12,2) as total_refunds,
SUM(${SPEND_EXPR})::numeric(12,2) as net_spend,
AVG(
CASE WHEN t.transaction_type IN ('debit', 'fee', 'interest') THEN
CASE WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100 ELSE COALESCE(t.amount_aud, t.amount) END
${MY_AMOUNT}
END
)::numeric(10,2) as avg_debit,
MIN(t.transaction_date)::text as first_seen,
+1
View File
@@ -29,6 +29,7 @@ export async function GET(req: NextRequest) {
SUM(
CASE
WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100
WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100
ELSE COALESCE(t.amount_aud, t.amount)
END
)::numeric(12,2) as total_spent,
@@ -24,6 +24,7 @@ export async function GET(req: NextRequest) {
t.transaction_date,
CASE
WHEN ts.share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * ts.share_percent / 100
WHEN o.my_share_percent IS NOT NULL THEN COALESCE(t.amount_aud, t.amount) * o.my_share_percent / 100
ELSE COALESCE(t.amount_aud, t.amount)
END AS my_amount
FROM transactions t
+31
View File
@@ -0,0 +1,31 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { ensureTag, batchInsertCSVTransactions } from "@/lib/queries";
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json() as {
bank_name: string;
transactions: {
date: string;
description: string;
amount: number;
transaction_type: string;
merchant_name?: string;
foreign_currency_amount?: number;
foreign_currency_code?: string;
category?: string;
}[];
};
if (!Array.isArray(body.transactions) || body.transactions.length === 0) {
return NextResponse.json({ error: "No transactions provided" }, { status: 400 });
}
const tagId = await ensureTag("csv-import", "#8b5cf6");
const inserted = await batchInsertCSVTransactions(user.id, body.transactions, tagId);
return NextResponse.json({ inserted }, { status: 201 });
}
+1 -1
View File
@@ -7,7 +7,7 @@ export async function GET(req: NextRequest) {
if (type === "banks") {
const banks = await getBankNames();
return NextResponse.json(banks.map((b) => b.bank_name));
return NextResponse.json(banks);
}
if (!search) return NextResponse.json([]);
+3 -1
View File
@@ -6,6 +6,8 @@ export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const balances = await getParticipantBalances(user.id);
const tagParam = req.nextUrl.searchParams.get("tag_ids");
const tagIds = tagParam ? tagParam.split(",").map(Number).filter(Boolean) : undefined;
const balances = await getParticipantBalances(user.id, tagIds);
return NextResponse.json(balances);
}
+8 -2
View File
@@ -1,11 +1,17 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { queryRaw } from "@/lib/db";
import { getCurrentUser } from "@/lib/auth";
export async function GET() {
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
const participants = await prisma.participants.findMany({
orderBy: { name: "asc" },
});
if (user) {
return NextResponse.json(
participants.map((p) => (p.id === user.id ? { ...p, name: "Me" } : p))
);
}
return NextResponse.json(participants);
}
+11
View File
@@ -0,0 +1,11 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { getPendingReconciliations } from "@/lib/queries";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const data = await getPendingReconciliations(user.id);
return NextResponse.json(data);
}
@@ -0,0 +1,102 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
interface SnapshotEntry {
transaction_id: number;
had_override: boolean;
prev_category_override: string | null;
prev_merchant_normalized: string | null;
prev_tag_ids: number[];
prev_splits: { participant_id: number; share_percent: number; settled: boolean }[];
}
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const { id } = await params;
const runId = Number(id);
const rows = await queryRaw<{
id: number;
owner_id: number;
reverted_at: string | null;
snapshot: unknown;
}>(
`SELECT id, owner_id, reverted_at, snapshot FROM rule_apply_runs WHERE id = $1`,
[runId]
);
if (!rows.length) return NextResponse.json({ error: "Run not found" }, { status: 404 });
const run = rows[0];
if (run.owner_id !== user.id) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
if (run.reverted_at) return NextResponse.json({ error: "Already reverted" }, { status: 409 });
const snapshot = (typeof run.snapshot === "string"
? JSON.parse(run.snapshot)
: run.snapshot) as SnapshotEntry[];
for (const entry of snapshot) {
const txId = entry.transaction_id;
// Restore overrides
if (entry.had_override) {
await queryRaw(
`INSERT INTO transaction_overrides (transaction_id, category_override, merchant_normalized)
VALUES ($1, $2, $3)
ON CONFLICT (transaction_id) DO UPDATE SET
category_override = $2,
merchant_normalized = $3,
updated_at = NOW()`,
[txId, entry.prev_category_override, entry.prev_merchant_normalized]
);
} else {
// No override existed before — remove any that were created
await queryRaw(
`DELETE FROM transaction_overrides WHERE transaction_id = $1
AND category_override IS NULL AND merchant_normalized IS NULL`,
[txId]
);
// If override row exists but was only partially set by this run, clear those fields
await queryRaw(
`UPDATE transaction_overrides SET
category_override = NULL,
merchant_normalized = NULL,
updated_at = NOW()
WHERE transaction_id = $1`,
[txId]
);
}
// Restore tags: remove any that weren't there before, don't touch pre-existing ones
const prevTagIds = entry.prev_tag_ids;
if (prevTagIds.length > 0) {
await queryRaw(
`DELETE FROM transaction_tags WHERE transaction_id = $1 AND tag_id != ALL($2::int[])`,
[txId, prevTagIds]
);
} else {
// No tags existed before — remove all tags (they were all added by this run)
// Note: this only removes tags on transactions that matched this run
await queryRaw(`DELETE FROM transaction_tags WHERE transaction_id = $1`, [txId]);
}
// Restore splits
await queryRaw(`DELETE FROM transaction_splits WHERE transaction_id = $1`, [txId]);
for (const s of entry.prev_splits) {
await queryRaw(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent, settled)
VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`,
[txId, s.participant_id, s.share_percent, s.settled]
);
}
}
await queryRaw(
`UPDATE rule_apply_runs SET reverted_at = NOW() WHERE id = $1`,
[runId]
);
return NextResponse.json({ reverted: snapshot.length });
}
+107 -66
View File
@@ -2,93 +2,127 @@ import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
import { getTransactions } from "@/lib/queries";
import { evaluateCondition, type Condition, type Actions } from "@/lib/rules";
interface Condition {
field: "merchant_normalized" | "description" | "category" | "bank_name" | "amount";
operator: "contains" | "equals" | "starts_with" | "gt" | "lt" | "not_equals";
value: string;
interface SnapshotEntry {
transaction_id: number;
had_override: boolean;
prev_category_override: string | null;
prev_merchant_normalized: string | null;
prev_tag_ids: number[];
prev_splits: { participant_id: number; share_percent: number; settled: boolean }[];
}
interface SplitEntry {
participant_id: number;
share_percent: number;
}
interface Actions {
set_category?: string;
add_tag_ids?: number[];
set_merchant?: string;
apply_split?: SplitEntry[];
}
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
interface TxFields {
effective_category: string;
effective_merchant: string;
description: string;
bank_name: string;
amount: number;
}
const runs = await queryRaw<{
id: number;
applied_at: string;
split_from: string | null;
matched: number;
transactions_affected: number;
reverted_at: string | null;
}>(
`SELECT id, applied_at, split_from, matched, transactions_affected, reverted_at
FROM rule_apply_runs WHERE owner_id = $1 ORDER BY applied_at DESC LIMIT 20`,
[user.id]
);
function evaluateCondition(cond: Condition, tx: TxFields): boolean {
if (cond.field === "amount") {
const numVal = Number(tx.amount);
const numCond = Number(cond.value);
switch (cond.operator) {
case "equals": return numVal === numCond;
case "not_equals": return numVal !== numCond;
case "gt": return numVal > numCond;
case "lt": return numVal < numCond;
default: return false;
}
}
let fieldVal: string;
switch (cond.field) {
case "merchant_normalized": fieldVal = tx.effective_merchant || ""; break;
case "description": fieldVal = tx.description || ""; break;
case "category": fieldVal = tx.effective_category || ""; break;
case "bank_name": fieldVal = tx.bank_name || ""; break;
default: return false;
}
const strVal = fieldVal.toLowerCase();
const strCond = cond.value.toLowerCase();
switch (cond.operator) {
case "contains": return strVal.includes(strCond);
case "equals": return strVal === strCond;
case "starts_with": return strVal.startsWith(strCond);
case "not_equals": return strVal !== strCond;
default: return false;
}
return NextResponse.json(runs);
}
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json().catch(() => ({})) as { splitFrom?: string | null; ruleId?: number | null };
const splitFrom = body.splitFrom || null;
const ruleId = body.ruleId || null;
const rules = await queryRaw<{ id: number; conditions: unknown; actions: unknown }>(
`SELECT id, conditions, actions FROM rules WHERE owner_id = $1 AND enabled = true ORDER BY priority DESC`,
[user.id]
ruleId
? `SELECT id, conditions, actions FROM rules WHERE owner_id = $1 AND id = $2`
: `SELECT id, conditions, actions FROM rules WHERE owner_id = $1 AND enabled = true ORDER BY priority DESC`,
ruleId ? [user.id, ruleId] : [user.id]
);
if (!rules.length) return NextResponse.json({ matched: 0, transactions_affected: 0 });
const { data: transactions } = await getTransactions(user.id, { limit: 100000, offset: 0 });
// --- Pre-pass: find all transactions that will match any rule ---
const parsedRules = rules.map((r) => ({
conditions: (typeof r.conditions === "string" ? JSON.parse(r.conditions) : r.conditions) as Condition[],
actions: (typeof r.actions === "string" ? JSON.parse(r.actions) : r.actions) as Actions,
}));
const matchedIds = new Set<number>();
for (const tx of transactions) {
for (const { conditions } of parsedRules) {
if (conditions.length === 0 || conditions.every((c) => evaluateCondition(c, tx))) {
matchedIds.add(tx.id);
break;
}
}
}
// --- Capture before-state for all matched transactions (batched) ---
const snapshot: SnapshotEntry[] = [];
if (matchedIds.size > 0) {
const ids = Array.from(matchedIds);
const idList = ids.join(",");
const overrides = await queryRaw<{ transaction_id: number; category_override: string | null; merchant_normalized: string | null }>(
`SELECT transaction_id, category_override, merchant_normalized FROM transaction_overrides WHERE transaction_id = ANY($1::int[])`,
[ids]
);
const overrideMap = new Map(overrides.map((o) => [o.transaction_id, o]));
const tagRows = await queryRaw<{ transaction_id: number; tag_id: number }>(
`SELECT transaction_id, tag_id FROM transaction_tags WHERE transaction_id = ANY($1::int[])`,
[ids]
);
const tagMap = new Map<number, number[]>();
for (const row of tagRows) {
if (!tagMap.has(row.transaction_id)) tagMap.set(row.transaction_id, []);
tagMap.get(row.transaction_id)!.push(row.tag_id);
}
const splitRows = await queryRaw<{ transaction_id: number; participant_id: number; share_percent: number; settled: boolean }>(
`SELECT transaction_id, participant_id, share_percent, settled FROM transaction_splits WHERE transaction_id = ANY($1::int[])`,
[ids]
);
const splitMap = new Map<number, { participant_id: number; share_percent: number; settled: boolean }[]>();
for (const row of splitRows) {
if (!splitMap.has(row.transaction_id)) splitMap.set(row.transaction_id, []);
splitMap.get(row.transaction_id)!.push({ participant_id: row.participant_id, share_percent: row.share_percent, settled: row.settled });
}
for (const id of ids) {
const ov = overrideMap.get(id);
snapshot.push({
transaction_id: id,
had_override: !!ov,
prev_category_override: ov?.category_override ?? null,
prev_merchant_normalized: ov?.merchant_normalized ?? null,
prev_tag_ids: tagMap.get(id) ?? [],
prev_splits: splitMap.get(id) ?? [],
});
}
void idList; // suppress unused warning
}
// --- Apply rules ---
let matched = 0;
const affectedIds = new Set<number>();
for (const rule of rules) {
const conditions = (typeof rule.conditions === "string"
? JSON.parse(rule.conditions)
: rule.conditions) as Condition[];
const actions = (typeof rule.actions === "string"
? JSON.parse(rule.actions)
: rule.actions) as Actions;
for (const { conditions, actions } of parsedRules) {
for (const tx of transactions) {
const allMatch =
conditions.length === 0 || conditions.every((c) => evaluateCondition(c, tx));
const allMatch = conditions.length === 0 || conditions.every((c) => evaluateCondition(c, tx));
if (!allMatch) continue;
matched++;
@@ -116,7 +150,7 @@ export async function POST(req: NextRequest) {
}
if (actions.apply_split?.length) {
// Delete existing splits then insert new ones
if (splitFrom && tx.transaction_date < splitFrom) continue;
await queryRaw(`DELETE FROM transaction_splits WHERE transaction_id = $1`, [tx.id]);
for (const s of actions.apply_split) {
await queryRaw(
@@ -129,5 +163,12 @@ export async function POST(req: NextRequest) {
}
}
return NextResponse.json({ matched, transactions_affected: affectedIds.size });
// --- Save run record ---
const run = await queryRaw<{ id: number }>(
`INSERT INTO rule_apply_runs (owner_id, split_from, matched, transactions_affected, snapshot)
VALUES ($1, $2, $3, $4, $5) RETURNING id`,
[user.id, splitFrom, matched, affectedIds.size, JSON.stringify(snapshot)]
);
return NextResponse.json({ id: run[0].id, matched, transactions_affected: affectedIds.size });
}
+7 -1
View File
@@ -6,6 +6,12 @@ export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const transactions = await getSharedTransactions(user.id);
const tagParam = req.nextUrl.searchParams.get("tag_ids");
const rawIds = tagParam ? tagParam.split(",").filter(Boolean) : [];
const noTags = rawIds.includes("untagged");
const tagIds = rawIds.filter((id) => id !== "untagged").map(Number).filter((n) => !isNaN(n));
const participantParam = req.nextUrl.searchParams.get("participant_id");
const participantId = participantParam ? Number(participantParam) : undefined;
const transactions = await getSharedTransactions(user.id, tagIds.length ? tagIds : undefined, noTags, participantId);
return NextResponse.json(transactions);
}
+88
View File
@@ -0,0 +1,88 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
import { prisma } from "@/lib/db";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const sp = req.nextUrl.searchParams;
const participantId = sp.get("participant_id");
// Return payment history between current user and a participant
const rows = await queryRaw<{
id: number;
from_participant_id: number;
from_name: string;
to_participant_id: number;
to_name: string;
amount: number;
payment_date: string;
notes: string | null;
linked_transaction_id: number | null;
created_at: string;
}>(
`SELECT sp.id, sp.from_participant_id, pf.name as from_name,
sp.to_participant_id, pt.name as to_name,
sp.amount, sp.payment_date, sp.notes,
sp.linked_transaction_id, sp.created_at
FROM split_payments sp
JOIN participants pf ON pf.id = sp.from_participant_id
JOIN participants pt ON pt.id = sp.to_participant_id
WHERE (sp.from_participant_id = $1 OR sp.to_participant_id = $1)
AND (sp.from_participant_id = $2 OR sp.to_participant_id = $2)
ORDER BY sp.payment_date DESC, sp.created_at DESC`,
[user.id, participantId ? Number(participantId) : user.id]
);
return NextResponse.json(rows);
}
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json() as {
from_participant_id: number;
to_participant_id: number;
amount: number;
payment_date: string;
notes?: string;
linked_transaction_id?: number;
};
const { from_participant_id, to_participant_id, amount, payment_date, notes, linked_transaction_id } = body;
if (!from_participant_id || !to_participant_id || !amount || !payment_date) {
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
}
if (amount <= 0) {
return NextResponse.json({ error: "Amount must be positive" }, { status: 400 });
}
const payment = await prisma.split_payments.create({
data: {
from_participant_id,
to_participant_id,
amount,
payment_date: new Date(payment_date),
notes: notes || null,
linked_transaction_id: linked_transaction_id || null,
},
});
return NextResponse.json(payment);
}
export async function DELETE(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const sp = req.nextUrl.searchParams;
const id = Number(sp.get("id"));
if (!id) return NextResponse.json({ error: "id required" }, { status: 400 });
await prisma.split_payments.delete({ where: { id } });
return NextResponse.json({ ok: true });
}
+36 -3
View File
@@ -23,13 +23,44 @@ export async function PATCH(
const transactionId = Number(id);
const body = await req.json();
const { category, merchant_normalized, notes, transaction_type } = body as {
const { category, merchant_normalized, notes, transaction_type, my_share_percent, description, amount, transaction_date } = body as {
category?: string;
merchant_normalized?: string;
notes?: string;
transaction_type?: string;
my_share_percent?: number | null;
description?: string;
amount?: number;
transaction_date?: string;
};
if (my_share_percent !== undefined && my_share_percent !== null) {
if (typeof my_share_percent !== "number" || my_share_percent <= 0 || my_share_percent > 100) {
return NextResponse.json({ error: "my_share_percent must be between 1 and 100" }, { status: 400 });
}
}
// Direct field edits — only allowed for manual transactions (statement_id IS NULL)
const directFields = [description, amount, transaction_date].filter((v) => v !== undefined);
if (directFields.length > 0) {
const txRows = await queryRaw<{ statement_id: number | null }>(
`SELECT statement_id FROM transactions WHERE id = $1`,
[transactionId]
);
if (!txRows[0]?.statement_id) {
const setClauses: string[] = [];
const params: unknown[] = [];
let idx = 1;
if (description !== undefined) { setClauses.push(`description = $${idx++}`); params.push(description); }
if (amount !== undefined) { setClauses.push(`amount = $${idx++}`); params.push(amount); }
if (transaction_date !== undefined) { setClauses.push(`transaction_date = $${idx++}`); params.push(transaction_date); }
if (setClauses.length) {
params.push(transactionId);
await queryRaw(`UPDATE transactions SET ${setClauses.join(", ")} WHERE id = $${idx}`, params);
}
}
}
// transaction_type is a direct correction on the transactions table
if (transaction_type !== undefined) {
if (!VALID_TYPES.includes(transaction_type)) {
@@ -41,8 +72,8 @@ export async function PATCH(
);
}
// category/merchant/notes go through the overrides table
const hasOverride = category !== undefined || merchant_normalized !== undefined || notes !== undefined;
// category/merchant/notes/my_share_percent go through the overrides table
const hasOverride = category !== undefined || merchant_normalized !== undefined || notes !== undefined || my_share_percent !== undefined;
if (!hasOverride) {
return NextResponse.json({ ok: true });
}
@@ -51,6 +82,7 @@ export async function PATCH(
if (category !== undefined) data.category_override = category;
if (merchant_normalized !== undefined) data.merchant_normalized = merchant_normalized;
if (notes !== undefined) data.notes = notes;
if (my_share_percent !== undefined) data.my_share_percent = my_share_percent;
const override = await prisma.transaction_overrides.upsert({
where: { transaction_id: transactionId },
@@ -60,6 +92,7 @@ export async function PATCH(
category_override: category || null,
merchant_normalized: merchant_normalized || null,
notes: notes || null,
my_share_percent: my_share_percent != null ? String(my_share_percent) : null,
},
});
@@ -0,0 +1,91 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma, queryRaw } from "@/lib/db";
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json() as {
matches: { manual_id: number; statement_tx_id: number }[];
};
if (!Array.isArray(body.matches) || body.matches.length === 0) {
return NextResponse.json({ error: "No matches provided" }, { status: 400 });
}
// Verify all manual_ids belong to this user
const manualIds = body.matches.map((m) => m.manual_id);
const owned = await queryRaw<{ id: number }>(
`SELECT id FROM transactions WHERE id = ANY($1::int[]) AND statement_id IS NULL AND owner_id = $2`,
[manualIds, user.id]
);
if (owned.length !== manualIds.length) {
return NextResponse.json({ error: "One or more transactions not found" }, { status: 404 });
}
let reconciled = 0;
for (const { manual_id, statement_tx_id } of body.matches) {
await prisma.$transaction(async (tx) => {
// Copy overrides: manual → statement tx
const override = await tx.transaction_overrides.findUnique({
where: { transaction_id: manual_id },
});
if (override) {
await tx.transaction_overrides.upsert({
where: { transaction_id: statement_tx_id },
update: {
category_override: override.category_override,
merchant_normalized: override.merchant_normalized,
notes: override.notes,
my_share_percent: override.my_share_percent,
updated_at: new Date(),
},
create: {
transaction_id: statement_tx_id,
category_override: override.category_override,
merchant_normalized: override.merchant_normalized,
notes: override.notes,
my_share_percent: override.my_share_percent,
},
});
await tx.transaction_overrides.deleteMany({ where: { transaction_id: manual_id } });
}
// Move tags: manual → statement tx
const tags = await tx.transaction_tags.findMany({ where: { transaction_id: manual_id } });
if (tags.length) {
await tx.transaction_tags.createMany({
data: tags.map((t) => ({ transaction_id: statement_tx_id, tag_id: t.tag_id })),
skipDuplicates: true,
});
await tx.transaction_tags.deleteMany({ where: { transaction_id: manual_id } });
}
// Move splits: manual → statement tx
const splits = await tx.transaction_splits.findMany({ where: { transaction_id: manual_id } });
if (splits.length) {
await tx.transaction_splits.createMany({
data: splits.map((s) => ({
transaction_id: statement_tx_id,
participant_id: s.participant_id,
share_percent: s.share_percent,
})),
skipDuplicates: true,
});
await tx.transaction_splits.deleteMany({ where: { transaction_id: manual_id } });
}
// Mark manual tx as reconciled (link to statement tx)
await tx.$executeRawUnsafe(
`UPDATE transactions SET reconciled_with_id = $1 WHERE id = $2`,
statement_tx_id,
manual_id
);
});
reconciled++;
}
return NextResponse.json({ reconciled });
}
+60 -3
View File
@@ -1,25 +1,82 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { getTransactions } from "@/lib/queries";
import { queryRaw } from "@/lib/db";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const sp = req.nextUrl.searchParams;
const parseArr = (key: string) => { const v = sp.get(key); return v ? v.split(",").filter(Boolean) : undefined; };
const result = await getTransactions(user.id, {
from: sp.get("from") || undefined,
to: sp.get("to") || undefined,
category: sp.get("category") || undefined,
bank_name: sp.get("bank_name") || undefined,
categories: parseArr("categories"),
bank_names: parseArr("bank_names"),
tag_ids: parseArr("tag_ids"),
transaction_types: parseArr("transaction_types"),
search: sp.get("search") || undefined,
statement_id: sp.get("statement_id") || undefined,
tag_id: sp.get("tag_id") || undefined,
sort_by: sp.get("sort_by") || undefined,
sort_dir: sp.get("sort_dir") || undefined,
limit: sp.get("limit") ? Number(sp.get("limit")) : undefined,
offset: sp.get("offset") ? Number(sp.get("offset")) : undefined,
amount_min: sp.get("amount_min") ? Number(sp.get("amount_min")) : undefined,
amount_max: sp.get("amount_max") ? Number(sp.get("amount_max")) : undefined,
has_split: sp.get("has_split") || undefined,
});
return NextResponse.json(result);
}
export async function POST(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const body = await req.json() as {
date: string;
description: string;
amount: number;
transaction_type?: string;
merchant_normalized?: string;
category?: string;
splits?: { participant_id: number; share_percent: number }[];
};
if (!body.date || !body.description || body.amount == null) {
return NextResponse.json({ error: "date, description, amount are required" }, { status: 400 });
}
// Insert manual transaction with no statement (statement_id = NULL, owner_id set directly)
const txRows = await queryRaw<{ id: number }>(
`INSERT INTO transactions (statement_id, owner_id, transaction_date, description, amount, transaction_type, merchant_normalized, category, row_index)
VALUES (NULL, $1, $2, $3, $4, $5, $6, $7, (
SELECT COALESCE(MAX(row_index), -1) + 1 FROM transactions WHERE owner_id = $1 AND statement_id IS NULL
))
RETURNING id`,
[
user.id,
body.date,
body.description,
body.amount,
body.transaction_type || "debit",
body.merchant_normalized || null,
body.category || null,
]
);
const transactionId = txRows[0].id;
// Insert splits if provided
if (body.splits?.length) {
for (const s of body.splits) {
await queryRaw(
`INSERT INTO transaction_splits (transaction_id, participant_id, share_percent)
VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`,
[transactionId, s.participant_id, s.share_percent]
);
}
}
return NextResponse.json({ id: transactionId }, { status: 201 });
}
+1 -1
View File
@@ -136,7 +136,7 @@ function CategoryPanel({ category, selectedMonth }: { category: string; selected
const [year, month] = selectedMonth.split("-").map(Number);
const nextDate = new Date(year, month, 1);
const to = `${nextDate.getFullYear()}-${String(nextDate.getMonth() + 1).padStart(2, "0")}-01`;
const { data, isLoading } = useTransactions({ category, from, to, limit: 200 });
const { data, isLoading } = useTransactions({ categories: category ? [category] : [], from, to, limit: 200 });
const txns = data?.data || [];
return (
+272 -40
View File
@@ -1,17 +1,21 @@
"use client";
import { useMemo } from "react";
import { useMemo, useState } from "react";
import {
ComposedChart, Bar, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, Cell, Legend,
ComposedChart, Bar, Line, XAxis, YAxis, Tooltip, ResponsiveContainer,
} from "recharts";
import { useMonthlyAnalytics, useSubscriptions, useFees } from "@/lib/hooks";
import { formatCategory } from "@/lib/categories";
import { useMonthlyAnalytics, useSubscriptions, useFees, useTransactions, useUpdateTransaction } from "@/lib/hooks";
import { CATEGORIES, REGULAR_CATEGORIES, formatCategory } from "@/lib/categories";
const COMMITTED_CATEGORIES = new Set(["rent", "utilities", "insurance", "subscriptions"]);
const SPEND_TYPES = new Set(["debit", "fee", "interest"]);
function fmt(n: number) {
return new Intl.NumberFormat("en-AU", { style: "currency", currency: "AUD", maximumFractionDigits: 0 }).format(n);
}
function fmtTx(amount: number, type: string) {
const formatted = new Intl.NumberFormat("en-AU", { style: "currency", currency: "AUD", minimumFractionDigits: 2 }).format(amount);
return SPEND_TYPES.has(type) ? formatted : `+${formatted}`;
}
function fmtExact(n: number) {
return new Intl.NumberFormat("en-AU", { style: "currency", currency: "AUD", minimumFractionDigits: 2 }).format(n);
}
@@ -46,16 +50,234 @@ function Section({ title, children }: { title: string; children: React.ReactNode
}
// ─── Custom tooltip ──────────────────────────────────────────────────
function CommittedTooltip({ active, payload, label }: any) {
function RegularTooltip({ active, payload, label }: any) {
if (!active || !payload?.length) return null;
const committed = payload.find((p: any) => p.dataKey === "committed")?.value ?? 0;
const discretionary = payload.find((p: any) => p.dataKey === "discretionary")?.value ?? 0;
const regular = payload.find((p: any) => p.dataKey === "regular")?.value ?? 0;
const occasional = payload.find((p: any) => p.dataKey === "occasional")?.value ?? 0;
return (
<div className="bg-zinc-900 border border-zinc-700 rounded px-3 py-2 text-xs space-y-1">
<div className="font-medium text-zinc-300 mb-1">{label}</div>
<div className="flex justify-between gap-4"><span className="text-indigo-400">Committed</span><span>{fmt(committed)}</span></div>
<div className="flex justify-between gap-4"><span className="text-zinc-400">Discretionary</span><span>{fmt(discretionary)}</span></div>
<div className="flex justify-between gap-4 border-t border-zinc-700 pt-1"><span className="text-zinc-500">Total</span><span>{fmt(committed + discretionary)}</span></div>
<div className="flex justify-between gap-4"><span className="text-indigo-400">Regular</span><span>{fmt(regular)}</span></div>
<div className="flex justify-between gap-4"><span className="text-zinc-400">Occasional</span><span>{fmt(occasional)}</span></div>
<div className="flex justify-between gap-4 border-t border-zinc-700 pt-1"><span className="text-zinc-500">Total</span><span>{fmt(regular + occasional)}</span></div>
</div>
);
}
// ─── Drill-down row ──────────────────────────────────────────────────
function DrillDownRow({
category,
from,
to,
}: {
category: string;
from: string;
to: string;
}) {
const { data, isLoading } = useTransactions({ categories: category ? [category] : [], from, to, limit: 200 });
const updateTx = useUpdateTransaction();
if (isLoading) {
return (
<tr>
<td colSpan={2} className="px-4 py-3 text-xs text-zinc-500">Loading...</td>
</tr>
);
}
const txns = data?.data ?? [];
return (
<tr>
<td colSpan={2} className="px-0 py-0">
<div className="bg-zinc-950 border-t border-zinc-800/50">
{txns.length === 0 ? (
<p className="px-6 py-3 text-xs text-zinc-600">No transactions found.</p>
) : (
<table className="w-full text-xs">
<thead>
<tr className="border-b border-zinc-800">
<th className="text-left px-6 py-2 text-zinc-600 font-medium">Date</th>
<th className="text-left px-4 py-2 text-zinc-600 font-medium">Merchant</th>
<th className="text-right px-4 py-2 text-zinc-600 font-medium">My share</th>
<th className="text-left px-4 py-2 text-zinc-600 font-medium">Category</th>
<th className="text-right px-4 py-2 text-zinc-600 font-medium">% mine</th>
</tr>
</thead>
<tbody>
{txns.map((t) => {
const sharePct = t.my_share_percent ?? 100;
const effectiveAmt = t.amount * sharePct / 100;
const isDebit = SPEND_TYPES.has(t.transaction_type);
return (
<tr key={t.id} className="border-b border-zinc-800/30 hover:bg-zinc-900/30">
<td className="px-6 py-2 text-zinc-500 whitespace-nowrap">
{new Date(t.transaction_date).toLocaleDateString("en-AU", { day: "2-digit", month: "short" })}
</td>
<td className="px-4 py-2 text-zinc-300">{t.merchant_name || t.description}</td>
<td className={`px-4 py-2 text-right tabular-nums ${isDebit ? "text-zinc-200" : "text-green-400"}`}>
{fmtTx(effectiveAmt, t.transaction_type)}
{sharePct < 100 && (
<span className="text-zinc-600 ml-1 line-through">{fmtExact(t.amount)}</span>
)}
</td>
<td className="px-4 py-2">
<select
className="bg-zinc-800 border border-zinc-700 rounded px-2 py-0.5 text-xs text-zinc-300 focus:outline-none focus:border-indigo-500"
defaultValue={t.effective_category ?? "other"}
onChange={(e) => updateTx.mutate({ id: t.id, category: e.target.value })}
>
{CATEGORIES.map((c) => (
<option key={c} value={c}>{formatCategory(c)}</option>
))}
</select>
</td>
<td className="px-4 py-2 text-right">
<select
className="bg-zinc-800 border border-zinc-700 rounded px-2 py-0.5 text-xs text-zinc-300 focus:outline-none focus:border-indigo-500"
defaultValue={t.my_share_percent ?? 100}
onChange={(e) => {
const val = Number(e.target.value);
updateTx.mutate({ id: t.id, my_share_percent: val === 100 ? null : val });
}}
>
<option value={100}>100%</option>
<option value={75}>75%</option>
<option value={67}>67%</option>
<option value={50}>50%</option>
<option value={33}>33%</option>
<option value={25}>25%</option>
</select>
</td>
</tr>
);
})}
</tbody>
</table>
)}
</div>
</td>
</tr>
);
}
// ─── Monthly Spend Breakdown ─────────────────────────────────────────
function MonthlyBreakdown({ analytics }: { analytics: NonNullable<ReturnType<typeof useMonthlyAnalytics>["data"]> }) {
// analytics.months is newest-first; show last 6
const months = useMemo(() => analytics.months.slice(0, 6), [analytics.months]);
const [selectedMonth, setSelectedMonth] = useState<string>(months[0] ?? "");
const [expandedCategory, setExpandedCategory] = useState<string | null>(null);
// Reset expanded when month changes
const handleSelectMonth = (m: string) => {
setSelectedMonth(m);
setExpandedCategory(null);
};
const from = selectedMonth + "-01";
const lastDay = new Date(parseInt(selectedMonth.slice(0, 4)), parseInt(selectedMonth.slice(5, 7)), 0).getDate();
const to = selectedMonth + "-" + String(lastDay).padStart(2, "0");
const categoryData = useMemo(() => {
return analytics.rows
.map((row) => ({ category: row.category, amount: Number(row.spent[selectedMonth] ?? 0) }))
.filter((r) => r.amount > 0)
.sort((a, b) => b.amount - a.amount);
}, [analytics.rows, selectedMonth]);
const regularRows = categoryData.filter((r) => REGULAR_CATEGORIES.has(r.category as any));
const occasionalRows = categoryData.filter((r) => !REGULAR_CATEGORIES.has(r.category as any));
const regularTotal = regularRows.reduce((s, r) => s + r.amount, 0);
const occasionalTotal = occasionalRows.reduce((s, r) => s + r.amount, 0);
function monthLabel(m: string) {
const [year, month] = m.split("-");
return new Date(parseInt(year), parseInt(month) - 1).toLocaleDateString("en-AU", { month: "short", year: "2-digit" });
}
function renderRows(rows: typeof categoryData, dotClass: string) {
return rows.map((row) => (
<>
<tr
key={row.category}
className="border-b border-zinc-800/50 hover:bg-zinc-800/20 cursor-pointer transition-colors"
onClick={() => setExpandedCategory(expandedCategory === row.category ? null : row.category)}
>
<td className="px-4 py-2.5">
<span className="flex items-center gap-2 text-sm text-zinc-300">
<span className={`w-2 h-2 rounded-full inline-block flex-shrink-0 ${dotClass}`} />
{formatCategory(row.category)}
<span className="text-zinc-600 text-xs">{expandedCategory === row.category ? "▲" : "▼"}</span>
</span>
</td>
<td className="px-4 py-2.5 text-right tabular-nums text-sm text-zinc-200">{fmt(row.amount)}</td>
</tr>
{expandedCategory === row.category && (
<DrillDownRow
key={`${row.category}-drill`}
category={row.category}
from={from}
to={to}
/>
)}
</>
));
}
return (
<div>
{/* Month tabs */}
<div className="flex gap-1 mb-3 flex-wrap">
{months.map((m) => (
<button
key={m}
onClick={() => handleSelectMonth(m)}
className={`px-3 py-1.5 rounded-md text-xs font-medium transition-colors ${
m === selectedMonth
? "bg-indigo-600 text-white"
: "bg-zinc-800 text-zinc-400 hover:bg-zinc-700 hover:text-zinc-200"
}`}
>
{monthLabel(m)}
</button>
))}
</div>
<div className="border border-zinc-700 rounded-xl overflow-hidden">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-zinc-800 bg-zinc-900">
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium">Category</th>
<th className="text-right px-4 py-2.5 text-xs text-zinc-500 font-medium">Spend</th>
</tr>
</thead>
<tbody>
{regularRows.length > 0 && (
<>
<tr className="bg-zinc-900/30">
<td className="px-4 py-1.5 text-xs text-indigo-400 font-medium tracking-wide uppercase">Regular</td>
<td className="px-4 py-1.5 text-right text-xs text-indigo-400 font-medium">{fmt(regularTotal)}</td>
</tr>
{renderRows(regularRows, "bg-indigo-500")}
</>
)}
{occasionalRows.length > 0 && (
<>
<tr className="bg-zinc-900/30">
<td className="px-4 py-1.5 text-xs text-zinc-500 font-medium tracking-wide uppercase">Occasional</td>
<td className="px-4 py-1.5 text-right text-xs text-zinc-500 font-medium">{fmt(occasionalTotal)}</td>
</tr>
{renderRows(occasionalRows, "bg-zinc-500")}
</>
)}
{categoryData.length === 0 && (
<tr>
<td colSpan={2} className="px-4 py-6 text-center text-xs text-zinc-600">No spend data for this month.</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
);
}
@@ -63,35 +285,36 @@ function CommittedTooltip({ active, payload, label }: any) {
// ─── Main page ────────────────────────────────────────────────────────
export default function InsightsPage() {
const { data: analytics } = useMonthlyAnalytics(12);
const { data: analytics6 } = useMonthlyAnalytics(6);
const { data: subData } = useSubscriptions();
const { data: feesData } = useFees();
// Build committed/discretionary chart data
// Build regular/occasional chart data
const chartData = useMemo(() => {
if (!analytics) return [];
return [...analytics.months].reverse().map((month) => {
let committed = 0;
let discretionary = 0;
let regular = 0;
let occasional = 0;
for (const row of analytics.rows) {
const spend = Number(row.spent[month] ?? 0);
if (COMMITTED_CATEGORIES.has(row.category)) committed += spend;
else discretionary += spend;
if (REGULAR_CATEGORIES.has(row.category as any)) regular += spend;
else occasional += spend;
}
return {
month: month.slice(5) + "/" + month.slice(2, 4),
committed: Math.round(committed),
discretionary: Math.round(discretionary),
total: Math.round(committed + discretionary),
regular: Math.round(regular),
occasional: Math.round(occasional),
total: Math.round(regular + occasional),
};
});
}, [analytics]);
const committedValues = chartData.map((d) => d.committed);
const committedTrend = trend(committedValues);
const avgCommitted = committedValues.length
? Math.round(committedValues.reduce((a, b) => a + b, 0) / committedValues.length)
const regularValues = chartData.map((d) => d.regular);
const regularTrend = trend(regularValues);
const avgRegular = regularValues.length
? Math.round(regularValues.reduce((a, b) => a + b, 0) / regularValues.length)
: 0;
const latestCommitted = committedValues[committedValues.length - 1] ?? 0;
const latestRegular = regularValues[regularValues.length - 1] ?? 0;
const activeSubscriptions = subData?.subscriptions.filter((s) => s.is_active) ?? [];
const inactiveSubscriptions = subData?.subscriptions.filter((s) => !s.is_active) ?? [];
@@ -100,21 +323,21 @@ export default function InsightsPage() {
<div className="max-w-4xl">
<h2 className="text-xl font-semibold mb-6">Insights</h2>
{/* ── 1. Committed vs Discretionary ── */}
<Section title="Committed vs Discretionary Spend">
{/* ── 1. Regular vs Occasional ── */}
<Section title="Regular vs Occasional Spend">
<div className="grid grid-cols-3 gap-3 mb-4">
<div className="bg-zinc-900 border border-zinc-800 rounded-lg px-4 py-3">
<div className="text-xs text-zinc-500 mb-1">This month committed floor</div>
<div className="text-xl font-semibold text-indigo-400">{fmt(latestCommitted)}</div>
<div className="text-xs text-zinc-500 mb-1">This month regular spend</div>
<div className="text-xl font-semibold text-indigo-400">{fmt(latestRegular)}</div>
</div>
<div className="bg-zinc-900 border border-zinc-800 rounded-lg px-4 py-3">
<div className="text-xs text-zinc-500 mb-1">12-month avg committed</div>
<div className="text-xl font-semibold text-zinc-200">{fmt(avgCommitted)}</div>
<div className="text-xs text-zinc-500 mb-1">12-month avg regular</div>
<div className="text-xl font-semibold text-zinc-200">{fmt(avgRegular)}</div>
</div>
<div className="bg-zinc-900 border border-zinc-800 rounded-lg px-4 py-3">
<div className="text-xs text-zinc-500 mb-1">Trend (first 3 vs last 3 mo)</div>
<div className={`text-xl font-semibold ${committedTrend.dir === "up" ? "text-red-400" : committedTrend.dir === "down" ? "text-green-400" : "text-zinc-400"}`}>
{committedTrend.dir === "up" ? "↑" : committedTrend.dir === "down" ? "↓" : "→"} {committedTrend.pct}%
<div className={`text-xl font-semibold ${regularTrend.dir === "up" ? "text-red-400" : regularTrend.dir === "down" ? "text-green-400" : "text-zinc-400"}`}>
{regularTrend.dir === "up" ? "↑" : regularTrend.dir === "down" ? "↓" : "→"} {regularTrend.pct}%
</div>
</div>
</div>
@@ -123,20 +346,29 @@ export default function InsightsPage() {
<ComposedChart data={chartData} margin={{ top: 4, right: 8, left: 0, bottom: 0 }}>
<XAxis dataKey="month" tick={{ fill: "#71717a", fontSize: 11 }} axisLine={false} tickLine={false} />
<YAxis tick={{ fill: "#71717a", fontSize: 11 }} axisLine={false} tickLine={false} tickFormatter={(v) => `$${(v / 1000).toFixed(0)}k`} width={44} />
<Tooltip content={<CommittedTooltip />} />
<Bar dataKey="committed" stackId="a" fill="#6366f1" name="Committed" radius={[0, 0, 0, 0]} />
<Bar dataKey="discretionary" stackId="a" fill="#3f3f46" name="Discretionary" radius={[3, 3, 0, 0]} />
<Line type="monotone" dataKey="committed" stroke="#818cf8" strokeWidth={2} dot={false} strokeDasharray="4 2" />
<Tooltip content={<RegularTooltip />} />
<Bar dataKey="regular" stackId="a" fill="#6366f1" name="Regular" radius={[0, 0, 0, 0]} />
<Bar dataKey="occasional" stackId="a" fill="#3f3f46" name="Occasional" radius={[3, 3, 0, 0]} />
<Line type="monotone" dataKey="regular" stroke="#818cf8" strokeWidth={2} dot={false} strokeDasharray="4 2" />
</ComposedChart>
</ResponsiveContainer>
<div className="flex gap-4 mt-2 justify-end">
<span className="flex items-center gap-1.5 text-xs text-zinc-500"><span className="w-3 h-2 rounded-sm bg-indigo-500 inline-block" />Committed (rent, utilities, insurance, subscriptions)</span>
<span className="flex items-center gap-1.5 text-xs text-zinc-500"><span className="w-3 h-2 rounded-sm bg-zinc-600 inline-block" />Discretionary</span>
<span className="flex items-center gap-1.5 text-xs text-zinc-500"><span className="w-3 h-2 rounded-sm bg-indigo-500 inline-block" />Regular (groceries, dining, transport)</span>
<span className="flex items-center gap-1.5 text-xs text-zinc-500"><span className="w-3 h-2 rounded-sm bg-zinc-600 inline-block" />Occasional</span>
</div>
</div>
</Section>
{/* ── 2. Recurring Charges ── */}
{/* ── 2. Monthly Spend Breakdown ── */}
<Section title="Monthly Spend Breakdown">
{!analytics6 ? (
<p className="text-zinc-500 text-sm">Loading...</p>
) : (
<MonthlyBreakdown analytics={analytics6} />
)}
</Section>
{/* ── 3. Recurring Charges ── */}
<Section title="Recurring Charges">
{!subData ? (
<p className="text-zinc-500 text-sm">Loading...</p>
@@ -186,7 +418,7 @@ export default function InsightsPage() {
)}
</Section>
{/* ── 3. Fees & Interest ── */}
{/* ── 4. Fees & Interest ── */}
<Section title="Fees & Interest">
{!feesData ? (
<p className="text-zinc-500 text-sm">Loading...</p>
+1 -1
View File
@@ -32,7 +32,7 @@ export default function RootLayout({
<Providers>
<div className="flex min-h-screen">
<Sidebar />
<main className="flex-1 p-6 overflow-auto">{children}</main>
<main className="flex-1 pt-[calc(3.5rem+1rem)] px-3 pb-3 md:p-6 md:pt-6 overflow-auto">{children}</main>
</div>
</Providers>
</body>
+298
View File
@@ -0,0 +1,298 @@
"use client";
import { useState } from "react";
import { usePendingReconciliations, useReconcile } from "@/lib/hooks";
import type { ManualTxWithMatches, PotentialMatch } from "@/lib/queries";
function formatDate(d: string | Date) {
return new Date(d).toLocaleDateString("en-AU", { day: "2-digit", month: "short", year: "numeric" });
}
function formatAmt(amount: number, type: string) {
const f = new Intl.NumberFormat("en-AU", { style: "currency", currency: "AUD" }).format(amount);
return ["debit", "fee", "interest"].includes(type) ? f : `+${f}`;
}
const TYPE_COLORS: Record<string, string> = {
debit: "bg-red-900/30 text-red-400",
credit: "bg-green-900/30 text-green-400",
payment: "bg-blue-900/30 text-blue-400",
refund: "bg-emerald-900/30 text-emerald-400",
fee: "bg-yellow-900/30 text-yellow-400",
interest: "bg-orange-900/30 text-orange-400",
transfer: "bg-zinc-800 text-zinc-400",
};
// Selections: manual_id → statement_tx_id or null (skip)
type Selections = Record<number, number | null>;
export default function ReconcilePage() {
const { data: pending = [], isLoading, refetch } = usePendingReconciliations();
const reconcile = useReconcile();
const [selections, setSelections] = useState<Selections>({});
const [error, setError] = useState("");
const [done, setDone] = useState<{ reconciled: number } | null>(null);
const withMatches = pending.filter((tx) => tx.matches.length > 0);
const noMatches = pending.filter((tx) => tx.matches.length === 0);
// Statement tx IDs already chosen in another row this session
const usedStatementIds = new Set(Object.values(selections).filter((v): v is number => v !== null));
function selectMatch(manualId: number, matchId: number) {
setSelections((prev) => {
// If this matchId was previously selected for a different manual tx, clear that
const updated: Selections = { ...prev };
for (const [k, v] of Object.entries(updated)) {
if (v === matchId && Number(k) !== manualId) delete updated[Number(k)];
}
updated[manualId] = matchId;
return updated;
});
}
function skipManual(manualId: number) {
setSelections((prev) => ({ ...prev, [manualId]: null }));
}
const confirmedMatches = Object.entries(selections)
.filter(([, v]) => v !== null)
.map(([k, v]) => ({ manual_id: Number(k), statement_tx_id: v as number }));
async function handleApply() {
if (!confirmedMatches.length) return;
setError("");
try {
const result = await reconcile.mutateAsync(confirmedMatches);
setDone(result);
setSelections({});
refetch();
} catch (e) {
setError(e instanceof Error ? e.message : "Reconcile failed");
}
}
if (isLoading) {
return <div className="p-6 text-zinc-500 text-sm">Loading...</div>;
}
if (done) {
return (
<div className="p-6 max-w-lg">
<div className="bg-emerald-900/20 border border-emerald-700/50 rounded-xl p-6 text-center space-y-2">
<p className="text-emerald-400 font-semibold text-lg"> {done.reconciled} transaction{done.reconciled !== 1 ? "s" : ""} reconciled</p>
<p className="text-zinc-400 text-sm">Overrides, tags, and splits copied to statement transactions.</p>
<button onClick={() => setDone(null)} className="mt-3 px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
Continue reconciling
</button>
</div>
</div>
);
}
if (pending.length === 0) {
return (
<div className="p-6">
<h2 className="text-xl font-semibold mb-2">Reconcile</h2>
<p className="text-zinc-500 text-sm">No unreconciled manual transactions. Import a CSV to get started.</p>
</div>
);
}
return (
<div className="p-6 space-y-4">
<div className="flex items-center justify-between">
<div>
<h2 className="text-xl font-semibold">Reconcile</h2>
<p className="text-xs text-zinc-500 mt-0.5">
{pending.length} manual transaction{pending.length !== 1 ? "s" : ""} ·{" "}
{withMatches.length} with potential matches
</p>
</div>
{confirmedMatches.length > 0 && (
<button
onClick={handleApply}
disabled={reconcile.isPending}
className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium disabled:opacity-50"
>
{reconcile.isPending ? "Reconciling..." : `Apply ${confirmedMatches.length} match${confirmedMatches.length !== 1 ? "es" : ""}`}
</button>
)}
</div>
{error && <p className="text-red-400 text-sm">{error}</p>}
{/* Transactions with matches */}
{withMatches.map((tx) => (
<ReconcileRow
key={tx.id}
tx={tx}
selection={selections[tx.id]}
usedStatementIds={usedStatementIds}
onSelect={(matchId) => selectMatch(tx.id, matchId)}
onSkip={() => skipManual(tx.id)}
onClear={() => setSelections((prev) => { const n = { ...prev }; delete n[tx.id]; return n; })}
/>
))}
{/* Transactions with no matches */}
{noMatches.length > 0 && (
<div className="border border-zinc-800 rounded-xl overflow-hidden">
<div className="px-4 py-3 bg-zinc-900/60 border-b border-zinc-800">
<p className="text-sm font-medium text-zinc-400">No statement matches found ({noMatches.length})</p>
<p className="text-xs text-zinc-600 mt-0.5">These may not have hit a statement yet, or the statement hasn't been imported.</p>
</div>
<div className="divide-y divide-zinc-800">
{noMatches.map((tx) => (
<div key={tx.id} className="px-4 py-3 flex items-center gap-3">
<span className="text-zinc-500 text-xs w-20 flex-shrink-0">{formatDate(tx.transaction_date)}</span>
<span className="text-zinc-300 text-sm flex-1 truncate">{tx.effective_merchant || tx.description}</span>
<span className={`text-sm font-mono ${["debit","fee","interest"].includes(tx.transaction_type) ? "text-red-400" : "text-green-400"}`}>
{formatAmt(tx.amount, tx.transaction_type)}
</span>
<div className="flex gap-1">
{(tx.tags as { id: number; name: string; color: string }[]).map((tag) => (
<span key={tag.id} className="w-2 h-2 rounded-full" style={{ backgroundColor: tag.color }} title={tag.name} />
))}
</div>
</div>
))}
</div>
</div>
)}
</div>
);
}
function ReconcileRow({
tx, selection, usedStatementIds, onSelect, onSkip, onClear,
}: {
tx: ManualTxWithMatches;
selection: number | null | undefined;
usedStatementIds: Set<number>;
onSelect: (matchId: number) => void;
onSkip: () => void;
onClear: () => void;
}) {
const isSkipped = selection === null;
const selectedMatchId = typeof selection === "number" ? selection : null;
const tags = tx.tags as { id: number; name: string; color: string }[];
return (
<div className={`border rounded-xl overflow-hidden transition-colors ${
selectedMatchId ? "border-emerald-700/60" : isSkipped ? "border-zinc-700/40 opacity-60" : "border-zinc-700"
}`}>
{/* Manual tx header */}
<div className="px-4 py-3 bg-zinc-900/60 border-b border-zinc-800 flex items-center gap-3">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-zinc-500 text-xs flex-shrink-0">{formatDate(tx.transaction_date)}</span>
<span className="text-zinc-200 text-sm truncate">{tx.effective_merchant || tx.description}</span>
{tx.effective_merchant && (
<span className="text-zinc-600 text-xs truncate hidden sm:inline">{tx.description}</span>
)}
</div>
{tags.length > 0 && (
<div className="flex gap-1 mt-1">
{tags.map((tag) => (
<span key={tag.id} className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-xs"
style={{ backgroundColor: tag.color + "33", color: tag.color }}>
{tag.name}
</span>
))}
</div>
)}
</div>
<span className={`text-sm font-mono flex-shrink-0 ${["debit","fee","interest"].includes(tx.transaction_type) ? "text-red-400" : "text-green-400"}`}>
{formatAmt(tx.amount, tx.transaction_type)}
</span>
<span className={`px-2 py-0.5 rounded text-xs font-medium flex-shrink-0 ${TYPE_COLORS[tx.transaction_type] || "bg-zinc-800 text-zinc-400"}`}>
{tx.transaction_type}
</span>
</div>
{/* Matches */}
<div className="divide-y divide-zinc-800/50">
{tx.matches.map((match) => {
const isSelected = selectedMatchId === match.id;
const isUsedElsewhere = !isSelected && usedStatementIds.has(match.id);
return (
<MatchRow
key={match.id}
match={match}
isSelected={isSelected}
isDisabled={isUsedElsewhere}
onSelect={() => onSelect(match.id)}
/>
);
})}
{/* Skip / status row */}
<div className="px-4 py-2.5 flex items-center justify-between bg-zinc-950/30">
{selectedMatchId ? (
<span className="text-emerald-400 text-xs font-medium"> Match selected</span>
) : isSkipped ? (
<span className="text-zinc-500 text-xs">Skipped will stay as manual transaction</span>
) : (
<span className="text-zinc-600 text-xs">Select a match above, or skip</span>
)}
<div className="flex gap-2">
{(selectedMatchId || isSkipped) && (
<button onClick={onClear} className="text-xs text-zinc-500 hover:text-zinc-300 px-2 py-1 rounded hover:bg-zinc-800">
Clear
</button>
)}
{!isSkipped && (
<button onClick={onSkip} className="text-xs text-zinc-500 hover:text-zinc-300 px-2 py-1 rounded hover:bg-zinc-800">
Skip (no match)
</button>
)}
</div>
</div>
</div>
</div>
);
}
function MatchRow({
match, isSelected, isDisabled, onSelect,
}: {
match: PotentialMatch;
isSelected: boolean;
isDisabled: boolean;
onSelect: () => void;
}) {
return (
<button
onClick={onSelect}
disabled={isDisabled}
className={`w-full px-4 py-2.5 flex items-center gap-3 text-left transition-colors ${
isSelected
? "bg-emerald-900/20 hover:bg-emerald-900/30"
: isDisabled
? "opacity-30 cursor-not-allowed"
: "hover:bg-zinc-800/50"
}`}
>
<div className={`w-4 h-4 rounded-full border-2 flex-shrink-0 flex items-center justify-center ${
isSelected ? "border-emerald-500 bg-emerald-500" : "border-zinc-600"
}`}>
{isSelected && <div className="w-2 h-2 rounded-full bg-white" />}
</div>
<span className="text-zinc-500 text-xs w-20 flex-shrink-0">{formatDate(match.transaction_date)}</span>
<span className="flex-1 min-w-0">
<span className="text-zinc-300 text-sm truncate block">
{match.effective_merchant || match.description}
</span>
{match.effective_merchant && (
<span className="text-zinc-600 text-xs truncate block">{match.description}</span>
)}
</span>
<span className="text-zinc-500 text-xs flex-shrink-0">{match.bank_name}</span>
<span className={`text-sm font-mono flex-shrink-0 ${["debit","fee","interest"].includes(match.transaction_type) ? "text-red-400" : "text-green-400"}`}>
{formatAmt(match.amount, match.transaction_type)}
</span>
</button>
);
}
+230 -31
View File
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { useRules, useCreateRule, useUpdateRule, useDeleteRule, useApplyRules, useTags } from "@/lib/hooks";
import { useRules, useCreateRule, useUpdateRule, useDeleteRule, useApplyRules, useRuleRuns, useRevertRuleRun, useTags, useParticipants } from "@/lib/hooks";
import { CATEGORIES, formatCategory } from "@/lib/categories";
const FIELDS = [
@@ -10,6 +10,8 @@ const FIELDS = [
{ value: "category", label: "Category" },
{ value: "bank_name", label: "Bank" },
{ value: "amount", label: "Amount" },
{ value: "transaction_type", label: "Transaction Type" },
{ value: "tag", label: "Tag" },
] as const;
const TEXT_OPS = [
@@ -24,18 +26,28 @@ const AMOUNT_OPS = [
{ value: "gt", label: ">" },
{ value: "lt", label: "<" },
];
const ENUM_OPS = [
{ value: "equals", label: "equals" },
{ value: "not_equals", label: "not equals" },
];
const TRANSACTION_TYPES = ["debit", "credit", "payment", "refund", "fee", "interest", "transfer"];
type Condition = { field: string; operator: string; value: string };
type Actions = { set_category?: string; add_tag_ids?: number[]; set_merchant?: string };
type SplitEntry = { participant_id: number; share_percent: number };
type Actions = { set_category?: string; add_tag_ids?: number[]; set_merchant?: string; apply_split?: SplitEntry[] };
function humanCondition(c: Condition): string {
function humanCondition(c: Condition, tagNames?: Map<number, string>): string {
const fieldLabel = FIELDS.find((f) => f.value === c.field)?.label || c.field;
const ops = [...TEXT_OPS, ...AMOUNT_OPS];
if (c.field === "tag") {
const tagName = tagNames?.get(Number(c.value)) || `tag#${c.value}`;
return `Tag ${c.operator === "not_equals" ? "is not" : "is"} "${tagName}"`;
}
const ops = [...TEXT_OPS, ...AMOUNT_OPS, ...ENUM_OPS];
const opText = ops.find((o) => o.value === c.operator)?.label || c.operator;
return `${fieldLabel} ${opText} "${c.value}"`;
}
function humanAction(a: Actions, tagNames: Map<number, string>): string {
function humanAction(a: Actions, tagNames: Map<number, string>, participantNames: Map<number, string>): string {
const parts: string[] = [];
if (a.set_category) parts.push(`set category: ${formatCategory(a.set_category)}`);
if (a.set_merchant) parts.push(`set merchant: ${a.set_merchant}`);
@@ -43,26 +55,62 @@ function humanAction(a: Actions, tagNames: Map<number, string>): string {
const names = a.add_tag_ids.map((id) => tagNames.get(id) || `tag#${id}`).join(", ");
parts.push(`add tags: ${names}`);
}
if (a.apply_split?.length) {
const splits = a.apply_split.map((s) => `${participantNames.get(s.participant_id) || `#${s.participant_id}`} ${s.share_percent}%`).join(", ");
parts.push(`split: ${splits}`);
}
return parts.length ? "→ " + parts.join(", ") : "(no actions)";
}
const EMPTY_ACTIONS: Actions = {};
export default function RulesPage() {
const { data: rules = [], isLoading } = useRules();
const { data: tags = [] } = useTags();
const { data: participants = [] } = useParticipants();
const createRule = useCreateRule();
const updateRule = useUpdateRule();
const deleteRule = useDeleteRule();
const applyRules = useApplyRules();
const { data: runs = [] } = useRuleRuns();
const revertRun = useRevertRuleRun();
const tagNames = new Map(tags.map((t) => [t.id, t.name]));
const participantNames = new Map(participants.map((p) => [p.id, p.name]));
const [applyFrom, setApplyFrom] = useState("2026-01-09");
const [showForm, setShowForm] = useState(false);
const [editingId, setEditingId] = useState<number | null>(null);
const [applyResult, setApplyResult] = useState<{ matched: number; transactions_affected: number } | null>(null);
const [name, setName] = useState("");
const [conditions, setConditions] = useState<Condition[]>([]);
const [actions, setActions] = useState<Actions>({});
const [actions, setActions] = useState<Actions>(EMPTY_ACTIONS);
const [priority, setPriority] = useState(0);
function openNewForm() {
setEditingId(null);
setName("");
setConditions([]);
setActions(EMPTY_ACTIONS);
setPriority(0);
setShowForm(true);
}
function openEditForm(rule: { id: number; name: string; conditions: Condition[]; actions: Actions; priority: number }) {
setEditingId(rule.id);
setName(rule.name);
setConditions(Array.isArray(rule.conditions) ? rule.conditions : []);
setActions(rule.actions && typeof rule.actions === "object" ? rule.actions : EMPTY_ACTIONS);
setPriority(rule.priority);
setShowForm(true);
window.scrollTo({ top: 0, behavior: "smooth" });
}
function closeForm() {
setShowForm(false);
setEditingId(null);
}
function addCondition() {
setConditions([...conditions, { field: "merchant_normalized", operator: "contains", value: "" }]);
}
@@ -75,35 +123,63 @@ export default function RulesPage() {
setConditions(conditions.filter((_, idx) => idx !== i));
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
await createRule.mutateAsync({ name, conditions, actions, enabled: true, priority });
setName("");
setConditions([]);
setActions({});
setPriority(0);
setShowForm(false);
function addSplitEntry() {
if (!participants.length) return;
const existing = actions.apply_split || [];
setActions({ ...actions, apply_split: [...existing, { participant_id: participants[0].id, share_percent: 0 }] });
}
async function handleApply() {
const result = await applyRules.mutateAsync();
function updateSplitEntry(i: number, patch: Partial<SplitEntry>) {
const entries = (actions.apply_split || []).map((s, idx) => (idx === i ? { ...s, ...patch } : s));
setActions({ ...actions, apply_split: entries });
}
function removeSplitEntry(i: number) {
const entries = (actions.apply_split || []).filter((_, idx) => idx !== i);
setActions({ ...actions, apply_split: entries.length ? entries : undefined });
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
const payload = { name, conditions, actions, enabled: true, priority };
if (editingId !== null) {
await updateRule.mutateAsync({ id: editingId, ...payload });
} else {
await createRule.mutateAsync(payload);
}
closeForm();
}
async function handleApply(ruleId?: number) {
const result = await applyRules.mutateAsync({ splitFrom: applyFrom || undefined, ruleId });
setApplyResult(result);
}
const splitTotal = (actions.apply_split || []).reduce((sum, s) => sum + (s.share_percent || 0), 0);
const isPending = editingId !== null ? updateRule.isPending : createRule.isPending;
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<h2 className="text-xl font-semibold">Rules</h2>
<div className="flex gap-2">
<div className="flex gap-2 items-center">
<label className="text-xs text-zinc-500 whitespace-nowrap">Splits from</label>
<input
type="date"
value={applyFrom}
onChange={(e) => setApplyFrom(e.target.value)}
className="bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
title="Split rules only apply to transactions on or after this date. Category/merchant/tag rules apply to all transactions."
/>
<button
onClick={handleApply}
onClick={() => handleApply()}
disabled={applyRules.isPending}
className="px-4 py-2 bg-emerald-600 hover:bg-emerald-500 text-white rounded-lg text-sm font-medium disabled:opacity-50"
>
{applyRules.isPending ? "Applying..." : "Apply All Rules"}
</button>
<button
onClick={() => setShowForm(!showForm)}
onClick={showForm ? closeForm : openNewForm}
className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium"
>
{showForm ? "Cancel" : "New Rule"}
@@ -123,7 +199,7 @@ export default function RulesPage() {
{showForm && (
<form onSubmit={handleSubmit} className="bg-zinc-900 border border-zinc-700 rounded-xl p-6 space-y-4">
<h3 className="font-semibold text-sm text-zinc-300">New Rule</h3>
<h3 className="font-semibold text-sm text-zinc-300">{editingId !== null ? "Edit Rule" : "New Rule"}</h3>
<div>
<label className="block text-xs text-zinc-500 mb-1">Rule Name</label>
@@ -145,17 +221,22 @@ export default function RulesPage() {
</div>
{conditions.map((cond, i) => {
const isAmount = cond.field === "amount";
const ops = isAmount ? AMOUNT_OPS : TEXT_OPS;
const isEnum = cond.field === "transaction_type";
const isTag = cond.field === "tag";
const ops = isAmount ? AMOUNT_OPS : (isEnum || isTag) ? ENUM_OPS : TEXT_OPS;
return (
<div key={i} className="flex gap-2 mb-2 items-center">
<select
value={cond.field}
onChange={(e) =>
updateCondition(i, {
field: e.target.value,
operator: e.target.value === "amount" ? "equals" : "contains",
})
}
onChange={(e) => {
const newField = e.target.value;
const patch: Partial<Condition> = { field: newField };
if (newField === "amount") { patch.operator = "equals"; patch.value = ""; }
else if (newField === "transaction_type") { patch.operator = "equals"; patch.value = "debit"; }
else if (newField === "tag") { patch.operator = "equals"; patch.value = tags[0] ? String(tags[0].id) : ""; }
else { patch.operator = "contains"; patch.value = ""; }
updateCondition(i, patch);
}}
className="bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{FIELDS.map((f) => (
@@ -175,12 +256,35 @@ export default function RulesPage() {
</option>
))}
</select>
{isTag ? (
<select
value={cond.value}
onChange={(e) => updateCondition(i, { value: e.target.value })}
className="flex-1 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{tags.map((t) => (
<option key={t.id} value={String(t.id)}>{t.name}</option>
))}
{tags.length === 0 && <option value="">No tags</option>}
</select>
) : isEnum ? (
<select
value={cond.value}
onChange={(e) => updateCondition(i, { value: e.target.value })}
className="flex-1 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{TRANSACTION_TYPES.map((t) => (
<option key={t} value={t}>{t}</option>
))}
</select>
) : (
<input
value={cond.value}
onChange={(e) => updateCondition(i, { value: e.target.value })}
placeholder="value"
className="flex-1 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
)}
<button
type="button"
onClick={() => removeCondition(i)}
@@ -252,6 +356,56 @@ export default function RulesPage() {
</div>
</div>
<div>
<div className="flex items-center justify-between mb-2">
<label className="text-xs text-zinc-500">
Apply Split (optional)
{(actions.apply_split?.length ?? 0) > 0 && (
<span className={`ml-2 ${splitTotal === 100 ? "text-emerald-400" : "text-amber-400"}`}>
{splitTotal}% total
</span>
)}
</label>
{participants.length > 0 && (
<button type="button" onClick={addSplitEntry} className="text-xs text-indigo-400 hover:text-indigo-300">
+ Add participant
</button>
)}
</div>
{(actions.apply_split || []).map((entry, i) => (
<div key={i} className="flex gap-2 mb-2 items-center">
<select
value={entry.participant_id}
onChange={(e) => updateSplitEntry(i, { participant_id: Number(e.target.value) })}
className="flex-1 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{participants.map((p) => (
<option key={p.id} value={p.id}>{p.name}</option>
))}
</select>
<input
type="number"
min={0}
max={100}
value={entry.share_percent}
onChange={(e) => updateSplitEntry(i, { share_percent: Number(e.target.value) })}
className="w-20 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
<span className="text-xs text-zinc-500">%</span>
<button
type="button"
onClick={() => removeSplitEntry(i)}
className="text-zinc-500 hover:text-red-400 text-lg leading-none px-1"
>
×
</button>
</div>
))}
{participants.length === 0 && (
<p className="text-xs text-zinc-600">No participants created yet.</p>
)}
</div>
<div className="flex items-end gap-4">
<div>
<label className="block text-xs text-zinc-500 mb-1">Priority</label>
@@ -264,15 +418,47 @@ export default function RulesPage() {
</div>
<button
type="submit"
disabled={createRule.isPending}
disabled={isPending}
className="px-6 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium disabled:opacity-50"
>
{createRule.isPending ? "Creating..." : "Create Rule"}
{isPending ? "Saving..." : editingId !== null ? "Save Changes" : "Create Rule"}
</button>
</div>
</form>
)}
{runs.length > 0 && (
<div>
<h3 className="text-sm font-medium text-zinc-400 mb-2">Apply History</h3>
<div className="space-y-2">
{runs.map((run) => (
<div key={run.id} className={`flex items-center justify-between px-4 py-2.5 rounded-lg border text-sm ${run.reverted_at ? "bg-zinc-900/40 border-zinc-800 opacity-60" : "bg-zinc-900 border-zinc-700"}`}>
<div className="flex items-center gap-4">
<span className="text-zinc-300">{new Date(run.applied_at).toLocaleString()}</span>
<span className="text-zinc-500">{run.matched} matches · {run.transactions_affected} transactions</span>
{run.split_from && <span className="text-zinc-600 text-xs">splits from {run.split_from}</span>}
</div>
{run.reverted_at ? (
<span className="text-xs text-zinc-500">reverted {new Date(run.reverted_at).toLocaleString()}</span>
) : (
<button
onClick={() => {
if (confirm("Revert this run? This will restore all affected transactions to their state before the rules were applied.")) {
revertRun.mutate(run.id);
}
}}
disabled={revertRun.isPending}
className="text-xs text-amber-400 hover:text-amber-300 disabled:opacity-50"
>
Revert
</button>
)}
</div>
))}
</div>
</div>
)}
{isLoading ? (
<p className="text-zinc-500 text-sm">Loading rules...</p>
) : rules.length === 0 ? (
@@ -292,11 +478,18 @@ export default function RulesPage() {
<span className="text-xs text-zinc-500">priority: {rule.priority}</span>
</div>
<p className="text-xs text-zinc-400">
{conds.length > 0 ? conds.map(humanCondition).join(" AND ") : "(matches all)"}
{conds.length > 0 ? conds.map((c) => humanCondition(c, tagNames)).join(" AND ") : "(matches all)"}
</p>
<p className="text-xs text-zinc-500 mt-1">{humanAction(acts, tagNames)}</p>
<p className="text-xs text-zinc-500 mt-1">{humanAction(acts, tagNames, participantNames)}</p>
</div>
<div className="flex items-center gap-3 shrink-0">
<button
onClick={() => handleApply(rule.id)}
disabled={applyRules.isPending}
className="text-xs text-emerald-400 hover:text-emerald-300 disabled:opacity-50"
>
Apply
</button>
<button
onClick={() => updateRule.mutate({ id: rule.id, enabled: !rule.enabled })}
className={`relative inline-flex h-5 w-9 rounded-full transition-colors ${
@@ -309,6 +502,12 @@ export default function RulesPage() {
}`}
/>
</button>
<button
onClick={() => openEditForm({ id: rule.id, name: rule.name, conditions: conds as Condition[], actions: acts, priority: rule.priority })}
className="text-zinc-400 hover:text-white text-sm"
>
Edit
</button>
<button
onClick={() => {
if (confirm("Delete this rule?")) deleteRule.mutate(rule.id);
+402 -60
View File
@@ -1,72 +1,392 @@
"use client";
import { useState } from "react";
import { useState, useRef, useEffect } from "react";
import {
useSharedTransactions,
useParticipantBalances,
useSettleSplits,
useParticipants,
useCreateParticipant,
useRecordPayment,
usePaymentHistory,
useDeletePayment,
useCurrentUser,
useTags,
type SplitPayment,
} from "@/lib/hooks";
import type { SharedTransactionRow } from "@/lib/queries";
import { EditTransactionModal } from "@/components/edit-transaction-modal";
function formatDate(d: string) {
return new Date(d).toLocaleDateString("en-AU", { day: "numeric", month: "short", year: "numeric" });
}
function formatAmount(n: number) {
return `$${Number(n).toFixed(2)}`;
const SPEND_TYPES = new Set(["debit", "fee", "interest"]);
function formatAmount(n: number, type?: string) {
const formatted = `$${Number(n).toFixed(2)}`;
return type && !SPEND_TYPES.has(type) ? `+${formatted}` : formatted;
}
// ── Tag multi-select ──────────────────────────────────────────────────────────
function TagFilter({ value, onChange }: { value: string[]; onChange: (v: string[]) => void }) {
const { data: tags = [] } = useTags();
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
function handler(e: MouseEvent) {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
}
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, []);
const toggle = (id: string) => {
let next: string[];
if (value.includes(id)) {
next = value.filter((x) => x !== id);
} else if (id === "untagged") {
next = ["untagged"];
} else {
next = [...value.filter((x) => x !== "untagged"), id];
}
onChange(next);
};
const label = value.length === 0 ? "All Tags"
: value.includes("untagged") ? "No tags"
: value.length === 1 ? (tags.find((t) => String(t.id) === value[0])?.name ?? "1 tag")
: `${value.length} tags`;
return (
<div ref={ref} className="relative">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className={`border rounded px-3 py-1.5 text-sm flex items-center gap-2 min-w-[120px] bg-zinc-900 ${value.length > 0 ? "border-indigo-500 text-white" : "border-zinc-700 text-zinc-400"}`}
>
<span className="flex-1 text-left">{label}</span>
<span className="text-zinc-500 text-xs"></span>
</button>
{open && (
<div className="absolute top-full mt-1 z-20 bg-zinc-900 border border-zinc-700 rounded-lg shadow-xl min-w-[160px] max-h-56 overflow-y-auto">
<label className="flex items-center gap-2 px-3 py-1.5 hover:bg-zinc-800 cursor-pointer text-sm border-b border-zinc-800">
<input type="checkbox" checked={value.includes("untagged")} onChange={() => toggle("untagged")}
className="accent-indigo-500 flex-shrink-0" />
<span className="text-zinc-400 italic">No tags</span>
</label>
{tags.map((t) => (
<label key={t.id} className="flex items-center gap-2 px-3 py-1.5 hover:bg-zinc-800 cursor-pointer text-sm">
<input type="checkbox" checked={value.includes(String(t.id))} onChange={() => toggle(String(t.id))}
className="accent-indigo-500 flex-shrink-0" />
<span className="w-2 h-2 rounded-full flex-shrink-0" style={{ backgroundColor: t.color }} />
{t.name}
</label>
))}
</div>
)}
</div>
);
}
// ── Add Participant ───────────────────────────────────────────────────────────
function AddParticipantForm({ onDone }: { onDone: () => void }) {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [error, setError] = useState("");
const create = useCreateParticipant();
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError("");
if (!name.trim()) { setError("Name is required"); return; }
try {
await create.mutateAsync({ name: name.trim(), email: email.trim() || undefined });
onDone();
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to create");
}
}
return (
<form onSubmit={handleSubmit} className="bg-zinc-900 border border-zinc-700 rounded-xl p-4 space-y-3">
<p className="text-sm font-medium">Add Participant</p>
<div className="flex gap-2">
<input type="text" placeholder="Name" value={name} onChange={(e) => setName(e.target.value)}
className="flex-1 bg-zinc-800 border border-zinc-700 rounded-lg px-3 py-1.5 text-sm focus:outline-none focus:border-zinc-500" />
<input type="email" placeholder="Email (optional)" value={email} onChange={(e) => setEmail(e.target.value)}
className="flex-1 bg-zinc-800 border border-zinc-700 rounded-lg px-3 py-1.5 text-sm focus:outline-none focus:border-zinc-500" />
<button type="submit" disabled={create.isPending}
className="px-3 py-1.5 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 text-white rounded-lg text-sm font-medium">
{create.isPending ? "Adding..." : "Add"}
</button>
<button type="button" onClick={onDone}
className="px-3 py-1.5 bg-zinc-800 hover:bg-zinc-700 text-zinc-400 rounded-lg text-sm">
Cancel
</button>
</div>
{error && <p className="text-red-400 text-xs">{error}</p>}
</form>
);
}
// ── Record Payment modal ──────────────────────────────────────────────────────
function RecordPaymentModal({
participant,
currentUserId,
currentBalance,
onClose,
}: {
participant: { id: number; name: string };
currentUserId: number;
currentBalance: number; // positive = they owe me, negative = I owe them
onClose: () => void;
}) {
const record = useRecordPayment();
const theyOweMe = currentBalance > 0;
// Default direction matches the debt direction
const [amount, setAmount] = useState(Math.abs(currentBalance).toFixed(2));
const [date, setDate] = useState(new Date().toISOString().slice(0, 10));
const [notes, setNotes] = useState("");
// direction: "received" = they paid me, "sent" = I paid them
const [direction, setDirection] = useState<"received" | "sent">(theyOweMe ? "received" : "sent");
const [error, setError] = useState("");
async function handleSave() {
setError("");
const amt = parseFloat(amount);
if (!amt || amt <= 0) { setError("Enter a valid amount"); return; }
try {
await record.mutateAsync({
from_participant_id: direction === "received" ? participant.id : currentUserId,
to_participant_id: direction === "received" ? currentUserId : participant.id,
amount: amt,
payment_date: date,
notes: notes || undefined,
});
onClose();
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to record payment");
}
}
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60" onClick={onClose}>
<div className="bg-zinc-900 border border-zinc-700 rounded-xl w-full max-w-sm mx-4 shadow-2xl p-6 space-y-4"
onClick={(e) => e.stopPropagation()}>
<h3 className="font-semibold text-sm text-zinc-300">Record Payment</h3>
{/* Direction toggle */}
<div className="flex rounded-lg overflow-hidden border border-zinc-700 text-sm">
<button
type="button"
onClick={() => setDirection("received")}
className={`flex-1 py-1.5 transition-colors ${direction === "received" ? "bg-emerald-700 text-white" : "bg-zinc-800 text-zinc-400 hover:bg-zinc-700"}`}
>
{participant.name} paid me
</button>
<button
type="button"
onClick={() => setDirection("sent")}
className={`flex-1 py-1.5 transition-colors ${direction === "sent" ? "bg-blue-700 text-white" : "bg-zinc-800 text-zinc-400 hover:bg-zinc-700"}`}
>
I paid {participant.name}
</button>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Amount</label>
<div className="relative">
<span className="absolute left-2.5 top-1/2 -translate-y-1/2 text-zinc-500 text-sm">$</span>
<input type="number" step="0.01" min="0.01" value={amount}
onChange={(e) => setAmount(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm pl-6" />
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Date</label>
<input type="date" value={date} onChange={(e) => setDate(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm" />
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Notes (optional)</label>
<input value={notes} onChange={(e) => setNotes(e.target.value)}
placeholder="e.g. Bank transfer, cash"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm" />
</div>
{error && <p className="text-red-400 text-xs">{error}</p>}
<div className="flex gap-2">
<button type="button" onClick={onClose}
className="flex-1 px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
Cancel
</button>
<button type="button" onClick={handleSave} disabled={record.isPending}
className="flex-1 px-4 py-2 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 text-white rounded-lg text-sm font-medium">
{record.isPending ? "Saving…" : "Record"}
</button>
</div>
</div>
</div>
);
}
// ── Payment history inline ────────────────────────────────────────────────────
function PaymentHistory({ participantId, currentUserId }: { participantId: number; currentUserId: number }) {
const { data: payments = [], isLoading } = usePaymentHistory(participantId);
const deletePayment = useDeletePayment();
if (isLoading) return <p className="text-xs text-zinc-600 mt-2">Loading payments</p>;
if (payments.length === 0) return <p className="text-xs text-zinc-600 italic mt-2">No payments recorded</p>;
return (
<div className="mt-3 space-y-1.5">
<p className="text-xs text-zinc-500 font-medium">Payment history</p>
{payments.map((p: SplitPayment) => {
const theyPaidMe = p.to_participant_id === currentUserId;
return (
<div key={p.id} className="flex items-center gap-2 text-xs">
<span className={`font-mono font-medium ${theyPaidMe ? "text-emerald-400" : "text-blue-400"}`}>
{theyPaidMe ? "+" : "-"}${Number(p.amount).toFixed(2)}
</span>
<span className="text-zinc-500">{formatDate(p.payment_date)}</span>
{p.notes && <span className="text-zinc-600 truncate flex-1">{p.notes}</span>}
<button
onClick={() => deletePayment.mutate(p.id)}
className="text-zinc-600 hover:text-red-400 leading-none ml-auto flex-shrink-0"
title="Delete payment"
>
×
</button>
</div>
);
})}
</div>
);
}
// ── Main page ─────────────────────────────────────────────────────────────────
type SortCol = "transaction_date" | "created_at" | "amount";
export default function SharedPage() {
const { data: transactions = [], isLoading: txLoading } = useSharedTransactions();
const { data: balances = [], isLoading: balLoading } = useParticipantBalances();
const settle = useSettleSplits();
const [settling, setSettling] = useState<number | null>(null);
const [tagIds, setTagIds] = useState<string[]>([]);
const [participantId, setParticipantId] = useState<number | undefined>(undefined);
const [sortCol, setSortCol] = useState<SortCol>("transaction_date");
const [sortDir, setSortDir] = useState<"asc" | "desc">("desc");
const realTagIds = tagIds.filter((id) => id !== "untagged");
const { data: participants = [] } = useParticipants();
const { data: rawTransactions = [], isLoading: txLoading } = useSharedTransactions(tagIds, participantId);
async function handleSettleParticipant(participantId: number) {
setSettling(participantId);
await settle.mutateAsync({ participant_id: participantId });
setSettling(null);
const transactions = [...rawTransactions].sort((a, b) => {
const av = sortCol === "amount" ? Number(a.amount) : new Date(a[sortCol]).getTime();
const bv = sortCol === "amount" ? Number(b.amount) : new Date(b[sortCol]).getTime();
return sortDir === "desc" ? bv - av : av - bv;
});
function toggleSort(col: SortCol) {
if (sortCol === col) setSortDir((d) => (d === "desc" ? "asc" : "desc"));
else { setSortCol(col); setSortDir("desc"); }
}
const others = balances.filter((b) => b.name !== "Me");
function SortIcon({ col }: { col: SortCol }) {
if (sortCol !== col) return <span className="text-zinc-600 ml-0.5"></span>;
return <span className="ml-0.5">{sortDir === "desc" ? "↓" : "↑"}</span>;
}
const { data: balances = [], isLoading: balLoading } = useParticipantBalances(realTagIds);
const { data: me } = useCurrentUser();
const [addingParticipant, setAddingParticipant] = useState(false);
const [paymentModal, setPaymentModal] = useState<{ id: number; name: string; balance: number } | null>(null);
const [showHistory, setShowHistory] = useState<number | null>(null);
const [editModal, setEditModal] = useState<SharedTransactionRow | null>(null);
return (
<div className="space-y-6">
<div className="flex items-center justify-between gap-3">
<h2 className="text-xl font-semibold">Shared Expenses</h2>
<div className="flex items-center gap-2 ml-auto flex-wrap">
<select
value={participantId ?? ""}
onChange={(e) => setParticipantId(e.target.value ? Number(e.target.value) : undefined)}
className={`border rounded px-3 py-1.5 text-sm bg-zinc-900 ${participantId ? "border-indigo-500 text-white" : "border-zinc-700 text-zinc-400"}`}
>
<option value="">All People</option>
{participants.map((p: { id: number; name: string }) => (
<option key={p.id} value={p.id}>{p.name}</option>
))}
</select>
<TagFilter value={tagIds} onChange={setTagIds} />
{!addingParticipant && (
<button onClick={() => setAddingParticipant(true)}
className="text-sm px-3 py-1.5 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg whitespace-nowrap">
+ Add Participant
</button>
)}
</div>
</div>
{/* Balance summary */}
{addingParticipant && <AddParticipantForm onDone={() => setAddingParticipant(false)} />}
{/* Balance cards */}
{realTagIds.length === 0 && tagIds.includes("untagged") ? null : realTagIds.length > 0 && (
<p className="text-xs text-zinc-500 mb-2">Showing split totals for selected tag payments excluded (payments settle overall debt, not per-tag)</p>
)}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{balLoading ? (
<p className="text-zinc-500 text-sm col-span-3">Loading balances...</p>
) : balances.length === 0 ? (
<p className="text-zinc-500 text-sm col-span-3">No participants yet.</p>
) : (
others.map((b) => (
balances.map((b) => {
const theyOweMe = b.total_owed > 0;
const net = Math.abs(b.total_owed);
const settled = net < 0.005;
return (
<div key={b.id} className="bg-zinc-900 border border-zinc-700 rounded-xl p-4">
<div className="flex items-start justify-between mb-3">
<div>
<p className="font-medium">{b.name}</p>
<p className="text-xs text-zinc-500">{b.unsettled_count} unsettled</p>
<p className="text-xs text-zinc-500">
{settled ? "all square" : theyOweMe ? `owes you` : "you owe"}
</p>
</div>
<div className="text-right">
<p className="text-lg font-semibold text-amber-400">{formatAmount(b.total_owed)}</p>
<p className="text-xs text-zinc-500">owes you</p>
<p className={`text-lg font-semibold ${settled ? "text-zinc-500" : theyOweMe ? "text-amber-400" : "text-blue-400"}`}>
${net.toFixed(2)}
</p>
</div>
</div>
{b.unsettled_count > 0 && (
<div className="flex gap-2">
<button
onClick={() => handleSettleParticipant(b.id)}
disabled={settling === b.id}
className="w-full py-1.5 text-xs font-medium bg-emerald-700 hover:bg-emerald-600 text-white rounded-lg disabled:opacity-50"
onClick={() => setPaymentModal({ id: b.id, name: b.name, balance: b.total_owed })}
className="flex-1 py-1.5 text-xs font-medium bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg"
>
{settling === b.id ? "Settling..." : "Mark All Settled"}
Record Payment
</button>
<button
onClick={() => setShowHistory(showHistory === b.id ? null : b.id)}
className={`px-3 py-1.5 text-xs rounded-lg ${showHistory === b.id ? "bg-zinc-700 text-white" : "bg-zinc-800 text-zinc-500 hover:text-zinc-300"}`}
>
History
</button>
</div>
{showHistory === b.id && me && (
<PaymentHistory participantId={b.id} currentUserId={me.id} />
)}
</div>
))
);
})
)}
</div>
{/* Transaction list */}
<div className="bg-zinc-900 border border-zinc-700 rounded-xl overflow-hidden">
<div className="bg-zinc-900 border border-zinc-700 rounded-xl overflow-x-auto">
<div className="px-4 py-3 border-b border-zinc-800">
<h3 className="text-sm font-medium">Split Transactions</h3>
</div>
@@ -77,63 +397,68 @@ export default function SharedPage() {
No split transactions yet. Use the Split button on any transaction.
</p>
) : (
<table className="w-full text-sm">
<table className="w-full text-sm min-w-[520px]">
<thead>
<tr className="border-b border-zinc-800">
<th className="text-left px-4 py-2 text-xs text-zinc-500 font-medium">Date</th>
<th className="text-left px-4 py-2 text-xs text-zinc-500 font-medium">Description</th>
<th className="text-right px-4 py-2 text-xs text-zinc-500 font-medium">Amount</th>
<th
className="text-left px-4 py-2 text-xs text-zinc-500 font-medium cursor-pointer hover:text-white whitespace-nowrap"
onClick={() => toggleSort("transaction_date")}
>
Date <SortIcon col="transaction_date" />
</th>
<th
className="text-left px-4 py-2 text-xs text-zinc-500 font-medium cursor-pointer hover:text-white whitespace-nowrap"
onClick={() => toggleSort("created_at")}
>
Imported <SortIcon col="created_at" />
</th>
<th className="text-left px-4 py-2 text-xs text-zinc-500 font-medium sticky left-0 z-10 bg-zinc-900 border-r border-zinc-800/80">Description</th>
<th
className="text-right px-4 py-2 text-xs text-zinc-500 font-medium cursor-pointer hover:text-white"
onClick={() => toggleSort("amount")}
>
Amount <SortIcon col="amount" />
</th>
<th className="text-left px-4 py-2 text-xs text-zinc-500 font-medium">Splits</th>
<th className="px-4 py-2 text-xs text-zinc-500 font-medium">Action</th>
<th className="px-4 py-2"></th>
</tr>
</thead>
<tbody>
{(transactions as SharedTransactionRow[]).map((tx) => {
const splits = Array.isArray(tx.splits) ? tx.splits : [];
const unsettled = splits.filter((s) => !s.settled && s.name !== "Me");
return (
<tr key={tx.id} className="border-b border-zinc-800/50 hover:bg-zinc-800/30">
<td className="px-4 py-3 text-zinc-400 whitespace-nowrap">
{formatDate(tx.transaction_date)}
<td className="px-4 py-3 text-zinc-400 whitespace-nowrap">{formatDate(tx.transaction_date)}</td>
<td className="px-4 py-3 text-zinc-500 text-xs whitespace-nowrap">{formatDate(tx.created_at)}</td>
<td className="px-4 py-3 max-w-xs sticky left-0 z-10 bg-zinc-900 border-r border-zinc-800/80">
<p className="font-medium break-words">{tx.effective_merchant || tx.description}</p>
{tx.effective_merchant && (
<p className="text-xs text-zinc-500 break-words">{tx.description}</p>
)}
{tx.notes && (
<p className="text-xs text-zinc-500 italic mt-0.5 break-words">{tx.notes}</p>
)}
</td>
<td className="px-4 py-3">
<p className="font-medium truncate max-w-48">{tx.effective_merchant || tx.description}</p>
<p className="text-xs text-zinc-500 truncate max-w-48">{tx.description}</p>
</td>
<td className="px-4 py-3 text-right font-medium tabular-nums">
{formatAmount(tx.amount)}
<td className={`px-4 py-3 text-right font-medium tabular-nums ${SPEND_TYPES.has(tx.transaction_type) ? "" : "text-green-400"}`}>
{formatAmount(tx.amount, tx.transaction_type)}
</td>
<td className="px-4 py-3">
<div className="flex flex-wrap gap-1">
{splits.map((s) => (
<span
key={s.participant_id}
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs ${
s.settled
? "bg-zinc-800 text-zinc-500"
: "bg-amber-900/40 text-amber-300"
}`}
>
{s.name} {s.share_percent}%
{s.settled && <span className="text-emerald-500"></span>}
<span key={s.participant_id}
className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs bg-zinc-800 text-zinc-300">
{s.participant_id === me?.id ? "Me" : s.name} {s.share_percent}%
</span>
))}
</div>
</td>
<td className="px-4 py-3 text-center">
{unsettled.length > 0 && (
<td className="px-4 py-3">
<button
onClick={() =>
settle.mutateAsync({
split_ids: unsettled.map((s) => (s as unknown as { split_id: number }).split_id),
})
}
disabled={settle.isPending}
className="text-xs px-2 py-1 bg-emerald-800 hover:bg-emerald-700 text-white rounded disabled:opacity-50"
onClick={() => setEditModal(tx)}
className="text-xs text-zinc-500 hover:text-zinc-200 px-2 py-0.5 rounded hover:bg-zinc-800 transition-colors"
>
Settle
Edit
</button>
)}
</td>
</tr>
);
@@ -142,6 +467,23 @@ export default function SharedPage() {
</table>
)}
</div>
{/* Payment modal */}
{paymentModal && me && (
<RecordPaymentModal
participant={{ id: paymentModal.id, name: paymentModal.name }}
currentUserId={me.id}
currentBalance={paymentModal.balance}
onClose={() => setPaymentModal(null)}
/>
)}
{editModal && (
<EditTransactionModal
transaction={editModal}
onClose={() => setEditModal(null)}
/>
)}
</div>
);
}
+14 -8
View File
@@ -134,12 +134,12 @@ export default function StatementsPage() {
) : !filtered.length ? (
<p className="text-zinc-500 text-sm">{hasFilters ? "No statements match filters" : "No statements found"}</p>
) : (
<div className="border border-zinc-700 rounded-xl overflow-hidden">
<table className="w-full text-sm">
<div className="border border-zinc-700 rounded-xl overflow-x-auto">
<table className="w-full text-sm min-w-[800px]">
<thead>
<tr className="border-b border-zinc-800 bg-zinc-900">
<th className="text-left px-3 py-2.5 text-xs text-zinc-600 font-medium w-8">#</th>
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium">Bank</th>
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium sticky left-0 z-10 bg-zinc-900 border-r border-zinc-800/80">Bank</th>
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium">Account</th>
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium">Period</th>
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium">Due / End</th>
@@ -147,7 +147,7 @@ export default function StatementsPage() {
<th className="text-right px-4 py-2.5 text-xs text-zinc-500 font-medium">Amount</th>
<th className="text-right px-4 py-2.5 text-xs text-zinc-500 font-medium">Txns</th>
<th className="text-left px-4 py-2.5 text-xs text-zinc-500 font-medium">Owner</th>
<th className="px-4 py-2.5"></th>
<th className="px-4 py-2.5 hidden sm:table-cell"></th>
</tr>
</thead>
<tbody>
@@ -164,13 +164,19 @@ export default function StatementsPage() {
return (
<tr key={s.id} className="border-b border-zinc-800/50 hover:bg-zinc-800/20 transition-colors">
<td className="px-3 py-3 text-xs text-zinc-600 tabular-nums">{idx + 1}</td>
<td className="px-4 py-3">
<div className="font-medium truncate max-w-[180px]" title={s.bank_name}>
<td className="px-4 py-3 sticky left-0 z-10 bg-zinc-950 border-r border-zinc-800/80">
<div className="font-medium truncate max-w-[160px]" title={s.bank_name}>
{s.bank_name}
</div>
{s.card_name && (
<div className="text-xs text-zinc-500 truncate max-w-[180px]">{s.card_name}</div>
<div className="text-xs text-zinc-500 truncate max-w-[160px]">{s.card_name}</div>
)}
<Link
href={`/transactions?statement_id=${s.id}`}
className="sm:hidden text-xs text-indigo-400 hover:text-indigo-300 mt-1 inline-block"
>
View
</Link>
</td>
<td className="px-4 py-3 text-zinc-400 font-mono text-xs">
{s.account_number}
@@ -211,7 +217,7 @@ export default function StatementsPage() {
<span className="text-zinc-600 text-xs">{s.owner_name}</span>
)}
</td>
<td className="px-4 py-3">
<td className="px-4 py-3 hidden sm:table-cell">
<Link
href={`/transactions?statement_id=${s.id}`}
className="px-3 py-1 bg-zinc-800 hover:bg-zinc-700 rounded text-xs transition-colors whitespace-nowrap"
+464 -48
View File
@@ -1,11 +1,15 @@
"use client";
import { useState, useCallback, Suspense } from "react";
import { useState, useCallback, useRef, useEffect, Suspense } from "react";
import { useSearchParams } from "next/navigation";
import { useTransactions, useBanks, useUpdateTransaction, useBulkAction, useTags, useStatement, useCreateRule } from "@/lib/hooks";
import { useTransactions, useBanks, useUpdateTransaction, useBulkAction, useTags, useStatement, useCreateRule, useParticipants, useRecordPayment, useCurrentUser } from "@/lib/hooks";
import { CATEGORIES, formatCategory } from "@/lib/categories";
import { SplitModal } from "@/components/split-modal";
import { TagPicker } from "@/components/tag-picker";
import { AddTransactionModal } from "@/components/add-transaction-modal";
import { EditTransactionModal } from "@/components/edit-transaction-modal";
import { CsvImportModal } from "@/components/csv-import-modal";
import type { TransactionRow } from "@/lib/queries";
function formatDate(d: string) {
return new Date(d).toLocaleDateString("en-AU", {
@@ -203,6 +207,248 @@ function InlineEdit({
);
}
// ── Mark as Payment modal ─────────────────────────────────────────────────────
function MarkAsPaymentModal({
transaction,
onClose,
}: {
transaction: TransactionRow;
onClose: () => void;
}) {
const { data: participants = [] } = useParticipants();
const { data: me } = useCurrentUser();
const record = useRecordPayment();
const others = participants.filter((p) => p.id !== me?.id);
const [participantId, setParticipantId] = useState<number | "">(others[0]?.id ?? "");
useEffect(() => {
if (participantId === "" && others.length > 0) {
setParticipantId(others[0].id);
}
}, [others]);
// For credits/refunds the default direction is "they paid me"
const [direction, setDirection] = useState<"received" | "sent">(
SPEND_TYPES.has(transaction.transaction_type) ? "sent" : "received"
);
const [amount, setAmount] = useState(Number(transaction.amount).toFixed(2));
const [date, setDate] = useState(transaction.transaction_date.slice(0, 10));
const [notes, setNotes] = useState("");
const [error, setError] = useState("");
const selectedParticipant = others.find((p) => p.id === participantId);
async function handleSave() {
setError("");
if (!participantId || !me) { setError("Select a participant"); return; }
const amt = parseFloat(amount);
if (!amt || amt <= 0) { setError("Enter a valid amount"); return; }
try {
await record.mutateAsync({
from_participant_id: direction === "received" ? participantId : me.id,
to_participant_id: direction === "received" ? me.id : participantId,
amount: amt,
payment_date: date,
notes: notes || undefined,
linked_transaction_id: transaction.id,
});
onClose();
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to record");
}
}
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60" onClick={onClose}>
<div
className="bg-zinc-900 border border-zinc-700 rounded-xl w-full max-w-sm mx-4 shadow-2xl p-6 space-y-4"
onClick={(e) => e.stopPropagation()}
>
<div>
<h3 className="font-semibold text-sm text-zinc-300">Record as Debt Payment</h3>
<p className="text-xs text-zinc-500 mt-0.5 truncate">{transaction.description}</p>
</div>
{/* Participant */}
<div>
<label className="block text-xs text-zinc-500 mb-1">Participant</label>
<select
value={participantId}
onChange={(e) => setParticipantId(Number(e.target.value))}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{others.map((p) => (
<option key={p.id} value={p.id}>{p.name}</option>
))}
</select>
</div>
{/* Direction */}
<div className="flex rounded-lg overflow-hidden border border-zinc-700 text-sm">
<button
type="button"
onClick={() => setDirection("received")}
className={`flex-1 py-1.5 transition-colors ${direction === "received" ? "bg-emerald-700 text-white" : "bg-zinc-800 text-zinc-400 hover:bg-zinc-700"}`}
>
{selectedParticipant?.name ?? "They"} paid me
</button>
<button
type="button"
onClick={() => setDirection("sent")}
className={`flex-1 py-1.5 transition-colors ${direction === "sent" ? "bg-blue-700 text-white" : "bg-zinc-800 text-zinc-400 hover:bg-zinc-700"}`}
>
I paid {selectedParticipant?.name ?? "them"}
</button>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Amount</label>
<div className="relative">
<span className="absolute left-2.5 top-1/2 -translate-y-1/2 text-zinc-500 text-sm">$</span>
<input
type="number" step="0.01" min="0.01" value={amount}
onChange={(e) => setAmount(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm pl-6"
/>
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Date</label>
<input
type="date" value={date} onChange={(e) => setDate(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Notes (optional)</label>
<input
value={notes} onChange={(e) => setNotes(e.target.value)}
placeholder="e.g. bank transfer reference"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
{error && <p className="text-red-400 text-xs">{error}</p>}
<div className="flex gap-2">
<button type="button" onClick={onClose}
className="flex-1 px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
Cancel
</button>
<button type="button" onClick={handleSave} disabled={record.isPending}
className="flex-1 px-4 py-2 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 text-white rounded-lg text-sm font-medium">
{record.isPending ? "Saving…" : "Record Payment"}
</button>
</div>
</div>
</div>
);
}
// ── Query bar parser ──────────────────────────────────────────────────────────
interface QueryToken { key: string; label: string }
interface ParsedQuery { text: string; amountMin?: number; amountMax?: number; tokens: QueryToken[] }
function parseQuery(input: string): ParsedQuery {
let text = input;
let amountMin: number | undefined;
let amountMax: number | undefined;
const tokens: QueryToken[] = [];
// Range shorthand: 500-1500
text = text.replace(/\b(\d+(?:\.\d+)?)\s*-\s*(\d+(?:\.\d+)?)\b/g, (_, a, b) => {
amountMin = parseFloat(a);
amountMax = parseFloat(b);
tokens.push({ key: "range", label: `$${a}$${b}` });
return "";
});
// Operators: >=500 <=500 >500 <500
text = text.replace(/(>=|<=|>|<)\s*(\d+(?:\.\d+)?)/g, (_, op, num) => {
const val = parseFloat(num);
if (op === ">") amountMin = val + 0.005;
else if (op === ">=") amountMin = val;
else if (op === "<") amountMax = Math.max(0, val - 0.005);
else if (op === "<=") amountMax = val;
const display = op === ">=" ? "≥" : op === "<=" ? "≤" : op;
tokens.push({ key: `amt_${op}`, label: `${display} $${num}` });
return "";
});
return { text: text.trim(), amountMin, amountMax, tokens };
}
// ── MultiSelect dropdown ──────────────────────────────────────────────────────
function MultiSelect({
options,
value,
onChange,
placeholder,
}: {
options: { value: string; label: string }[];
value: string[];
onChange: (v: string[]) => void;
placeholder: string;
}) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
function handler(e: MouseEvent) {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
}
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, []);
const toggle = (v: string) =>
onChange(value.includes(v) ? value.filter((x) => x !== v) : [...value, v]);
const label =
value.length === 0
? placeholder
: value.length === 1
? (options.find((o) => o.value === value[0])?.label ?? value[0])
: `${value.length} selected`;
return (
<div ref={ref} className="relative">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className={`bg-zinc-900 border rounded px-3 py-1.5 text-sm flex items-center gap-2 min-w-[140px] whitespace-nowrap ${
value.length > 0 ? "border-indigo-500 text-white" : "border-zinc-700 text-zinc-400"
}`}
>
<span className="flex-1 text-left truncate">{label}</span>
<span className="text-zinc-500 text-xs"></span>
</button>
{open && (
<div className="absolute top-full mt-1 z-20 bg-zinc-900 border border-zinc-700 rounded-lg shadow-xl min-w-[160px] max-h-64 overflow-y-auto">
{options.map((o) => (
<label
key={o.value}
className="flex items-center gap-2 px-3 py-1.5 hover:bg-zinc-800 cursor-pointer text-sm"
>
<input
type="checkbox"
checked={value.includes(o.value)}
onChange={() => toggle(o.value)}
className="accent-indigo-500 flex-shrink-0"
/>
{o.label}
</label>
))}
</div>
)}
</div>
);
}
export default function TransactionsPage() {
return (
<Suspense fallback={<p className="text-zinc-500 text-sm">Loading...</p>}>
@@ -218,20 +464,53 @@ function TransactionsContent() {
const [filters, setFilters] = useState({
from: "",
to: "",
category: "",
bank_name: "",
categories: [] as string[],
bank_names: [] as string[],
tag_ids: [] as string[],
transaction_types: [] as string[],
search: "",
statement_id: initialStatementId,
tag_id: "",
sort_by: "transaction_date",
sort_dir: "desc",
limit: 50,
offset: 0,
amount_min: undefined as number | undefined,
amount_max: undefined as number | undefined,
has_split: "" as string,
});
const [queryInput, setQueryInput] = useState("");
const [queryTokens, setQueryTokens] = useState<QueryToken[]>([]);
function handleQueryChange(val: string) {
setQueryInput(val);
const parsed = parseQuery(val);
setQueryTokens(parsed.tokens);
setFilters((f) => ({
...f,
search: parsed.text,
amount_min: parsed.amountMin,
amount_max: parsed.amountMax,
offset: 0,
}));
}
function clearQueryToken(key: string) {
// Rebuild input without the token's contribution by re-running parse on cleared input
// Simplest: just clear the whole query bar
const next = queryInput
.replace(/(>=|<=|>|<)\s*\d+(?:\.\d+)?/g, "")
.replace(/\b\d+(?:\.\d+)?\s*-\s*\d+(?:\.\d+)?\b/g, "")
.trim();
handleQueryChange(next);
}
const [selected, setSelected] = useState<Set<number>>(new Set());
const [bulkCategory, setBulkCategory] = useState("");
const [bulkTagId, setBulkTagId] = useState("");
const [splitModal, setSplitModal] = useState<{ transactionId?: number; transactionIds?: number[]; amount?: number; description: string; merchant?: string } | null>(null);
const [addModal, setAddModal] = useState<{ prefill?: Parameters<typeof AddTransactionModal>[0]["prefill"]; title?: string } | null>(null);
const [editModal, setEditModal] = useState<TransactionRow | null>(null);
const [showImportModal, setShowImportModal] = useState(false);
const [paymentModal, setPaymentModal] = useState<TransactionRow | null>(null);
const [rulePrompt, setRulePrompt] = useState<{
tx: { id: number; effective_merchant: string; description: string; bank_name: string };
field: "category" | "merchant";
@@ -241,6 +520,7 @@ function TransactionsContent() {
const { data, isLoading } = useTransactions(filters);
const { data: banks } = useBanks();
const { data: tags } = useTags();
const { data: me } = useCurrentUser();
const { data: statementInfo } = useStatement(parseInt(filters.statement_id) || 0);
const updateTxn = useUpdateTransaction();
const bulkAction = useBulkAction();
@@ -283,7 +563,23 @@ function TransactionsContent() {
return (
<div>
<h2 className="text-xl font-semibold mb-4">Transactions</h2>
<div className="flex items-center justify-between mb-4">
<h2 className="text-xl font-semibold">Transactions</h2>
<div className="flex gap-2">
<button
onClick={() => setShowImportModal(true)}
className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm font-medium"
>
Import CSV
</button>
<button
onClick={() => setAddModal({})}
className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium"
>
+ Add Transaction
</button>
</div>
</div>
{/* Statement context banner */}
{filters.statement_id && statementInfo && (
@@ -307,14 +603,38 @@ function TransactionsContent() {
)}
{/* Filter bar */}
<div className="flex flex-wrap gap-3 mb-4">
<div className="flex flex-wrap gap-3 mb-2">
{/* Smart query bar */}
<div className="flex flex-col gap-1">
<input
type="text"
placeholder="Search..."
value={filters.search}
onChange={(e) => setFilters((f) => ({ ...f, search: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm w-48"
value={queryInput}
onChange={(e) => handleQueryChange(e.target.value)}
placeholder="Search… or >500 <=1500 200-800"
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm w-full sm:w-64 font-mono placeholder:font-sans placeholder:text-zinc-600"
/>
{queryTokens.length > 0 && (
<div className="flex flex-wrap gap-1">
{queryTokens.map((tok) => (
<span
key={tok.key}
className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs bg-indigo-900/50 border border-indigo-700/50 text-indigo-300"
>
{tok.label}
<button
type="button"
onClick={() => clearQueryToken(tok.key)}
className="text-indigo-400 hover:text-white leading-none"
>
×
</button>
</span>
))}
</div>
)}
</div>
{/* Date range */}
<input
type="date"
value={filters.from}
@@ -327,35 +647,45 @@ function TransactionsContent() {
onChange={(e) => setFilters((f) => ({ ...f, to: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
/>
{/* Multi-select dropdowns */}
<MultiSelect
options={CATEGORIES.map((c) => ({ value: c, label: formatCategory(c) }))}
value={filters.categories}
onChange={(v) => setFilters((f) => ({ ...f, categories: v, offset: 0 }))}
placeholder="All Categories"
/>
<MultiSelect
options={(banks ?? []).map((b) => ({ value: b, label: b }))}
value={filters.bank_names}
onChange={(v) => setFilters((f) => ({ ...f, bank_names: v, offset: 0 }))}
placeholder="All Banks"
/>
<MultiSelect
options={[{ value: "untagged", label: "No tags" }, ...(tags ?? []).map((t) => ({ value: String(t.id), label: t.name }))]}
value={filters.tag_ids}
onChange={(v) => {
let next = v;
if (v.includes("untagged") && !filters.tag_ids.includes("untagged")) next = ["untagged"];
else if (filters.tag_ids.includes("untagged") && v.length > 1) next = v.filter((x) => x !== "untagged");
setFilters((f) => ({ ...f, tag_ids: next, offset: 0 }));
}}
placeholder="All Tags"
/>
<MultiSelect
options={TYPE_OPTIONS}
value={filters.transaction_types}
onChange={(v) => setFilters((f) => ({ ...f, transaction_types: v, offset: 0 }))}
placeholder="All Types"
/>
<select
value={filters.category}
onChange={(e) => setFilters((f) => ({ ...f, category: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
value={filters.has_split}
onChange={(e) => setFilters((f) => ({ ...f, has_split: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm text-zinc-300"
>
<option value="">All Categories</option>
{CATEGORIES.map((c) => (
<option key={c} value={c}>{formatCategory(c)}</option>
))}
</select>
<select
value={filters.bank_name}
onChange={(e) => setFilters((f) => ({ ...f, bank_name: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
>
<option value="">All Banks</option>
{banks?.map((b) => (
<option key={b} value={b}>{b}</option>
))}
</select>
<select
value={filters.tag_id}
onChange={(e) => setFilters((f) => ({ ...f, tag_id: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
>
<option value="">All Tags</option>
{tags?.map((t) => (
<option key={t.id} value={t.id}>{t.name}</option>
))}
<option value="">All Splits</option>
<option value="yes">Split only</option>
<option value="no">Unsplit only</option>
</select>
</div>
@@ -429,10 +759,10 @@ function TransactionsContent() {
{/* Table */}
<div className="overflow-x-auto border border-zinc-800 rounded-lg">
<table className="w-full text-sm">
<table className="w-full text-sm min-w-[900px]">
<thead>
<tr className="border-b border-zinc-800 bg-zinc-900/50">
<th className="p-2 w-8">
<th className="p-2 w-8 sticky left-0 z-10 bg-zinc-900">
<input
type="checkbox"
checked={data?.data.length ? selected.size === data.data.length : false}
@@ -441,11 +771,17 @@ function TransactionsContent() {
/>
</th>
<th
className="p-2 text-left cursor-pointer hover:text-white"
className="p-2 text-left cursor-pointer hover:text-white sticky left-8 z-10 bg-zinc-900 border-r border-zinc-800/80 whitespace-nowrap"
onClick={() => toggleSort("transaction_date")}
>
Date {filters.sort_by === "transaction_date" && (filters.sort_dir === "desc" ? "\u2193" : "\u2191")}
</th>
<th
className="p-2 text-left text-zinc-500 cursor-pointer hover:text-white whitespace-nowrap"
onClick={() => toggleSort("created_at")}
>
Imported {filters.sort_by === "created_at" && (filters.sort_dir === "desc" ? "\u2193" : "\u2191")}
</th>
<th className="p-2 text-left">Description</th>
<th className="p-2 text-left">Merchant</th>
<th
@@ -463,9 +799,9 @@ function TransactionsContent() {
</thead>
<tbody>
{isLoading ? (
<tr><td colSpan={10} className="p-8 text-center text-zinc-500">Loading...</td></tr>
<tr><td colSpan={11} className="p-8 text-center text-zinc-500">Loading...</td></tr>
) : !data?.data.length ? (
<tr><td colSpan={10} className="p-8 text-center text-zinc-500">No transactions found</td></tr>
<tr><td colSpan={11} className="p-8 text-center text-zinc-500">No transactions found</td></tr>
) : (
data.data.map((t) => (
<tr
@@ -474,7 +810,7 @@ function TransactionsContent() {
selected.has(t.id) ? "bg-zinc-800/40" : ""
}`}
>
<td className="p-2">
<td className={`p-2 sticky left-0 z-10 ${selected.has(t.id) ? "bg-zinc-800" : "bg-zinc-950"}`}>
<input
type="checkbox"
checked={selected.has(t.id)}
@@ -482,8 +818,14 @@ function TransactionsContent() {
className="accent-blue-600"
/>
</td>
<td className="p-2 whitespace-nowrap">{formatDate(t.transaction_date)}</td>
<td className="p-2 max-w-xs truncate" title={t.description}>{t.description}</td>
<td className={`p-2 whitespace-nowrap sticky left-8 z-10 border-r border-zinc-800/80 ${selected.has(t.id) ? "bg-zinc-800" : "bg-zinc-950"}`}>{formatDate(t.transaction_date)}</td>
<td className="p-2 whitespace-nowrap text-zinc-500 text-xs">{formatDate(t.created_at)}</td>
<td className="p-2 max-w-xs">
<p className="truncate" title={t.description}>{t.description}</p>
{t.notes && (
<p className="truncate text-xs text-zinc-500 italic mt-0.5" title={t.notes}>{t.notes}</p>
)}
</td>
<td className="p-2 max-w-[150px]">
<div className="relative">
<InlineEdit
@@ -540,14 +882,64 @@ function TransactionsContent() {
<TagPicker transactionId={t.id} currentTags={t.tags ?? []} />
</div>
</td>
<td className="p-2">
<td className="p-2 whitespace-nowrap">
<div className="flex items-center gap-1 flex-wrap">
{t.splits?.filter((s) => s.participant_id !== me?.id).map((s) => (
<span
key={s.participant_id}
className={`inline-flex items-center px-1.5 py-0.5 rounded text-xs font-medium ${
s.settled ? "bg-zinc-800 text-zinc-500" : "bg-amber-900/40 text-amber-300"
}`}
title={`${s.name}: ${s.share_percent}%${s.settled ? " (settled)" : ""}`}
>
{s.name} {s.share_percent}%
</span>
))}
<button
onClick={() => setSplitModal({ transactionId: t.id, amount: t.amount, description: t.description, merchant: t.effective_merchant || undefined, transactionIds: undefined })}
className="text-xs text-zinc-500 hover:text-zinc-200 px-2 py-0.5 rounded hover:bg-zinc-800 transition-colors"
className={`text-xs px-2 py-0.5 rounded transition-colors ${
t.splits?.some((s) => s.participant_id !== me?.id)
? "text-amber-400 hover:text-amber-200 hover:bg-zinc-800"
: "text-zinc-500 hover:text-zinc-200 hover:bg-zinc-800"
}`}
title="Split this transaction"
>
Split
</button>
</div>
<button
onClick={() => setEditModal(t)}
className="text-xs text-zinc-500 hover:text-zinc-200 px-2 py-0.5 rounded hover:bg-zinc-800 transition-colors"
title="Edit this transaction"
>
Edit
</button>
{!SPEND_TYPES.has(t.transaction_type) && (
<button
onClick={() => setPaymentModal(t)}
className="text-xs text-emerald-600 hover:text-emerald-400 px-2 py-0.5 rounded hover:bg-zinc-800 transition-colors"
title="Record as debt payment"
>
Payment
</button>
)}
<button
onClick={() => setAddModal({
title: "Duplicate Transaction",
prefill: {
date: new Date().toISOString().slice(0, 10),
description: t.description,
amount: t.amount,
transaction_type: t.transaction_type,
merchant_normalized: t.effective_merchant || undefined,
category: t.effective_category || undefined,
},
})}
className="text-xs text-zinc-500 hover:text-zinc-200 px-2 py-0.5 rounded hover:bg-zinc-800 transition-colors"
title="Duplicate this transaction"
>
Dupe
</button>
</td>
</tr>
))
@@ -568,6 +960,30 @@ function TransactionsContent() {
/>
)}
{showImportModal && <CsvImportModal onClose={() => setShowImportModal(false)} />}
{addModal && (
<AddTransactionModal
prefill={addModal.prefill}
title={addModal.title}
onClose={() => setAddModal(null)}
/>
)}
{editModal && (
<EditTransactionModal
transaction={editModal}
onClose={() => setEditModal(null)}
/>
)}
{paymentModal && (
<MarkAsPaymentModal
transaction={paymentModal}
onClose={() => setPaymentModal(null)}
/>
)}
{rulePrompt && (
<SaveAsRulePrompt
tx={rulePrompt.tx}
+265
View File
@@ -0,0 +1,265 @@
"use client";
import { useState } from "react";
import { useCreateTransaction, useParticipants, useTags } from "@/lib/hooks";
import { CATEGORIES, formatCategory } from "@/lib/categories";
const TRANSACTION_TYPES = ["debit", "credit", "payment", "refund", "fee", "interest", "transfer"];
interface Prefill {
date?: string;
description?: string;
amount?: number;
transaction_type?: string;
merchant_normalized?: string;
category?: string;
splits?: { participant_id: number; share_percent: number }[];
}
export function AddTransactionModal({
prefill,
title,
onClose,
}: {
prefill?: Prefill;
title?: string;
onClose: () => void;
}) {
const createTransaction = useCreateTransaction();
const { data: participants = [] } = useParticipants();
const { data: allTags = [] } = useTags();
const [date, setDate] = useState(prefill?.date ?? new Date().toISOString().slice(0, 10));
const [description, setDescription] = useState(prefill?.description ?? "");
const [amount, setAmount] = useState(prefill?.amount != null ? String(prefill.amount) : "");
const [type, setType] = useState(prefill?.transaction_type ?? "debit");
const [merchant, setMerchant] = useState(prefill?.merchant_normalized ?? "");
const [category, setCategory] = useState(prefill?.category ?? "");
const [selectedTagIds, setSelectedTagIds] = useState<number[]>([]);
const [splits, setSplits] = useState<{ participant_id: number; share_percent: number }[]>(
prefill?.splits ?? []
);
function addSplit() {
if (!participants.length) return;
setSplits([...splits, { participant_id: participants[0].id, share_percent: 50 }]);
}
function updateSplit(i: number, patch: Partial<{ participant_id: number; share_percent: number }>) {
setSplits(splits.map((s, idx) => (idx === i ? { ...s, ...patch } : s)));
}
function removeSplit(i: number) {
setSplits(splits.filter((_, idx) => idx !== i));
}
function toggleTag(id: number) {
setSelectedTagIds((prev) =>
prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]
);
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
const result = await createTransaction.mutateAsync({
date,
description,
amount: parseFloat(amount),
transaction_type: type,
merchant_normalized: merchant || undefined,
category: category || undefined,
splits: splits.length ? splits : undefined,
});
if (selectedTagIds.length && result?.id) {
await Promise.all(
selectedTagIds.map((tagId) =>
fetch(`/api/transactions/${result.id}/tags`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tag_id: tagId }),
})
)
);
}
onClose();
}
const splitTotal = splits.reduce((s, e) => s + (e.share_percent || 0), 0);
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60" onClick={onClose}>
<div
className="bg-zinc-900 border border-zinc-700 rounded-xl p-6 w-full max-w-md shadow-2xl space-y-4"
onClick={(e) => e.stopPropagation()}
>
<h3 className="font-semibold text-sm text-zinc-300">{title ?? "Add Transaction"}</h3>
<form onSubmit={handleSubmit} className="space-y-3">
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Date</label>
<input
type="date"
required
value={date}
onChange={(e) => setDate(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Amount</label>
<input
type="number"
step="0.01"
required
value={amount}
onChange={(e) => setAmount(e.target.value)}
placeholder="0.00"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Description</label>
<input
required
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="e.g. Coles Wyndham Vale"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Type</label>
<select
value={type}
onChange={(e) => setType(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{TRANSACTION_TYPES.map((t) => (
<option key={t} value={t}>{t}</option>
))}
</select>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Category</label>
<select
value={category}
onChange={(e) => setCategory(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
<option value=""> none </option>
{CATEGORIES.map((c) => (
<option key={c} value={c}>{formatCategory(c)}</option>
))}
</select>
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Merchant (optional)</label>
<input
value={merchant}
onChange={(e) => setMerchant(e.target.value)}
placeholder="Normalized merchant name"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
{/* Tags */}
{allTags.length > 0 && (
<div>
<label className="block text-xs text-zinc-500 mb-1">Tags (optional)</label>
<div className="flex flex-wrap gap-1.5">
{allTags.map((tag) => {
const selected = selectedTagIds.includes(tag.id);
return (
<button
key={tag.id}
type="button"
onClick={() => toggleTag(tag.id)}
className={`inline-flex items-center gap-1.5 px-2 py-0.5 rounded text-xs border transition-colors ${
selected
? "border-transparent text-white"
: "border-zinc-600 text-zinc-400 hover:border-zinc-500 hover:text-zinc-300"
}`}
style={selected ? { backgroundColor: tag.color + "cc", borderColor: tag.color } : {}}
>
<span
className="w-1.5 h-1.5 rounded-full flex-shrink-0"
style={{ backgroundColor: tag.color }}
/>
{tag.name}
</button>
);
})}
</div>
</div>
)}
{/* Splits */}
<div>
<div className="flex items-center justify-between mb-1">
<label className="text-xs text-zinc-500">
Splits (optional)
{splits.length > 0 && (
<span className={`ml-2 ${splitTotal === 100 ? "text-emerald-400" : "text-amber-400"}`}>
{splitTotal}%
</span>
)}
</label>
{participants.length > 0 && (
<button type="button" onClick={addSplit} className="text-xs text-indigo-400 hover:text-indigo-300">
+ Add
</button>
)}
</div>
{splits.map((s, i) => (
<div key={i} className="flex gap-2 mb-1.5 items-center">
<select
value={s.participant_id}
onChange={(e) => updateSplit(i, { participant_id: Number(e.target.value) })}
className="flex-1 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{participants.map((p) => (
<option key={p.id} value={p.id}>{p.name}</option>
))}
</select>
<input
type="number"
min={0}
max={100}
value={s.share_percent}
onChange={(e) => updateSplit(i, { share_percent: Number(e.target.value) })}
className="w-16 bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
<span className="text-xs text-zinc-500">%</span>
<button type="button" onClick={() => removeSplit(i)} className="text-zinc-500 hover:text-red-400 text-lg leading-none">×</button>
</div>
))}
</div>
<div className="flex gap-2 pt-1">
<button
type="submit"
disabled={createTransaction.isPending}
className="flex-1 px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium disabled:opacity-50"
>
{createTransaction.isPending ? "Saving..." : "Save Transaction"}
</button>
<button
type="button"
onClick={onClose}
className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm"
>
Cancel
</button>
</div>
</form>
</div>
</div>
);
}
+413
View File
@@ -0,0 +1,413 @@
"use client";
import { useState, useRef, useEffect } from "react";
import { CATEGORIES, formatCategory } from "@/lib/categories";
import { useImportCSV } from "@/lib/hooks";
import {
parseCSVRows, detectHasHeaders, getColumnLabels, getDataRows, applyMapping,
saveBankPreset, loadBankPresets,
type DateFormat, type ColumnMapping, type ParsedTransaction, type BankPreset,
} from "@/lib/csv-parser";
const DATE_FORMATS: DateFormat[] = ["DD/MM/YYYY", "YYYY-MM-DD", "MM/DD/YYYY", "M/D/YYYY"];
const TX_TYPES = ["debit", "credit", "payment", "refund", "fee", "interest", "transfer"];
type Step = "upload" | "map" | "review" | "done";
function ColSelect({
label, value, onChange, options, required,
}: {
label: string; value: string; onChange: (v: string) => void;
options: string[]; required?: boolean;
}) {
return (
<div>
<label className="block text-xs text-zinc-500 mb-1">{label}</label>
<select
value={value}
onChange={(e) => onChange(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{!required && <option value=""> none </option>}
{options.map((o) => <option key={o} value={o}>{o}</option>)}
</select>
</div>
);
}
export function CsvImportModal({ onClose }: { onClose: () => void }) {
const importCSV = useImportCSV();
const fileRef = useRef<HTMLInputElement>(null);
const [step, setStep] = useState<Step>("upload");
const [rawRows, setRawRows] = useState<string[][]>([]);
const [hasHeaders, setHasHeaders] = useState(false);
const [columnLabels, setColumnLabels] = useState<string[]>([]);
const [dataRows, setDataRows] = useState<string[][]>([]);
const [bankName, setBankName] = useState("");
const [dateFormat, setDateFormat] = useState<DateFormat>("DD/MM/YYYY");
const [mapping, setMapping] = useState<ColumnMapping>({
dateCol: "", descriptionCol: "", amountMode: "single", amountCol: "",
});
const [savePreset, setSavePreset] = useState(false);
const [editedRows, setEditedRows] = useState<ParsedTransaction[]>([]);
const [error, setError] = useState("");
const [presets, setPresets] = useState<BankPreset[]>([]);
const [insertedCount, setInsertedCount] = useState(0);
useEffect(() => { setPresets(loadBankPresets()); }, []);
function handleFile(file: File) {
setError("");
const reader = new FileReader();
reader.onload = (e) => {
const text = e.target?.result as string;
const rows = parseCSVRows(text);
if (rows.length === 0) { setError("No data found in file"); return; }
setRawRows(rows);
const headers = detectHasHeaders(rows, dateFormat);
setHasHeaders(headers);
const labels = getColumnLabels(rows, headers);
setColumnLabels(labels);
setDataRows(getDataRows(rows, headers));
// auto-set first columns as defaults
setMapping((m) => ({
...m,
dateCol: labels[0] ?? "",
descriptionCol: labels[1] ?? "",
amountCol: labels[2] ?? "",
}));
setStep("map");
};
reader.readAsText(file);
}
function applyPreset(preset: BankPreset) {
setBankName(preset.bankName);
setDateFormat(preset.dateFormat);
setMapping(preset.mapping);
}
function refreshColumns() {
if (!rawRows.length) return;
const headers = detectHasHeaders(rawRows, dateFormat);
setHasHeaders(headers);
const labels = getColumnLabels(rawRows, headers);
setColumnLabels(labels);
setDataRows(getDataRows(rawRows, headers));
}
function handleNext() {
setError("");
if (!bankName.trim()) { setError("Bank name is required"); return; }
if (!mapping.dateCol) { setError("Date column is required"); return; }
if (!mapping.descriptionCol) { setError("Description column is required"); return; }
if (mapping.amountMode === "single" && !mapping.amountCol) { setError("Amount column is required"); return; }
if (mapping.amountMode === "debit_credit" && !mapping.debitCol && !mapping.creditCol) {
setError("At least one of debit/credit columns is required"); return;
}
const parsed = applyMapping(dataRows, columnLabels, mapping, dateFormat);
if (parsed.length === 0) { setError("No valid transactions could be parsed — check your column mapping and date format"); return; }
if (savePreset) {
saveBankPreset({ bankName: bankName.trim(), mapping, dateFormat });
}
setEditedRows(parsed);
setStep("review");
}
async function handleImport() {
setError("");
const valid = editedRows.filter((r) => r.date && r.amount > 0 && r.description);
if (!valid.length) { setError("No valid rows to import"); return; }
try {
const result = await importCSV.mutateAsync({ bank_name: bankName, transactions: valid });
setInsertedCount(result.inserted);
setStep("done");
} catch (e) {
setError(e instanceof Error ? e.message : "Import failed");
}
}
function updateRow(i: number, patch: Partial<ParsedTransaction>) {
setEditedRows((rows) => rows.map((r, idx) => idx === i ? { ...r, ...patch } : r));
}
function deleteRow(i: number) {
setEditedRows((rows) => rows.filter((_, idx) => idx !== i));
}
const modalWidth = step === "review" ? "max-w-5xl" : step === "map" ? "max-w-xl" : "max-w-md";
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4" onClick={onClose}>
<div
className={`bg-zinc-900 border border-zinc-700 rounded-xl shadow-2xl w-full ${modalWidth} flex flex-col max-h-[90vh]`}
onClick={(e) => e.stopPropagation()}
>
{/* Header */}
<div className="flex items-center justify-between px-6 py-4 border-b border-zinc-800 flex-shrink-0">
<div>
<h3 className="font-semibold text-sm text-zinc-200">Import CSV</h3>
<div className="flex gap-2 mt-1">
{(["upload", "map", "review", "done"] as Step[]).map((s, i) => (
<span
key={s}
className={`text-xs ${step === s ? "text-indigo-400 font-medium" : "text-zinc-600"}`}
>
{i + 1}. {s.charAt(0).toUpperCase() + s.slice(1)}
</span>
))}
</div>
</div>
<button onClick={onClose} className="text-zinc-500 hover:text-zinc-300 text-xl leading-none">×</button>
</div>
{/* Body */}
<div className="overflow-y-auto flex-1 px-6 py-5">
{/* Step 1: Upload */}
{step === "upload" && (
<div className="space-y-4">
{presets.length > 0 && (
<div>
<label className="block text-xs text-zinc-500 mb-1">Load saved preset</label>
<select
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
defaultValue=""
onChange={(e) => {
const p = presets.find((x) => x.bankName === e.target.value);
if (p) applyPreset(p);
}}
>
<option value=""> select preset </option>
{presets.map((p) => <option key={p.bankName} value={p.bankName}>{p.bankName}</option>)}
</select>
</div>
)}
<div>
<input
ref={fileRef}
type="file"
accept=".csv,text/csv"
className="hidden"
onChange={(e) => { const f = e.target.files?.[0]; if (f) handleFile(f); }}
/>
<button
onClick={() => fileRef.current?.click()}
className="w-full border-2 border-dashed border-zinc-700 hover:border-indigo-500 rounded-xl py-12 text-center transition-colors"
onDragOver={(e) => e.preventDefault()}
onDrop={(e) => { e.preventDefault(); const f = e.dataTransfer.files?.[0]; if (f) handleFile(f); }}
>
<p className="text-zinc-400 text-sm">Drop a CSV file here, or click to browse</p>
<p className="text-zinc-600 text-xs mt-1">Westpac, ANZ, CBA, NAB and others</p>
</button>
</div>
{error && <p className="text-red-400 text-xs">{error}</p>}
</div>
)}
{/* Step 2: Map */}
{step === "map" && (
<div className="space-y-4">
{/* Raw preview */}
<div>
<p className="text-xs text-zinc-500 mb-2">First 3 rows from file:</p>
<div className="overflow-x-auto rounded border border-zinc-800">
<table className="text-xs text-zinc-400 w-full">
<thead>
<tr className="border-b border-zinc-800">
{columnLabels.map((h) => (
<th key={h} className="px-2 py-1.5 text-left font-medium text-zinc-300 whitespace-nowrap">{h}</th>
))}
</tr>
</thead>
<tbody>
{dataRows.slice(0, 3).map((row, i) => (
<tr key={i} className="border-b border-zinc-800/50">
{columnLabels.map((_, ci) => (
<td key={ci} className="px-2 py-1 truncate max-w-[160px]">{row[ci] ?? ""}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Bank Name</label>
<input
value={bankName}
onChange={(e) => setBankName(e.target.value)}
placeholder="e.g. Westpac"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Date Format</label>
<select
value={dateFormat}
onChange={(e) => { setDateFormat(e.target.value as DateFormat); refreshColumns(); }}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{DATE_FORMATS.map((f) => <option key={f} value={f}>{f}</option>)}
</select>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<ColSelect label="Date Column *" value={mapping.dateCol} onChange={(v) => setMapping((m) => ({ ...m, dateCol: v }))} options={columnLabels} required />
<ColSelect label="Description Column *" value={mapping.descriptionCol} onChange={(v) => setMapping((m) => ({ ...m, descriptionCol: v }))} options={columnLabels} required />
</div>
<div>
<label className="block text-xs text-zinc-500 mb-2">Amount</label>
<div className="flex gap-4 mb-2">
<label className="flex items-center gap-2 text-sm cursor-pointer">
<input type="radio" name="amtmode" value="single" checked={mapping.amountMode === "single"} onChange={() => setMapping((m) => ({ ...m, amountMode: "single" }))} className="accent-indigo-500" />
Single signed column
</label>
<label className="flex items-center gap-2 text-sm cursor-pointer">
<input type="radio" name="amtmode" value="debit_credit" checked={mapping.amountMode === "debit_credit"} onChange={() => setMapping((m) => ({ ...m, amountMode: "debit_credit" }))} className="accent-indigo-500" />
Separate debit / credit columns
</label>
</div>
{mapping.amountMode === "single" ? (
<ColSelect label="Amount Column *" value={mapping.amountCol ?? ""} onChange={(v) => setMapping((m) => ({ ...m, amountCol: v }))} options={columnLabels} required />
) : (
<div className="grid grid-cols-2 gap-3">
<ColSelect label="Debit Column" value={mapping.debitCol ?? ""} onChange={(v) => setMapping((m) => ({ ...m, debitCol: v }))} options={columnLabels} />
<ColSelect label="Credit Column" value={mapping.creditCol ?? ""} onChange={(v) => setMapping((m) => ({ ...m, creditCol: v }))} options={columnLabels} />
</div>
)}
</div>
<div className="grid grid-cols-2 gap-3">
<ColSelect label="Merchant Column (optional)" value={mapping.merchantCol ?? ""} onChange={(v) => setMapping((m) => ({ ...m, merchantCol: v || undefined }))} options={columnLabels} />
<ColSelect label="Category Column (optional)" value={mapping.categoryCol ?? ""} onChange={(v) => setMapping((m) => ({ ...m, categoryCol: v || undefined }))} options={columnLabels} />
</div>
<label className="flex items-center gap-2 text-sm cursor-pointer text-zinc-400">
<input type="checkbox" checked={savePreset} onChange={(e) => setSavePreset(e.target.checked)} className="accent-indigo-500" />
Save as preset for {bankName || "this bank"}
</label>
{error && <p className="text-red-400 text-xs">{error}</p>}
</div>
)}
{/* Step 3: Review */}
{step === "review" && (
<div>
<p className="text-xs text-zinc-500 mb-3">
{editedRows.length} transactions parsed. Edit or remove rows before importing.
</p>
<div className="overflow-x-auto rounded border border-zinc-800">
<table className="w-full text-xs">
<thead className="border-b border-zinc-800">
<tr>
{["Date", "Description", "Amount", "Type", "Merchant", "Category", ""].map((h) => (
<th key={h} className="px-2 py-2 text-left text-zinc-400 font-medium whitespace-nowrap">{h}</th>
))}
</tr>
</thead>
<tbody>
{editedRows.map((row, i) => (
<tr key={i} className="border-b border-zinc-800/50 hover:bg-zinc-800/30">
<td className="px-2 py-1">
<input type="date" value={row.date} onChange={(e) => updateRow(i, { date: e.target.value })}
className="bg-transparent border-b border-zinc-700 text-zinc-300 text-xs w-28 focus:outline-none focus:border-indigo-500" />
</td>
<td className="px-2 py-1">
<input value={row.description} onChange={(e) => updateRow(i, { description: e.target.value })}
className="bg-transparent border-b border-zinc-700 text-zinc-300 text-xs w-48 focus:outline-none focus:border-indigo-500" />
</td>
<td className="px-2 py-1">
<input type="number" step="0.01" value={row.amount} onChange={(e) => updateRow(i, { amount: parseFloat(e.target.value) || 0 })}
className="bg-transparent border-b border-zinc-700 text-zinc-300 text-xs w-20 focus:outline-none focus:border-indigo-500" />
</td>
<td className="px-2 py-1">
<select value={row.transaction_type} onChange={(e) => updateRow(i, { transaction_type: e.target.value })}
className="bg-zinc-800 border border-zinc-700 rounded px-1 py-0.5 text-xs">
{TX_TYPES.map((t) => <option key={t} value={t}>{t}</option>)}
</select>
</td>
<td className="px-2 py-1">
<input value={row.merchant_name ?? ""} onChange={(e) => updateRow(i, { merchant_name: e.target.value || undefined })}
className="bg-transparent border-b border-zinc-700 text-zinc-300 text-xs w-28 focus:outline-none focus:border-indigo-500" />
</td>
<td className="px-2 py-1">
<select value={row.category ?? ""} onChange={(e) => updateRow(i, { category: e.target.value || undefined })}
className="bg-zinc-800 border border-zinc-700 rounded px-1 py-0.5 text-xs">
<option value=""></option>
{CATEGORIES.map((c) => <option key={c} value={c}>{formatCategory(c)}</option>)}
</select>
</td>
<td className="px-2 py-1">
<button onClick={() => deleteRow(i)} className="text-zinc-600 hover:text-red-400 text-base leading-none">×</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
{error && <p className="text-red-400 text-xs mt-2">{error}</p>}
</div>
)}
{/* Step 4: Done */}
{step === "done" && (
<div className="text-center py-6 space-y-3">
<div className="text-4xl"></div>
<p className="text-zinc-200 font-medium">Imported {insertedCount} transactions</p>
<p className="text-zinc-500 text-sm">Tagged with <span className="text-indigo-400">csv-import</span></p>
<div className="flex gap-2 justify-center pt-2">
<a href="/transactions" className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
View Transactions
</a>
<a href="/reconcile" className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm">
Reconcile
</a>
</div>
</div>
)}
</div>
{/* Footer */}
{step !== "done" && (
<div className="flex gap-2 px-6 py-4 border-t border-zinc-800 flex-shrink-0">
{step === "map" && (
<button onClick={() => setStep("upload")} className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
Back
</button>
)}
{step === "review" && (
<button onClick={() => setStep("map")} className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
Back
</button>
)}
<div className="flex-1" />
<button onClick={onClose} className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm">
Cancel
</button>
{step === "map" && (
<button onClick={handleNext} className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium">
Next
</button>
)}
{step === "review" && (
<button
onClick={handleImport}
disabled={importCSV.isPending || editedRows.length === 0}
className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white rounded-lg text-sm font-medium disabled:opacity-50"
>
{importCSV.isPending ? "Importing..." : `Import ${editedRows.length} transactions`}
</button>
)}
</div>
)}
</div>
</div>
);
}
+332
View File
@@ -0,0 +1,332 @@
"use client";
import { useState } from "react";
import {
useUpdateTransaction,
useTags,
useAddTransactionTag,
useRemoveTransactionTag,
useTransactionSplits,
} from "@/lib/hooks";
import { SplitModal } from "./split-modal";
import { CATEGORIES, formatCategory } from "@/lib/categories";
import type { TransactionRow, TagRow } from "@/lib/queries";
const TRANSACTION_TYPES = ["debit", "credit", "payment", "refund", "fee", "interest", "transfer"];
const SPEND_TYPES = new Set(["debit", "fee", "interest"]);
function formatAmount(amount: number, type: string) {
const formatted = `$${Number(amount).toFixed(2)}`;
return SPEND_TYPES.has(type) ? formatted : `+${formatted}`;
}
function InlineTags({ transactionId, initialTags }: { transactionId: number; initialTags: TagRow[] }) {
const { data: allTags = [] } = useTags();
const addTag = useAddTransactionTag();
const removeTag = useRemoveTransactionTag();
const [tags, setTags] = useState<TagRow[]>(initialTags);
const [showPicker, setShowPicker] = useState(false);
const available = allTags.filter((t) => !tags.find((ct) => ct.id === t.id));
return (
<div>
<div className="flex flex-wrap gap-1 items-center">
{tags.map((tag) => (
<span
key={tag.id}
className="inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium text-white"
style={{ backgroundColor: tag.color + "99" }}
>
{tag.name}
<button
type="button"
onClick={() => {
removeTag.mutate({ transactionId, tagId: tag.id });
setTags((prev) => prev.filter((t) => t.id !== tag.id));
}}
className="ml-0.5 text-white/60 hover:text-white leading-none"
>
×
</button>
</span>
))}
{available.length > 0 && (
<button
type="button"
onClick={() => setShowPicker((v) => !v)}
className="text-xs text-zinc-500 hover:text-zinc-300 px-1.5 py-0.5 rounded hover:bg-zinc-800"
>
+ Add tag
</button>
)}
</div>
{showPicker && (
<div className="mt-1.5 flex flex-wrap gap-1">
{available.map((tag) => (
<button
key={tag.id}
type="button"
onClick={() => {
addTag.mutate({ transactionId, tagId: tag.id });
setTags((prev) => [...prev, tag]);
setShowPicker(false);
}}
className="px-2 py-0.5 rounded text-xs font-medium text-white hover:brightness-125"
style={{ backgroundColor: tag.color + "66" }}
>
{tag.name}
</button>
))}
</div>
)}
</div>
);
}
export function EditTransactionModal({
transaction,
onClose,
}: {
transaction: TransactionRow;
onClose: () => void;
}) {
const isManual = !transaction.statement_id;
const updateTxn = useUpdateTransaction();
// Editable override fields
const [merchant, setMerchant] = useState(transaction.merchant_override ?? transaction.merchant_normalized ?? "");
const [category, setCategory] = useState(transaction.effective_category ?? "");
const [type, setType] = useState(transaction.transaction_type);
const [notes, setNotes] = useState(transaction.notes ?? "");
// Manual-only direct fields
const [date, setDate] = useState(transaction.transaction_date?.slice(0, 10) ?? "");
const [description, setDescription] = useState(transaction.description);
const [amount, setAmount] = useState(String(transaction.amount));
// Splits — live via hook so they refresh after SplitModal saves
const { data: liveSplits = [] } = useTransactionSplits(transaction.id);
const [showSplitModal, setShowSplitModal] = useState(false);
const [error, setError] = useState("");
async function handleSave() {
setError("");
try {
const patch: Parameters<typeof updateTxn.mutateAsync>[0] = { id: transaction.id };
// Override fields (always)
if (merchant !== (transaction.merchant_override ?? transaction.merchant_normalized ?? ""))
patch.merchant_normalized = merchant;
if (category !== (transaction.effective_category ?? ""))
patch.category = category;
if (type !== transaction.transaction_type)
patch.transaction_type = type;
if (notes !== (transaction.notes ?? ""))
patch.notes = notes;
// Direct fields (manual only)
if (isManual) {
if (date !== transaction.transaction_date?.slice(0, 10))
patch.transaction_date = date;
if (description !== transaction.description)
patch.description = description;
if (parseFloat(amount) !== transaction.amount)
patch.amount = parseFloat(amount);
}
await updateTxn.mutateAsync(patch);
onClose();
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to save");
}
}
return (
<>
<div className="fixed inset-0 z-40 flex items-center justify-center bg-black/60" onClick={onClose}>
<div
className="bg-zinc-900 border border-zinc-700 rounded-xl w-full max-w-lg mx-4 shadow-2xl flex flex-col max-h-[90vh]"
onClick={(e) => e.stopPropagation()}
>
{/* Header */}
<div className="px-6 pt-5 pb-4 border-b border-zinc-800">
<h3 className="font-semibold text-sm text-zinc-300">Edit Transaction</h3>
<p className="text-xs text-zinc-500 mt-0.5">{transaction.bank_name}</p>
</div>
<div className="overflow-y-auto flex-1 px-6 py-4 space-y-5">
{/* Core fields — read-only for statement, editable for manual */}
{isManual ? (
<div className="space-y-3">
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Date</label>
<input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Amount</label>
<input
type="number"
step="0.01"
value={amount}
onChange={(e) => setAmount(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Description</label>
<input
value={description}
onChange={(e) => setDescription(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
</div>
) : (
<div className="bg-zinc-800/50 rounded-lg px-3 py-2.5 space-y-1">
<p className="text-sm font-medium">{transaction.description}</p>
<p className={`text-sm font-mono ${SPEND_TYPES.has(transaction.transaction_type) ? "text-red-400" : "text-green-400"}`}>
{formatAmount(transaction.amount, transaction.transaction_type)}
</p>
<p className="text-xs text-zinc-500">
{new Date(transaction.transaction_date).toLocaleDateString("en-AU", { day: "numeric", month: "short", year: "numeric" })}
</p>
</div>
)}
{/* Override fields */}
<div className="space-y-3">
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-xs text-zinc-500 mb-1">Type</label>
<select
value={type}
onChange={(e) => setType(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
{TRANSACTION_TYPES.map((t) => (
<option key={t} value={t}>{t}</option>
))}
</select>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Category</label>
<select
value={category}
onChange={(e) => setCategory(e.target.value)}
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
>
<option value=""> none </option>
{CATEGORIES.map((c) => (
<option key={c} value={c}>{formatCategory(c)}</option>
))}
</select>
</div>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Merchant</label>
<input
value={merchant}
onChange={(e) => setMerchant(e.target.value)}
placeholder="Normalized merchant name"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm"
/>
</div>
<div>
<label className="block text-xs text-zinc-500 mb-1">Notes</label>
<textarea
value={notes}
onChange={(e) => setNotes(e.target.value)}
rows={3}
placeholder="Additional context about this transaction…"
className="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1.5 text-sm resize-none"
/>
</div>
</div>
{/* Tags */}
<div>
<p className="text-xs text-zinc-500 mb-1.5">Tags</p>
<InlineTags transactionId={transaction.id} initialTags={transaction.tags ?? []} />
</div>
{/* Splits */}
<div>
<div className="flex items-center justify-between mb-1.5">
<p className="text-xs text-zinc-500">Splits</p>
<button
type="button"
onClick={() => setShowSplitModal(true)}
className="text-xs text-blue-400 hover:text-blue-300"
>
{liveSplits.length > 0 ? "Edit splits" : "Add split"}
</button>
</div>
{liveSplits.length > 0 ? (
<div className="flex flex-wrap gap-1">
{liveSplits.map((s: { participant_id: number; name: string; share_percent: number; settled: boolean }) => (
<span
key={s.participant_id}
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs ${
s.settled ? "bg-zinc-800 text-zinc-500" : "bg-amber-900/40 text-amber-300"
}`}
>
{s.name} {s.share_percent}%
{s.settled && <span className="text-emerald-500"></span>}
</span>
))}
</div>
) : (
<p className="text-xs text-zinc-600 italic">No splits</p>
)}
</div>
</div>
{/* Footer */}
<div className="px-6 py-4 border-t border-zinc-800 flex gap-2">
{error && <p className="text-red-400 text-xs flex-1 self-center">{error}</p>}
<div className="flex gap-2 ml-auto">
<button
type="button"
onClick={onClose}
className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-sm"
>
Cancel
</button>
<button
type="button"
onClick={handleSave}
disabled={updateTxn.isPending}
className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 text-white rounded-lg text-sm font-medium"
>
{updateTxn.isPending ? "Saving…" : "Save"}
</button>
</div>
</div>
</div>
</div>
{showSplitModal && (
<SplitModal
transactionId={transaction.id}
amount={transaction.amount}
description={transaction.description}
merchant={transaction.effective_merchant || undefined}
onClose={() => setShowSplitModal(false)}
/>
)}
</>
);
}
+73 -3
View File
@@ -2,6 +2,7 @@
import Link from "next/link";
import { usePathname } from "next/navigation";
import { useState, useEffect } from "react";
const NAV_ITEMS = [
{ href: "/transactions", label: "Transactions", icon: "receipt" },
@@ -12,6 +13,7 @@ const NAV_ITEMS = [
{ href: "/merchants", label: "Merchants", icon: "store" },
{ href: "/tags", label: "Tags", icon: "tag" },
{ href: "/rules", label: "Rules", icon: "settings" },
{ href: "/reconcile", label: "Reconcile", icon: "git-merge" },
];
const ICONS: Record<string, React.ReactNode> = {
@@ -51,6 +53,11 @@ const ICONS: Record<string, React.ReactNode> = {
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m1.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
</svg>
),
"git-merge": (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 3v12M18 9a3 3 0 100-6 3 3 0 000 6zm0 0v12M6 15a3 3 0 100 6 3 3 0 000-6zm0 0c0-4 3-6 6-6h6" />
</svg>
),
store: (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z" />
@@ -60,11 +67,34 @@ const ICONS: Record<string, React.ReactNode> = {
export function Sidebar() {
const pathname = usePathname();
const [open, setOpen] = useState(false);
return (
<aside className="w-56 bg-zinc-900 border-r border-zinc-800 flex flex-col min-h-screen">
<div className="p-4 border-b border-zinc-800">
// Close drawer on route change
useEffect(() => {
setOpen(false);
}, [pathname]);
// Prevent body scroll when drawer is open
useEffect(() => {
if (open) {
document.body.style.overflow = "hidden";
return () => { document.body.style.overflow = ""; };
}
}, [open]);
const navContent = (
<>
<div className="p-4 border-b border-zinc-800 flex items-center justify-between">
<h1 className="text-lg font-semibold text-white">Finance</h1>
<button
onClick={() => setOpen(false)}
className="md:hidden p-1 rounded text-zinc-400 hover:text-white hover:bg-zinc-800"
aria-label="Close menu"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<nav className="flex-1 p-2">
{NAV_ITEMS.map((item) => {
@@ -85,6 +115,46 @@ export function Sidebar() {
);
})}
</nav>
</>
);
return (
<>
{/* Mobile header bar */}
<div className="md:hidden fixed top-0 left-0 right-0 z-40 bg-zinc-900 border-b border-zinc-800 flex items-center px-4 h-14">
<button
onClick={() => setOpen(true)}
className="p-1.5 -ml-1.5 rounded text-zinc-400 hover:text-white hover:bg-zinc-800"
aria-label="Open menu"
>
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
</svg>
</button>
<span className="ml-3 text-base font-semibold text-white">Finance</span>
</div>
{/* Mobile drawer overlay */}
{open && (
<div
className="md:hidden fixed inset-0 z-50 bg-black/60 backdrop-blur-sm"
onClick={() => setOpen(false)}
/>
)}
{/* Mobile drawer */}
<aside
className={`md:hidden fixed top-0 left-0 z-50 w-64 h-full bg-zinc-900 border-r border-zinc-800 flex flex-col transform transition-transform duration-200 ease-in-out ${
open ? "translate-x-0" : "-translate-x-full"
}`}
>
{navContent}
</aside>
{/* Desktop sidebar — unchanged */}
<aside className="hidden md:flex w-56 bg-zinc-900 border-r border-zinc-800 flex-col min-h-screen">
{navContent}
</aside>
</>
);
}
+7 -7
View File
@@ -1,7 +1,7 @@
"use client";
import { useState, useEffect } from "react";
import { useParticipants, useSetSplits, useTransactionSplits, useBulkAction, useCreateRule } from "@/lib/hooks";
import { useParticipants, useSetSplits, useTransactionSplits, useBulkAction, useCreateRule, useCurrentUser } from "@/lib/hooks";
interface Split {
participant_id: number;
@@ -22,6 +22,7 @@ export function SplitModal({ transactionId, transactionIds, amount, description,
const singleId = transactionId ?? 0;
const { data: participants } = useParticipants();
const { data: currentUser } = useCurrentUser();
const { data: existingSplits } = useTransactionSplits(isBulk ? 0 : singleId);
const setSplits = useSetSplits();
const bulkAction = useBulkAction();
@@ -34,10 +35,9 @@ export function SplitModal({ transactionId, transactionIds, amount, description,
// Initialise: bulk always defaults to 100% Me; single loads existing splits
useEffect(() => {
if (!participants || participants.length === 0) return;
const me = participants.find((p) => p.name === "Me");
if (!participants || participants.length === 0 || !currentUser) return;
if (isBulk) {
if (me) setSplitsState([{ participant_id: me.id, share_percent: 100 }]);
setSplitsState([{ participant_id: currentUser.id, share_percent: 100 }]);
} else if (existingSplits && existingSplits.length > 0) {
setSplitsState(
existingSplits.map((s: { participant_id: number; share_percent: number }) => ({
@@ -45,10 +45,10 @@ export function SplitModal({ transactionId, transactionIds, amount, description,
share_percent: Number(s.share_percent),
}))
);
} else if (me) {
setSplitsState([{ participant_id: me.id, share_percent: 100 }]);
} else {
setSplitsState([{ participant_id: currentUser.id, share_percent: 100 }]);
}
}, [existingSplits, participants, isBulk]);
}, [existingSplits, participants, isBulk, currentUser]);
const total = splits.reduce((sum, s) => sum + s.share_percent, 0);
+6
View File
@@ -1,3 +1,9 @@
export const REGULAR_CATEGORIES = new Set([
"rent", "utilities", "insurance", "subscriptions",
"groceries", "dining", "transport", "fuel",
"health", "personal_care", "government", "charity", "pets",
] as const);
export const CATEGORIES = [
"groceries",
"dining",
+156
View File
@@ -0,0 +1,156 @@
export type DateFormat = "DD/MM/YYYY" | "YYYY-MM-DD" | "MM/DD/YYYY" | "M/D/YYYY";
export interface ColumnMapping {
dateCol: string;
descriptionCol: string;
amountMode: "single" | "debit_credit";
amountCol?: string;
debitCol?: string;
creditCol?: string;
merchantCol?: string;
categoryCol?: string;
}
export interface BankPreset {
bankName: string;
mapping: ColumnMapping;
dateFormat: DateFormat;
}
export interface ParsedTransaction {
date: string;
description: string;
amount: number;
transaction_type: string;
merchant_name?: string;
category?: string;
}
export function parseCSVRows(text: string): string[][] {
const rows: string[][] = [];
let row: string[] = [];
let field = "";
let inQuotes = false;
for (let i = 0; i < text.length; i++) {
const c = text[i];
const next = text[i + 1];
if (inQuotes) {
if (c === '"' && next === '"') { field += '"'; i++; }
else if (c === '"') { inQuotes = false; }
else { field += c; }
} else {
if (c === '"') { inQuotes = true; }
else if (c === ',') { row.push(field.trim()); field = ""; }
else if (c === '\r' && next === '\n') {
row.push(field.trim());
if (row.some((f) => f !== "")) rows.push(row);
row = []; field = ""; i++;
} else if (c === '\n' || c === '\r') {
row.push(field.trim());
if (row.some((f) => f !== "")) rows.push(row);
row = []; field = "";
} else { field += c; }
}
}
if (field || row.length > 0) {
row.push(field.trim());
if (row.some((f) => f !== "")) rows.push(row);
}
return rows;
}
export function parseDate(raw: string, format: DateFormat): string {
const s = raw.trim();
try {
if (format === "DD/MM/YYYY") {
const [d, m, y] = s.split("/");
if (!d || !m || !y || y.length !== 4) return "";
return `${y}-${m.padStart(2, "0")}-${d.padStart(2, "0")}`;
}
if (format === "YYYY-MM-DD") {
return /^\d{4}-\d{2}-\d{2}$/.test(s) ? s : "";
}
if (format === "MM/DD/YYYY" || format === "M/D/YYYY") {
const [m, d, y] = s.split("/");
if (!d || !m || !y || y.length !== 4) return "";
return `${y}-${m.padStart(2, "0")}-${d.padStart(2, "0")}`;
}
} catch { return ""; }
return "";
}
export function detectHasHeaders(rows: string[][], dateFormat: DateFormat): boolean {
if (rows.length === 0) return false;
return parseDate(rows[0][0] ?? "", dateFormat) === "";
}
export function getColumnLabels(rows: string[][], hasHeaders: boolean): string[] {
if (hasHeaders && rows.length > 0) {
return rows[0].map((h, i) => h || `Column ${i + 1}`);
}
const maxCols = rows.reduce((m, r) => Math.max(m, r.length), 0);
return Array.from({ length: maxCols }, (_, i) => `Column ${i + 1}`);
}
export function getDataRows(rows: string[][], hasHeaders: boolean): string[][] {
return hasHeaders ? rows.slice(1) : rows;
}
export function applyMapping(
dataRows: string[][],
columnLabels: string[],
mapping: ColumnMapping,
dateFormat: DateFormat
): ParsedTransaction[] {
const idx = (name: string) => columnLabels.indexOf(name);
const dateIdx = idx(mapping.dateCol);
const descIdx = idx(mapping.descriptionCol);
const merchantIdx = mapping.merchantCol ? idx(mapping.merchantCol) : -1;
const categoryIdx = mapping.categoryCol ? idx(mapping.categoryCol) : -1;
const results: ParsedTransaction[] = [];
for (const row of dataRows) {
const date = parseDate(row[dateIdx] ?? "", dateFormat);
const description = (row[descIdx] ?? "").trim();
if (!date || !description) continue;
let amount = 0;
let transaction_type = "debit";
if (mapping.amountMode === "single" && mapping.amountCol) {
const raw = (row[idx(mapping.amountCol)] ?? "").replace(/[^\d.\-+]/g, "");
const val = parseFloat(raw);
if (isNaN(val) || val === 0) continue;
amount = Math.abs(val);
transaction_type = val < 0 ? "debit" : "credit";
} else if (mapping.amountMode === "debit_credit") {
const debitIdx = mapping.debitCol ? idx(mapping.debitCol) : -1;
const creditIdx = mapping.creditCol ? idx(mapping.creditCol) : -1;
const dVal = parseFloat((row[debitIdx] ?? "").replace(/[^\d.]/g, ""));
const cVal = parseFloat((row[creditIdx] ?? "").replace(/[^\d.]/g, ""));
if (!isNaN(dVal) && dVal > 0) { amount = dVal; transaction_type = "debit"; }
else if (!isNaN(cVal) && cVal > 0) { amount = cVal; transaction_type = "credit"; }
else continue;
}
if (amount <= 0) continue;
const tx: ParsedTransaction = { date, description, amount, transaction_type };
if (merchantIdx >= 0 && row[merchantIdx]) tx.merchant_name = row[merchantIdx].trim();
if (categoryIdx >= 0 && row[categoryIdx]) tx.category = row[categoryIdx].trim();
results.push(tx);
}
return results;
}
export function saveBankPreset(preset: BankPreset): void {
try {
const existing = loadBankPresets().filter((p) => p.bankName !== preset.bankName);
localStorage.setItem("csv-presets", JSON.stringify([...existing, preset]));
} catch { /* localStorage unavailable */ }
}
export function loadBankPresets(): BankPreset[] {
try { return JSON.parse(localStorage.getItem("csv-presets") || "[]"); }
catch { return []; }
}
+211 -17
View File
@@ -14,21 +14,30 @@ interface TransactionsResponse {
interface TransactionFilters {
from?: string;
to?: string;
category?: string;
bank_name?: string;
categories?: string[];
bank_names?: string[];
tag_ids?: string[];
transaction_types?: string[];
search?: string;
statement_id?: string;
tag_id?: string;
sort_by?: string;
sort_dir?: string;
limit?: number;
offset?: number;
amount_min?: number;
amount_max?: number;
has_split?: string;
}
function buildParams(filters: TransactionFilters): string {
const params = new URLSearchParams();
Object.entries(filters).forEach(([key, val]) => {
if (val !== undefined && val !== "") params.set(key, String(val));
if (val === undefined || val === "") return;
if (Array.isArray(val)) {
if (val.length > 0) params.set(key, val.join(","));
} else {
params.set(key, String(val));
}
});
return params.toString();
}
@@ -83,6 +92,35 @@ export function useBanks() {
});
}
export function useCreateTransaction() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (data: {
date: string;
description: string;
amount: number;
transaction_type?: string;
merchant_normalized?: string;
category?: string;
splits?: { participant_id: number; share_percent: number }[];
}) => {
const res = await fetch("/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!res.ok) throw new Error((await res.json()).error || "Failed to create transaction");
return res.json();
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["transactions"] });
qc.invalidateQueries({ queryKey: ["splits"] });
qc.invalidateQueries({ queryKey: ["shared-transactions"] });
qc.invalidateQueries({ queryKey: ["participant-balances"] });
},
});
}
export function useUpdateTransaction() {
const qc = useQueryClient();
return useMutation({
@@ -95,6 +133,10 @@ export function useUpdateTransaction() {
merchant_normalized?: string;
notes?: string;
transaction_type?: string;
my_share_percent?: number | null;
description?: string;
amount?: number;
transaction_date?: string;
}) => {
const res = await fetch(`/api/transactions/${id}`, {
method: "PATCH",
@@ -106,6 +148,7 @@ export function useUpdateTransaction() {
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["transactions"] });
qc.invalidateQueries({ queryKey: ["transaction"] });
qc.invalidateQueries({ queryKey: ["analytics"] });
},
});
}
@@ -156,21 +199,26 @@ export function useParticipants() {
});
}
export function useParticipantBalances() {
export function useParticipantBalances(tagIds?: string[]) {
return useQuery<{ id: number; name: string; total_owed: number; unsettled_count: number }[]>({
queryKey: ["participant-balances"],
queryKey: ["participant-balances", tagIds],
queryFn: async () => {
const res = await fetch("/api/participants/balances");
const params = tagIds?.length ? `?tag_ids=${tagIds.join(",")}` : "";
const res = await fetch(`/api/participants/balances${params}`);
return res.json();
},
});
}
export function useSharedTransactions() {
export function useSharedTransactions(tagIds?: string[], participantId?: number) {
return useQuery({
queryKey: ["shared-transactions"],
queryKey: ["shared-transactions", tagIds, participantId],
queryFn: async () => {
const res = await fetch("/api/shared-transactions");
const sp = new URLSearchParams();
if (tagIds?.length) sp.set("tag_ids", tagIds.join(","));
if (participantId) sp.set("participant_id", String(participantId));
const query = sp.toString() ? `?${sp.toString()}` : "";
const res = await fetch(`/api/shared-transactions${query}`);
return res.json();
},
});
@@ -215,11 +263,43 @@ export function useSetSplits() {
});
}
export function useSettleSplits() {
export interface SplitPayment {
id: number;
from_participant_id: number;
from_name: string;
to_participant_id: number;
to_name: string;
amount: number;
payment_date: string;
notes: string | null;
linked_transaction_id: number | null;
created_at: string;
}
export function usePaymentHistory(participantId: number | null) {
return useQuery<SplitPayment[]>({
queryKey: ["split-payments", participantId],
queryFn: async () => {
if (!participantId) return [];
const res = await fetch(`/api/split-payments?participant_id=${participantId}`);
return res.json();
},
enabled: !!participantId,
});
}
export function useRecordPayment() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: { participant_id?: number; split_ids?: number[] }) => {
const res = await fetch("/api/splits/settle", {
mutationFn: async (body: {
from_participant_id: number;
to_participant_id: number;
amount: number;
payment_date: string;
notes?: string;
linked_transaction_id?: number;
}) => {
const res = await fetch("/api/split-payments", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
@@ -227,8 +307,22 @@ export function useSettleSplits() {
return res.json();
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["shared-transactions"] });
qc.invalidateQueries({ queryKey: ["participant-balances"] });
qc.invalidateQueries({ queryKey: ["split-payments"] });
},
});
}
export function useDeletePayment() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (id: number) => {
const res = await fetch(`/api/split-payments?id=${id}`, { method: "DELETE" });
return res.json();
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["participant-balances"] });
qc.invalidateQueries({ queryKey: ["split-payments"] });
},
});
}
@@ -421,14 +515,54 @@ export function useDeleteRule() {
export function useApplyRules() {
const qc = useQueryClient();
return useMutation({
mutationFn: async () => {
const res = await fetch("/api/rules/apply", { method: "POST" });
mutationFn: async (args?: { splitFrom?: string; ruleId?: number }) => {
const res = await fetch("/api/rules/apply", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ splitFrom: args?.splitFrom || null, ruleId: args?.ruleId || null }),
});
if (!res.ok) throw new Error("Failed to apply rules");
return res.json() as Promise<{ matched: number; transactions_affected: number }>;
return res.json() as Promise<{ id: number; matched: number; transactions_affected: number }>;
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["transactions"] });
qc.invalidateQueries({ queryKey: ["rules"] });
qc.invalidateQueries({ queryKey: ["rule-runs"] });
},
});
}
export interface RuleRun {
id: number;
applied_at: string;
split_from: string | null;
matched: number;
transactions_affected: number;
reverted_at: string | null;
}
export function useRuleRuns() {
return useQuery({
queryKey: ["rule-runs"],
queryFn: async () => {
const res = await fetch("/api/rules/apply");
if (!res.ok) throw new Error("Failed to fetch rule runs");
return res.json() as Promise<RuleRun[]>;
},
});
}
export function useRevertRuleRun() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (runId: number) => {
const res = await fetch(`/api/rules/apply/${runId}/revert`, { method: "POST" });
if (!res.ok) throw new Error("Failed to revert run");
return res.json() as Promise<{ reverted: number }>;
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["transactions"] });
qc.invalidateQueries({ queryKey: ["rule-runs"] });
},
});
}
@@ -597,6 +731,66 @@ export interface MerchantTxnRow {
statement_id: number;
}
// --- CSV Import & Reconcile ---
export function useImportCSV() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: {
bank_name: string;
transactions: {
date: string; description: string; amount: number; transaction_type: string;
merchant_name?: string; foreign_currency_amount?: number; foreign_currency_code?: string; category?: string;
}[];
}) => {
const res = await fetch("/api/import/csv", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) throw new Error((await res.json()).error || "Import failed");
return res.json() as Promise<{ inserted: number }>;
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["transactions"] });
qc.invalidateQueries({ queryKey: ["tags"] });
qc.invalidateQueries({ queryKey: ["reconcile-pending"] });
},
});
}
import type { ManualTxWithMatches } from "./queries";
export function usePendingReconciliations() {
return useQuery<ManualTxWithMatches[]>({
queryKey: ["reconcile-pending"],
queryFn: async () => {
const res = await fetch("/api/reconcile/pending");
if (!res.ok) throw new Error("Failed to fetch");
return res.json();
},
});
}
export function useReconcile() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (matches: { manual_id: number; statement_tx_id: number }[]) => {
const res = await fetch("/api/transactions/reconcile", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ matches }),
});
if (!res.ok) throw new Error((await res.json()).error || "Reconcile failed");
return res.json() as Promise<{ reconciled: number }>;
},
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["transactions"] });
qc.invalidateQueries({ queryKey: ["reconcile-pending"] });
},
});
}
export function useMerchantTransactions(merchant: string | null) {
return useQuery<{ transactions: MerchantTxnRow[] }>({
queryKey: ["analytics", "merchant-txns", merchant],
+356 -49
View File
@@ -8,7 +8,7 @@ export interface TagRow {
export interface TransactionRow {
id: number;
statement_id: number;
statement_id: number | null;
transaction_date: string;
description: string;
amount: number;
@@ -26,14 +26,17 @@ export interface TransactionRow {
category_override: string | null;
merchant_override: string | null;
notes: string | null;
my_share_percent: number | null;
effective_category: string;
effective_merchant: string;
// statement context
// statement context (null for manual transactions)
bank_name: string;
owner_id: number;
owner_name: string;
// tags
tags: TagRow[];
// splits
splits: { participant_id: number; name: string; share_percent: number; settled: boolean }[];
}
export interface StatementRow {
@@ -67,19 +70,26 @@ export interface StatementRow {
interface TransactionFilters {
from?: string;
to?: string;
category?: string;
bank_name?: string;
categories?: string[];
bank_names?: string[];
tag_ids?: string[];
transaction_types?: string[];
search?: string;
statement_id?: string;
tag_id?: string;
sort_by?: string;
sort_dir?: string;
limit?: number;
offset?: number;
amount_min?: number;
amount_max?: number;
has_split?: string;
}
export async function getTransactions(ownerId: number, filters: TransactionFilters) {
const conditions: string[] = [`s.owner_id = $1`];
const conditions: string[] = [
`(COALESCE(t.owner_id, s.owner_id) = $1 OR EXISTS (SELECT 1 FROM transaction_splits ts_me WHERE ts_me.transaction_id = t.id AND ts_me.participant_id = $1))`,
`NOT (t.statement_id IS NULL AND t.reconciled_with_id IS NOT NULL)`,
];
const params: unknown[] = [ownerId];
let paramIdx = 2;
@@ -91,16 +101,39 @@ export async function getTransactions(ownerId: number, filters: TransactionFilte
conditions.push(`t.transaction_date <= $${paramIdx++}`);
params.push(filters.to);
}
if (filters.category) {
conditions.push(`COALESCE(o.category_override, t.category) = $${paramIdx++}`);
params.push(filters.category);
if (filters.categories?.length) {
conditions.push(`COALESCE(o.category_override, t.category) = ANY($${paramIdx++}::text[])`);
params.push(filters.categories);
}
if (filters.bank_name) {
conditions.push(`s.bank_name = $${paramIdx++}`);
params.push(filters.bank_name);
if (filters.bank_names?.length) {
const hasManual = filters.bank_names.includes("Manual");
const bankList = filters.bank_names.filter((b) => b !== "Manual");
if (hasManual && bankList.length > 0) {
conditions.push(`(t.statement_id IS NULL OR s.bank_name = ANY($${paramIdx++}::text[]))`);
params.push(bankList);
} else if (hasManual) {
conditions.push(`t.statement_id IS NULL`);
} else {
conditions.push(`s.bank_name = ANY($${paramIdx++}::text[])`);
params.push(bankList);
}
}
if (filters.tag_ids?.length) {
const noTags = filters.tag_ids.includes("untagged");
const realTagIds = filters.tag_ids.filter((id) => id !== "untagged").map(Number);
if (noTags) {
conditions.push(`NOT EXISTS (SELECT 1 FROM transaction_tags tt2 WHERE tt2.transaction_id = t.id)`);
} else if (realTagIds.length > 0) {
conditions.push(`EXISTS (SELECT 1 FROM transaction_tags tt2 WHERE tt2.transaction_id = t.id AND tt2.tag_id = ANY($${paramIdx++}::int[]))`);
params.push(realTagIds);
}
}
if (filters.transaction_types?.length) {
conditions.push(`t.transaction_type = ANY($${paramIdx++}::text[])`);
params.push(filters.transaction_types);
}
if (filters.search) {
conditions.push(`(t.description ILIKE $${paramIdx} OR t.merchant_name ILIKE $${paramIdx})`);
conditions.push(`(t.description ILIKE $${paramIdx} OR t.merchant_name ILIKE $${paramIdx} OR COALESCE(o.merchant_normalized, t.merchant_normalized) ILIKE $${paramIdx})`);
params.push(`%${filters.search}%`);
paramIdx++;
}
@@ -108,14 +141,23 @@ export async function getTransactions(ownerId: number, filters: TransactionFilte
conditions.push(`t.statement_id = $${paramIdx++}`);
params.push(Number(filters.statement_id));
}
if (filters.tag_id) {
conditions.push(`EXISTS (SELECT 1 FROM transaction_tags tt2 WHERE tt2.transaction_id = t.id AND tt2.tag_id = $${paramIdx++})`);
params.push(Number(filters.tag_id));
if (filters.amount_min !== undefined) {
conditions.push(`t.amount >= $${paramIdx++}`);
params.push(filters.amount_min);
}
if (filters.amount_max !== undefined) {
conditions.push(`t.amount <= $${paramIdx++}`);
params.push(filters.amount_max);
}
if (filters.has_split === "yes") {
conditions.push(`EXISTS (SELECT 1 FROM transaction_splits ts_f WHERE ts_f.transaction_id = t.id)`);
} else if (filters.has_split === "no") {
conditions.push(`NOT EXISTS (SELECT 1 FROM transaction_splits ts_f WHERE ts_f.transaction_id = t.id)`);
}
const where = `WHERE ${conditions.join(" AND ")}`;
const sortCol = filters.sort_by === "amount" ? "t.amount" : "t.transaction_date";
const sortCol = filters.sort_by === "amount" ? "t.amount" : filters.sort_by === "created_at" ? "t.created_at" : "t.transaction_date";
const sortDir = filters.sort_dir === "asc" ? "ASC" : "DESC";
const limit = filters.limit || 50;
const offset = filters.offset || 0;
@@ -124,7 +166,7 @@ export async function getTransactions(ownerId: number, filters: TransactionFilte
SELECT COUNT(*)::int as total
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
JOIN statements s ON s.id = t.statement_id
LEFT JOIN statements s ON s.id = t.statement_id
${where}
`;
const countResult = await queryRaw<{ total: number }>(countSql, params);
@@ -132,32 +174,43 @@ export async function getTransactions(ownerId: number, filters: TransactionFilte
const dataSql = `
SELECT t.*,
o.category_override, o.merchant_normalized as merchant_override, o.notes,
o.category_override, o.merchant_normalized as merchant_override, o.notes, o.my_share_percent,
COALESCE(o.category_override, t.category) as effective_category,
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) as effective_merchant,
s.bank_name, s.owner_id,
COALESCE(s.bank_name, 'Manual') as bank_name,
COALESCE(t.owner_id, s.owner_id) as owner_id,
p.name as owner_name,
txn_tags.tags
COALESCE(src.created_at, t.created_at) as created_at,
txn_tags.tags,
txn_splits.splits
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
JOIN statements s ON s.id = t.statement_id
LEFT JOIN participants p ON p.id = s.owner_id
LEFT JOIN statements s ON s.id = t.statement_id
LEFT JOIN participants p ON p.id = COALESCE(t.owner_id, s.owner_id)
LEFT JOIN transactions src ON src.reconciled_with_id = t.id AND src.statement_id IS NULL
LEFT JOIN LATERAL (
SELECT COALESCE(json_agg(json_build_object('id', tg.id, 'name', tg.name, 'color', tg.color) ORDER BY tg.name), '[]'::json) as tags
FROM transaction_tags tt
JOIN tags tg ON tg.id = tt.tag_id
WHERE tt.transaction_id = t.id
) txn_tags ON true
LEFT JOIN LATERAL (
SELECT COALESCE(json_agg(json_build_object('participant_id', ts.participant_id, 'name', sp.name, 'share_percent', ts.share_percent, 'settled', ts.settled) ORDER BY sp.name), '[]'::json) as splits
FROM transaction_splits ts
JOIN participants sp ON sp.id = ts.participant_id
WHERE ts.transaction_id = t.id
) txn_splits ON true
${where}
ORDER BY ${sortCol} ${sortDir}, t.row_index ASC
LIMIT $${paramIdx++} OFFSET $${paramIdx++}
`;
params.push(limit, offset);
const raw = await queryRaw<TransactionRow & { tags: string | TagRow[] }>(dataSql, params);
const raw = await queryRaw<TransactionRow & { tags: string | TagRow[]; splits: string | TransactionRow["splits"] }>(dataSql, params);
const data = raw.map((r) => ({
...r,
tags: typeof r.tags === "string" ? JSON.parse(r.tags) : (r.tags ?? []),
splits: typeof r.splits === "string" ? JSON.parse(r.splits) : (r.splits ?? []),
})) as TransactionRow[];
return { data, total, limit, offset };
@@ -166,15 +219,16 @@ export async function getTransactions(ownerId: number, filters: TransactionFilte
export async function getTransactionById(id: number) {
const sql = `
SELECT t.*,
o.category_override, o.merchant_normalized as merchant_override, o.notes,
o.category_override, o.merchant_normalized as merchant_override, o.notes, o.my_share_percent,
COALESCE(o.category_override, t.category) as effective_category,
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) as effective_merchant,
s.bank_name, s.owner_id,
COALESCE(s.bank_name, 'Manual') as bank_name,
COALESCE(t.owner_id, s.owner_id) as owner_id,
p.name as owner_name
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
JOIN statements s ON s.id = t.statement_id
LEFT JOIN participants p ON p.id = s.owner_id
LEFT JOIN statements s ON s.id = t.statement_id
LEFT JOIN participants p ON p.id = COALESCE(t.owner_id, s.owner_id)
WHERE t.id = $1
`;
const rows = await queryRaw<TransactionRow>(sql, [id]);
@@ -220,8 +274,13 @@ export async function getMerchantSuggestions(search: string) {
}
export async function getBankNames() {
const sql = `SELECT DISTINCT bank_name FROM statements ORDER BY bank_name`;
return queryRaw<{ bank_name: string }>(sql);
const [bankRows, manualCount] = await Promise.all([
queryRaw<{ bank_name: string }>(`SELECT DISTINCT bank_name FROM statements ORDER BY bank_name`),
queryRaw<{ count: number }>(`SELECT COUNT(*)::int as count FROM transactions WHERE statement_id IS NULL`),
]);
const banks = bankRows.map((r) => r.bank_name);
if (manualCount[0]?.count > 0) banks.push("Manual");
return banks;
}
export interface ParticipantBalance {
@@ -231,25 +290,249 @@ export interface ParticipantBalance {
unsettled_count: number;
}
export async function getParticipantBalances(ownerId: number) {
export async function getParticipantBalances(ownerId: number, tagIds?: number[]) {
const params: unknown[] = [ownerId];
let tagFilter = "";
if (tagIds?.length) {
params.push(tagIds);
tagFilter = `AND EXISTS (SELECT 1 FROM transaction_tags tt WHERE tt.transaction_id = t.id AND tt.tag_id = ANY($2::int[]))`;
}
// Payments settle the total relationship between two people, not a specific tag.
// Only subtract payments when viewing the unfiltered total; with a tag filter
// active, show the raw split amount for that tag context only.
const paymentsJoin = tagIds?.length ? "" : `
LEFT JOIN (
SELECT
CASE WHEN sp.from_participant_id != $1 THEN sp.from_participant_id ELSE sp.to_participant_id END AS pid,
SUM(CASE WHEN sp.to_participant_id = $1 THEN sp.amount ELSE -sp.amount END) AS net_paid
FROM split_payments sp
WHERE sp.from_participant_id = $1 OR sp.to_participant_id = $1
GROUP BY pid
) payments ON payments.pid = p.id`;
const paymentsSelect = tagIds?.length ? "" : "- COALESCE(payments.net_paid, 0)::numeric(12,2)";
const paymentsGroup = tagIds?.length ? "" : ", payments.net_paid";
return queryRaw<ParticipantBalance>(`
SELECT p.id, p.name,
COALESCE(SUM(CASE WHEN ts.settled = false THEN t.amount * ts.share_percent / 100 ELSE 0 END), 0)::numeric(12,2) as total_owed,
COUNT(CASE WHEN ts.settled = false THEN 1 END)::int as unsettled_count
COALESCE(SUM(splits.signed_amount), 0)::numeric(12,2)
${paymentsSelect} AS total_owed,
COALESCE(SUM(splits.split_count), 0)::int AS unsettled_count
FROM participants p
LEFT JOIN transaction_splits ts ON ts.participant_id = p.id
LEFT JOIN transactions t ON t.id = ts.transaction_id
LEFT JOIN (
-- They owe me: their splits on transactions I own
SELECT ts.participant_id AS pid,
(CASE WHEN t.transaction_type IN ('debit', 'fee', 'interest') THEN t.amount ELSE -t.amount END) * ts.share_percent / 100 AS signed_amount,
1 AS split_count
FROM transaction_splits ts
JOIN transactions t ON t.id = ts.transaction_id
LEFT JOIN statements s ON s.id = t.statement_id
WHERE (s.owner_id = $1 OR s.id IS NULL)
GROUP BY p.id, p.name
WHERE COALESCE(t.owner_id, s.owner_id) = $1 AND ts.participant_id != $1
AND NOT (t.statement_id IS NULL AND t.reconciled_with_id IS NOT NULL)
${tagFilter}
UNION ALL
-- I owe them: my splits on transactions they own
SELECT COALESCE(t.owner_id, s.owner_id) AS pid,
-((CASE WHEN t.transaction_type IN ('debit', 'fee', 'interest') THEN t.amount ELSE -t.amount END) * ts.share_percent / 100) AS signed_amount,
0 AS split_count
FROM transaction_splits ts
JOIN transactions t ON t.id = ts.transaction_id
LEFT JOIN statements s ON s.id = t.statement_id
WHERE ts.participant_id = $1 AND COALESCE(t.owner_id, s.owner_id) != $1
AND NOT (t.statement_id IS NULL AND t.reconciled_with_id IS NOT NULL)
${tagFilter}
) splits ON splits.pid = p.id
${paymentsJoin}
WHERE p.id != $1
GROUP BY p.id, p.name ${paymentsGroup}
ORDER BY p.name
`, [ownerId]);
`, params);
}
export interface SharedTransactionRow extends TransactionRow {
splits: { participant_id: number; name: string; share_percent: number; settled: boolean }[];
}
export async function ensureTag(name: string, color: string): Promise<number> {
const rows = await queryRaw<{ id: number }>(
`INSERT INTO tags (name, color) VALUES ($1, $2)
ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name
RETURNING id`,
[name, color]
);
return rows[0].id;
}
export async function batchInsertCSVTransactions(
ownerId: number,
rows: {
date: string;
description: string;
amount: number;
transaction_type: string;
merchant_name?: string;
foreign_currency_amount?: number;
foreign_currency_code?: string;
category?: string;
}[],
tagId: number
): Promise<number> {
if (rows.length === 0) return 0;
const baseRows = await queryRaw<{ base: number }>(
`SELECT COALESCE(MAX(row_index), -1) as base FROM transactions WHERE owner_id = $1 AND statement_id IS NULL`,
[ownerId]
);
const base = Number(baseRows[0].base);
const valueClauses: string[] = [];
const params: unknown[] = [ownerId];
let p = 2;
rows.forEach((r, i) => {
valueClauses.push(`(NULL, $1, $${p++}, $${p++}, $${p++}, $${p++}, $${p++}, $${p++}, $${p++}, ${base + 1 + i})`);
params.push(r.date, r.description, r.amount, r.transaction_type, r.merchant_name ?? null, r.foreign_currency_amount ?? null, r.foreign_currency_code ?? null);
});
const txIds = await queryRaw<{ id: number }>(
`INSERT INTO transactions (statement_id, owner_id, transaction_date, description, amount, transaction_type, merchant_name, foreign_currency_amount, foreign_currency_code, row_index)
VALUES ${valueClauses.join(", ")}
RETURNING id`,
params
);
if (txIds.length > 0) {
const tagValueClauses = txIds.map((_, i) => `($${i * 2 + 1}, $${i * 2 + 2})`);
const tagParams: unknown[] = txIds.flatMap((r) => [r.id, tagId]);
await queryRaw(
`INSERT INTO transaction_tags (transaction_id, tag_id) VALUES ${tagValueClauses.join(", ")} ON CONFLICT DO NOTHING`,
tagParams
);
}
return txIds.length;
}
export interface PotentialMatch {
id: number;
transaction_date: string;
description: string;
amount: number;
transaction_type: string;
effective_merchant: string;
effective_category: string;
bank_name: string;
billing_end_date: string | null;
}
export interface ManualTxWithMatches extends TransactionRow {
matches: PotentialMatch[];
}
export async function getPendingReconciliations(ownerId: number): Promise<ManualTxWithMatches[]> {
// Fetch all unreconciled manual transactions
const raw = await queryRaw<TransactionRow & { tags: string | TagRow[]; splits: string | TransactionRow["splits"] }>(
`SELECT t.*,
o.category_override, o.merchant_normalized as merchant_override, o.notes, o.my_share_percent,
COALESCE(o.category_override, t.category) as effective_category,
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) as effective_merchant,
'Manual' as bank_name,
t.owner_id,
p.name as owner_name,
txn_tags.tags,
txn_splits.splits
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
LEFT JOIN participants p ON p.id = t.owner_id
LEFT JOIN LATERAL (
SELECT COALESCE(json_agg(json_build_object('id', tg.id, 'name', tg.name, 'color', tg.color) ORDER BY tg.name), '[]'::json) as tags
FROM transaction_tags tt JOIN tags tg ON tg.id = tt.tag_id
WHERE tt.transaction_id = t.id
) txn_tags ON true
LEFT JOIN LATERAL (
SELECT COALESCE(json_agg(json_build_object('participant_id', ts.participant_id, 'name', sp.name, 'share_percent', ts.share_percent, 'settled', ts.settled) ORDER BY sp.name), '[]'::json) as splits
FROM transaction_splits ts JOIN participants sp ON sp.id = ts.participant_id
WHERE ts.transaction_id = t.id
) txn_splits ON true
WHERE t.statement_id IS NULL AND t.owner_id = $1 AND t.reconciled_with_id IS NULL
ORDER BY t.transaction_date DESC, t.row_index ASC`,
[ownerId]
);
const manualTxs = raw.map((r) => ({
...r,
tags: typeof r.tags === "string" ? JSON.parse(r.tags) : (r.tags ?? []),
splits: typeof r.splits === "string" ? JSON.parse(r.splits) : (r.splits ?? []),
})) as TransactionRow[];
if (manualTxs.length === 0) return [];
// Fetch all potential matches in one query using window function
const matchRows = await queryRaw<PotentialMatch & { manual_id: number; rn: number }>(
`SELECT manual_id, id, transaction_date, description, amount, transaction_type,
effective_merchant, effective_category, bank_name, billing_end_date
FROM (
SELECT
m.id AS manual_id,
t.id,
t.transaction_date,
t.description,
t.amount,
t.transaction_type,
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name, '') AS effective_merchant,
COALESCE(o.category_override, t.category, '') AS effective_category,
s.bank_name,
s.billing_end_date,
ROW_NUMBER() OVER (
PARTITION BY m.id
ORDER BY ABS(t.amount - m.amount), ABS(t.transaction_date - m.transaction_date)
) AS rn
FROM transactions m
JOIN transactions t ON t.statement_id IS NOT NULL
AND t.transaction_date BETWEEN m.transaction_date - 3 AND m.transaction_date + 3
AND t.amount BETWEEN m.amount * 0.99 AND m.amount * 1.01
JOIN statements s ON s.id = t.statement_id
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
WHERE m.statement_id IS NULL
AND m.owner_id = $1
AND m.reconciled_with_id IS NULL
AND COALESCE(t.owner_id, s.owner_id) = $1
AND NOT EXISTS (
SELECT 1 FROM transactions mt WHERE mt.reconciled_with_id = t.id
)
) sq
WHERE rn <= 5
ORDER BY manual_id, rn`,
[ownerId]
);
// Group matches by manual_id
const matchesByManualId = new Map<number, PotentialMatch[]>();
for (const row of matchRows) {
const list = matchesByManualId.get(row.manual_id) ?? [];
list.push({
id: row.id,
transaction_date: row.transaction_date,
description: row.description,
amount: row.amount,
transaction_type: row.transaction_type,
effective_merchant: row.effective_merchant,
effective_category: row.effective_category,
bank_name: row.bank_name,
billing_end_date: row.billing_end_date,
});
matchesByManualId.set(row.manual_id, list);
}
return manualTxs.map((tx) => ({
...tx,
matches: matchesByManualId.get(tx.id) ?? [],
}));
}
export async function getTags() {
return queryRaw<TagRow & { transaction_count: number }>(`
SELECT tg.id, tg.name, tg.color,
@@ -261,14 +544,31 @@ export async function getTags() {
`);
}
export async function getSharedTransactions(ownerId: number) {
export async function getSharedTransactions(ownerId: number, tagIds?: number[], noTags?: boolean, participantId?: number) {
const params: unknown[] = [ownerId];
let tagClause = "";
if (noTags) {
tagClause = `AND NOT EXISTS (SELECT 1 FROM transaction_tags tt WHERE tt.transaction_id = t.id)`;
} else if (tagIds?.length) {
params.push(tagIds);
tagClause = `AND EXISTS (SELECT 1 FROM transaction_tags tt WHERE tt.transaction_id = t.id AND tt.tag_id = ANY($2::int[]))`;
}
let participantClause = "";
if (participantId) {
params.push(participantId);
participantClause = `AND EXISTS (SELECT 1 FROM transaction_splits ts_p WHERE ts_p.transaction_id = t.id AND ts_p.participant_id = $${params.length})`;
}
const rows = await queryRaw<TransactionRow & { split_data: string }>(`
SELECT t.*,
o.category_override, o.merchant_normalized as merchant_override, o.notes,
COALESCE(o.category_override, t.category) as effective_category,
COALESCE(o.merchant_normalized, t.merchant_normalized, t.merchant_name) as effective_merchant,
s.bank_name, s.owner_id,
COALESCE(s.bank_name, 'Manual') as bank_name,
COALESCE(t.owner_id, s.owner_id) as owner_id,
p_owner.name as owner_name,
COALESCE(src.created_at, t.created_at) as created_at,
json_agg(json_build_object(
'split_id', ts.id,
'participant_id', ts.participant_id,
@@ -280,17 +580,24 @@ export async function getSharedTransactions(ownerId: number) {
JOIN transaction_splits ts ON ts.transaction_id = t.id
JOIN participants p ON p.id = ts.participant_id
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
JOIN statements s ON s.id = t.statement_id
LEFT JOIN participants p_owner ON p_owner.id = s.owner_id
WHERE s.owner_id = $1
AND EXISTS (
SELECT 1 FROM transaction_splits ts2
JOIN participants p2 ON p2.id = ts2.participant_id
WHERE ts2.transaction_id = t.id AND p2.name != 'Me'
LEFT JOIN statements s ON s.id = t.statement_id
LEFT JOIN participants p_owner ON p_owner.id = COALESCE(t.owner_id, s.owner_id)
LEFT JOIN transactions src ON src.reconciled_with_id = t.id AND src.statement_id IS NULL
WHERE (
(
COALESCE(t.owner_id, s.owner_id) = $1
AND EXISTS (SELECT 1 FROM transaction_splits ts2 WHERE ts2.transaction_id = t.id AND ts2.participant_id != $1)
) OR (
COALESCE(t.owner_id, s.owner_id) != $1
AND EXISTS (SELECT 1 FROM transaction_splits ts_me WHERE ts_me.transaction_id = t.id AND ts_me.participant_id = $1)
)
GROUP BY t.id, o.category_override, o.merchant_normalized, o.notes, s.bank_name, s.owner_id, p_owner.name
)
AND NOT (t.statement_id IS NULL AND t.reconciled_with_id IS NOT NULL)
${tagClause}
${participantClause}
GROUP BY t.id, o.category_override, o.merchant_normalized, o.notes, s.bank_name, s.owner_id, p_owner.name, src.created_at
ORDER BY t.transaction_date DESC
`, [ownerId]);
`, params);
return rows.map((r) => ({
...r,
+74
View File
@@ -0,0 +1,74 @@
export interface Condition {
field:
| "merchant_normalized"
| "description"
| "category"
| "bank_name"
| "amount"
| "transaction_type"
| "tag";
operator: "contains" | "equals" | "starts_with" | "gt" | "lt" | "not_equals";
value: string;
}
export interface SplitEntry {
participant_id: number;
share_percent: number;
}
export interface Actions {
set_category?: string;
add_tag_ids?: number[];
set_merchant?: string;
apply_split?: SplitEntry[];
}
export interface TxFields {
effective_category: string;
effective_merchant: string;
description: string;
bank_name: string;
amount: number;
transaction_type: string;
tags: { id: number }[];
}
export function evaluateCondition(cond: Condition, tx: TxFields): boolean {
if (cond.field === "amount") {
const numVal = Number(tx.amount);
const numCond = Number(cond.value);
switch (cond.operator) {
case "equals": return numVal === numCond;
case "not_equals": return numVal !== numCond;
case "gt": return numVal > numCond;
case "lt": return numVal < numCond;
default: return false;
}
}
if (cond.field === "tag") {
const tagId = Number(cond.value);
const hasTag = tx.tags.some((t) => t.id === tagId);
return cond.operator === "not_equals" ? !hasTag : hasTag;
}
let fieldVal: string;
switch (cond.field) {
case "merchant_normalized": fieldVal = tx.effective_merchant || ""; break;
case "description": fieldVal = tx.description || ""; break;
case "category": fieldVal = tx.effective_category || ""; break;
case "bank_name": fieldVal = tx.bank_name || ""; break;
case "transaction_type": fieldVal = tx.transaction_type || ""; break;
default: return false;
}
const strVal = fieldVal.toLowerCase();
const strCond = cond.value.toLowerCase();
switch (cond.operator) {
case "contains": return strVal.includes(strCond);
case "equals": return strVal === strCond;
case "starts_with": return strVal.startsWith(strCond);
case "not_equals": return strVal !== strCond;
default: return false;
}
}
+10
View File
@@ -0,0 +1,10 @@
import { defineConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [tsconfigPaths()],
test: {
environment: "node",
include: ["src/__tests__/unit/**/*.test.ts"],
},
});
+24
View File
@@ -0,0 +1,24 @@
import { defineConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";
import { readFileSync, existsSync } from "fs";
import { resolve } from "path";
// Load .env.test into process.env HERE, at config-load time.
// Forked workers inherit the parent process env, so Prisma's singleton
// will see the test DATABASE_URL when db.ts is first imported.
const envTestPath = resolve(__dirname, ".env.test");
if (existsSync(envTestPath)) {
for (const line of readFileSync(envTestPath, "utf-8").split("\n")) {
const m = line.match(/^([^#=]+)=(.*)/);
if (m) process.env[m[1].trim()] = m[2].trim();
}
}
export default defineConfig({
plugins: [tsconfigPaths()],
test: {
environment: "node",
include: ["src/__tests__/integration/**/*.test.ts"],
pool: "forks",
},
});