mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Implements the last three infra-gated endpoints with Workers-native approaches: - PDF reports via pdf-lib (pure JS; no PDFKit/Puppeteer): monthly, tax-summary, pl-summary, annual. Each gathers the same data as its JSON endpoint and renders a clean A4 report. PDFs are generated on the fly (no storage), so they stay free. - Receipt files in R2 (new [[r2_buckets]] binding): upload validates type/size + a per-profile count cap, stores under the profile id, and serving is scoped to the owner (fixes the old unauthenticated file route). Upload is a PREMIUM feature - a new users.plan column gates it (free accounts get 402) so we don't hold lots of binary data for free; serving/deleting existing receipts is not gated. - Spreadsheet import via SheetJS (xlsx): /import/upload parses xlsx/csv in-memory and returns a preview; the stateful fileId/file-sheet flow is replaced by re-uploading with a sheetName field (stateless). The JSON/CSV execute path was already ported. Adds plan.ts (premium helper + limits) and pdf.ts (report builder). Verified: tsc clean; wrangler deploy --dry-run bundles (1.8 MB / 436 KiB gzip). No deploy; the PLACEHOLDERs (R2 bucket name, D1 id, domain) still need filling before go-live.
341 lines
12 KiB
SQL
341 lines
12 KiB
SQL
-- Finance Manager — D1 initial schema (migration 0001).
|
|
-- Derived from backend/schema.sql (keep the data tables in sync when it changes).
|
|
-- The `users` table is ADAPTED for the Worker's JWT + Google auth and so diverges
|
|
-- from the Express backend's users table on purpose.
|
|
-- D1 is SQLite, so this DDL transfers verbatim (applied via `wrangler d1 migrations apply`).
|
|
|
|
-- Users — Worker auth model: username/password are nullable (Google users have
|
|
-- neither); provider_id holds the Google `sub`; token_version is a JWT revocation
|
|
-- counter bumped on logout / "sign out everywhere".
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT UNIQUE,
|
|
password_hash TEXT,
|
|
email TEXT UNIQUE,
|
|
email_verified INTEGER NOT NULL DEFAULT 0,
|
|
auth_provider TEXT NOT NULL DEFAULT 'password',
|
|
provider_id TEXT,
|
|
token_version INTEGER NOT NULL DEFAULT 1,
|
|
plan TEXT NOT NULL DEFAULT 'free',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_provider
|
|
ON users(auth_provider, provider_id) WHERE provider_id IS NOT NULL;
|
|
|
|
-- Profiles
|
|
CREATE TABLE IF NOT EXISTS profiles (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL UNIQUE,
|
|
user_id INTEGER REFERENCES users(id),
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
|
|
-- Categories
|
|
CREATE TABLE IF NOT EXISTS categories (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
color TEXT NOT NULL DEFAULT '#6b7280',
|
|
icon TEXT NOT NULL DEFAULT 'tag',
|
|
type TEXT NOT NULL DEFAULT 'expense',
|
|
parent_id INTEGER,
|
|
tax_deductible INTEGER DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_categories_profile ON categories(profile_id);
|
|
|
|
-- Transactions
|
|
CREATE TABLE IF NOT EXISTS transactions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
description TEXT NOT NULL DEFAULT '',
|
|
amount REAL NOT NULL,
|
|
date TEXT NOT NULL,
|
|
beneficiary TEXT DEFAULT '',
|
|
payor TEXT DEFAULT '',
|
|
category_id INTEGER,
|
|
currency TEXT NOT NULL DEFAULT 'USD',
|
|
amount_local REAL,
|
|
exchange_rate REAL NOT NULL DEFAULT 1.0,
|
|
type TEXT NOT NULL DEFAULT 'expense',
|
|
notes TEXT DEFAULT '',
|
|
reconciled INTEGER DEFAULT 0,
|
|
reconciled_at TEXT,
|
|
means_of_payment TEXT DEFAULT '',
|
|
receipt_id INTEGER,
|
|
receipt_name TEXT DEFAULT '',
|
|
account_id INTEGER,
|
|
transfer_account_id INTEGER,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_profile ON transactions(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_date ON transactions(date);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_category ON transactions(category_id);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_profile_date ON transactions(profile_id, date);
|
|
CREATE INDEX IF NOT EXISTS idx_transactions_profile_type_date ON transactions(profile_id, type, date);
|
|
|
|
-- Budgets
|
|
CREATE TABLE IF NOT EXISTS budgets (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
category_id INTEGER NOT NULL,
|
|
amount REAL NOT NULL,
|
|
period TEXT NOT NULL DEFAULT 'monthly',
|
|
start_date TEXT NOT NULL,
|
|
end_date TEXT,
|
|
rollover_enabled INTEGER DEFAULT 0,
|
|
rollover_amount REAL DEFAULT 0,
|
|
rollover_used REAL DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_profile ON budgets(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_category ON budgets(category_id);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_start_date ON budgets(start_date);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_profile_date ON budgets(profile_id, start_date);
|
|
|
|
-- Zero Based Budgets
|
|
CREATE TABLE IF NOT EXISTS budgets_zero_based (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
profile_id INTEGER NOT NULL,
|
|
category_id INTEGER NOT NULL,
|
|
amount REAL NOT NULL,
|
|
month TEXT NOT NULL,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
FOREIGN KEY (profile_id) REFERENCES profiles(id),
|
|
FOREIGN KEY (category_id) REFERENCES categories(id)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_zero_based_profile ON budgets_zero_based(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_zero_based_month ON budgets_zero_based(month);
|
|
CREATE INDEX IF NOT EXISTS idx_budgets_zero_based_category ON budgets_zero_based(category_id);
|
|
|
|
-- Savings Goals
|
|
CREATE TABLE IF NOT EXISTS savings_goals (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
target_amount REAL NOT NULL,
|
|
current_amount REAL NOT NULL DEFAULT 0,
|
|
deadline TEXT,
|
|
notes TEXT DEFAULT '',
|
|
category_id INTEGER,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_savings_goals_profile ON savings_goals(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_savings_goals_category ON savings_goals(category_id);
|
|
|
|
-- Retirement Goals
|
|
CREATE TABLE IF NOT EXISTS retirement_goals (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
target_amount REAL NOT NULL,
|
|
current_amount REAL NOT NULL DEFAULT 0,
|
|
deadline TEXT,
|
|
notes TEXT DEFAULT '',
|
|
current_age INTEGER DEFAULT 30,
|
|
retirement_age INTEGER DEFAULT 65,
|
|
monthly_contribution REAL DEFAULT 0,
|
|
expected_return_rate REAL DEFAULT 7,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_retirement_goals_profile ON retirement_goals(profile_id);
|
|
|
|
-- Emergency Fund
|
|
CREATE TABLE IF NOT EXISTS emergency_fund_config (
|
|
id INTEGER PRIMARY KEY,
|
|
monthly_expenses REAL NOT NULL DEFAULT 0,
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_emergency_fund_profile ON emergency_fund_config(profile_id);
|
|
|
|
-- Loans
|
|
CREATE TABLE IF NOT EXISTS loans (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
principal REAL NOT NULL,
|
|
interest_rate REAL NOT NULL DEFAULT 5.0,
|
|
start_date TEXT NOT NULL,
|
|
term_months INTEGER NOT NULL,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_loans_profile ON loans(profile_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS loan_rate_periods (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
loan_id INTEGER NOT NULL,
|
|
rate REAL NOT NULL,
|
|
start_month INTEGER NOT NULL,
|
|
end_month INTEGER
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS loan_prepayments (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
loan_id INTEGER NOT NULL,
|
|
month INTEGER NOT NULL,
|
|
amount REAL NOT NULL,
|
|
note TEXT DEFAULT ''
|
|
);
|
|
|
|
-- Settings
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
key TEXT NOT NULL,
|
|
value TEXT NOT NULL,
|
|
profile_id INTEGER DEFAULT 1,
|
|
PRIMARY KEY (key, profile_id)
|
|
);
|
|
|
|
-- Sessions
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
sid TEXT PRIMARY KEY,
|
|
sess TEXT NOT NULL,
|
|
expire TEXT NOT NULL
|
|
);
|
|
|
|
-- Accounts
|
|
CREATE TABLE IF NOT EXISTS accounts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
bank_name TEXT DEFAULT '',
|
|
type TEXT NOT NULL DEFAULT 'giro',
|
|
currency TEXT NOT NULL DEFAULT 'USD',
|
|
balance REAL NOT NULL DEFAULT 0,
|
|
starting_balance REAL NOT NULL DEFAULT 0,
|
|
starting_date TEXT DEFAULT NULL,
|
|
notes TEXT DEFAULT '',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_accounts_profile ON accounts(profile_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS account_balance_history (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
account_id INTEGER NOT NULL,
|
|
balance REAL NOT NULL,
|
|
recorded_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_balance_history_account ON account_balance_history(account_id);
|
|
CREATE INDEX IF NOT EXISTS idx_balance_history_recorded ON account_balance_history(recorded_at);
|
|
|
|
-- Recurring Transactions
|
|
CREATE TABLE IF NOT EXISTS recurring_transactions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
profile_id INTEGER NOT NULL DEFAULT 1,
|
|
description TEXT NOT NULL DEFAULT '',
|
|
amount REAL NOT NULL,
|
|
type TEXT NOT NULL DEFAULT 'expense',
|
|
category_id INTEGER,
|
|
frequency TEXT NOT NULL DEFAULT 'monthly',
|
|
day_of_month INTEGER,
|
|
next_date TEXT,
|
|
notes TEXT DEFAULT '',
|
|
active INTEGER NOT NULL DEFAULT 1,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_recurring_profile ON recurring_transactions(profile_id);
|
|
|
|
-- Bills
|
|
CREATE TABLE IF NOT EXISTS bills (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
profile_id INTEGER NOT NULL DEFAULT 1,
|
|
name TEXT NOT NULL,
|
|
amount REAL NOT NULL,
|
|
frequency TEXT NOT NULL DEFAULT 'monthly',
|
|
day_of_month INTEGER,
|
|
category_id INTEGER,
|
|
due_date TEXT NOT NULL,
|
|
next_due_date TEXT,
|
|
recurring INTEGER NOT NULL DEFAULT 1,
|
|
is_active INTEGER NOT NULL DEFAULT 1,
|
|
last_paid TEXT,
|
|
last_paid_date TEXT,
|
|
notes TEXT DEFAULT '',
|
|
type TEXT NOT NULL DEFAULT 'bill',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_bills_profile ON bills(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_bills_profile_due_date ON bills(profile_id, due_date);
|
|
|
|
-- Housings
|
|
CREATE TABLE IF NOT EXISTS housings (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
profile_id INTEGER NOT NULL DEFAULT 1,
|
|
name TEXT NOT NULL,
|
|
type TEXT NOT NULL DEFAULT 'other',
|
|
monthly_amount REAL NOT NULL,
|
|
due_date TEXT NOT NULL DEFAULT '',
|
|
autopay INTEGER NOT NULL DEFAULT 0,
|
|
notes TEXT DEFAULT '',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_housings_profile ON housings(profile_id);
|
|
|
|
-- Tags
|
|
CREATE TABLE IF NOT EXISTS tags (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
color TEXT DEFAULT '#6b7280',
|
|
profile_id INTEGER NOT NULL DEFAULT 1,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
UNIQUE(name, profile_id)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_tags_profile ON tags(profile_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS transaction_tags (
|
|
transaction_id INTEGER NOT NULL,
|
|
tag_id INTEGER NOT NULL,
|
|
PRIMARY KEY (transaction_id, tag_id),
|
|
FOREIGN KEY (transaction_id) REFERENCES transactions(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_tx_tags_tx ON transaction_tags(transaction_id);
|
|
CREATE INDEX IF NOT EXISTS idx_tx_tags_tag ON transaction_tags(tag_id);
|
|
|
|
-- Category Mappings
|
|
CREATE TABLE IF NOT EXISTS category_mappings (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
profile_id INTEGER NOT NULL,
|
|
pattern TEXT NOT NULL,
|
|
category_id INTEGER NOT NULL,
|
|
confidence REAL NOT NULL,
|
|
use_count INTEGER DEFAULT 1,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
FOREIGN KEY (profile_id) REFERENCES profiles(id),
|
|
FOREIGN KEY (category_id) REFERENCES categories(id)
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_mappings_profile ON category_mappings(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_mappings_pattern ON category_mappings(pattern);
|
|
|
|
-- Receipts
|
|
CREATE TABLE IF NOT EXISTS receipts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
transaction_id INTEGER UNIQUE,
|
|
filename TEXT NOT NULL,
|
|
original_name TEXT NOT NULL,
|
|
file_type TEXT NOT NULL,
|
|
file_size INTEGER NOT NULL,
|
|
storage_path TEXT NOT NULL,
|
|
uploaded_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1,
|
|
FOREIGN KEY (transaction_id) REFERENCES transactions(id) ON DELETE SET NULL
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_receipts_profile ON receipts(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_receipts_transaction ON receipts(transaction_id);
|
|
|
|
-- Portfolio Holdings
|
|
CREATE TABLE IF NOT EXISTS portfolio_holdings (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
ticker TEXT NOT NULL,
|
|
shares REAL NOT NULL,
|
|
purchase_price REAL NOT NULL,
|
|
purchase_date TEXT NOT NULL,
|
|
notes TEXT DEFAULT '',
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
updated_at TEXT DEFAULT (datetime('now')),
|
|
profile_id INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_portfolio_profile ON portfolio_holdings(profile_id);
|
|
CREATE INDEX IF NOT EXISTS idx_portfolio_ticker ON portfolio_holdings(profile_id, ticker);
|