Skip to content
incialPublic

Repository files navigation

Bluebill

GST invoicing for Indian businesses, built with Next.js 16 (App Router), TypeScript and PostgreSQL. Create invoices, share a payment link with a UPI QR, see what is overdue, and keep GST workings ready. Sign-in is Google only; there is an admin area for running the product.

Quick start

Requires Node 22.13 or newer (24 LTS recommended). No database server is needed to try it: without DATABASE_URL the app uses an embedded PostgreSQL (PGlite) stored in ~/.bluebill/pglite (outside the project on purpose, so the dev server does not recompile whenever the database writes).

npm install
cp .env.example .env.local        # then set GOOGLE_CLIENT_ID and ADMIN_EMAILS
npm run dev                       # http://localhost:3000
Command What it does
npm run dev / build / start Development server, production build, production server
npm run lint · typecheck · test ESLint, tsc --noEmit, Vitest (152 tests)
npm run check All four of the above plus the build: the gate before a release
npm run db:migrate Apply pending migrations to the configured database
npm run db:seed Load demo data (a business with invoices in every state, plus a few other accounts)

Google sign-in

  1. In Google Cloud Console → Credentials create an OAuth client ID of type Web application.
  2. Add Authorized JavaScript origins: http://localhost and http://localhost:3000 for local use, plus your real address once deployed. No redirect URI is needed.
  3. Put the Client ID in GOOGLE_CLIENT_ID. While the consent screen is in Testing, only listed test users can sign in; publish it for everyone.

Admin

Emails in ADMIN_EMAILS are always administrators. Open /login?admin=1 (or the Admin tab once signed in) to see every account with its invoice totals, a leaderboard of the best accounts this month, recent activity, and buttons to make someone an admin or suspend them (suspending signs them out at once). Administrators cannot edit their own account, and ADMIN_EMAILS accounts cannot be demoted from the dashboard.

Development sign-in without Google

ALLOW_DEV_LOGIN=1 enables GET /api/dev/login?email=you@example.com outside production. It is ignored (404) when NODE_ENV=production, whatever the variable says, and a test asserts that.

Project layout

src/
  app/                    Routes only: pages, layouts, loading/error states, and thin API handlers
    (app)/                Signed-in area (layout enforces sign-in): dashboard, invoices, customers,
                          expenses, reports, gst, settings, admin
    api/                  30 route handlers (see below), each a few lines
    pay/[token]/          Public payment page
    login/  page.tsx      Sign-in and landing
  components/
    ui/                   Reusable primitives: Button, Card, StatCard, StatusBadge, Tabs, Pagination, Field, Alert
    layout/ invoices/ customers/ expenses/ settings/ gst/ admin/ auth/ charts/ public/
  server/                 Everything that runs only on the server
    http/                 route() wrapper (auth, CSRF, rate limit, validation, errors), error types, limiter
    validation/           Zod schemas shared by API routes and pages (one definition of every input)
    services/             Business rules and transactions (invoices, analytics, reports, leaderboards, admin, ...)
    repositories/         All SQL, one file per area; nothing else talks to the database
    db/                   Driver abstraction (pg + PGlite), migrations, connection
    auth/                 Google token verification, sessions, current-user helpers
  lib/                    Pure, framework-free code shared by server and browser:
                          money, invoice math, dates, number-to-words, CSV
  types/                  Types shared by API and UI
tests/                    unit · db · services · api · perf
scripts/                  migrate, seed-demo

Dependency direction is one-way: app → services → repositories → db. Pages call services directly (no HTTP hop to themselves); the same services back the JSON API. Client components only do interaction: they call the API, then router.refresh() so what is shown always comes from the server.

API

All endpoints return JSON ({ error: { code, message, details? } } on failure). Money is integer paise; tax rates are basis points (1800 = 18%). Writes require Content-Type: application/json and a same-origin Origin.

Area Endpoints
Auth POST /api/auth/google · POST /api/auth/logout · GET /api/auth/me
Invoices GET/POST /api/invoices · GET/PUT/DELETE /api/invoices/:id · POST …/:id/status · …/duplicate · …/generate-next
Customers GET/POST /api/customers · GET/PUT/DELETE /api/customers/:id
Expenses GET/POST /api/expenses · PUT/DELETE /api/expenses/:id
Business GET/PUT /api/business · PUT /api/business/filings
Calculations POST /api/calculations/invoice (totals, GST split, amount in words)
Analytics GET /api/analytics/dashboard · GET /api/analytics/gst?month=
Reports GET /api/reports · GET /api/reports/export (CSV) · GET /api/reports/gstr1?month= (CSV)
Leaderboards GET /api/leaderboards/customers?metric=collected|billed|fastest · GET /api/admin/leaderboards/accounts
Public GET /api/public/invoices/:token · POST …/:token/report-paid
Admin GET /api/admin/stats · /users · /users/:id · /activity · PATCH /users/:id
Ops GET /api/health (checks the database)

List endpoints page in SQL (page, pageSize ≤ 100) and return the exact total.

Database

PostgreSQL 15+ (or embedded PGlite, which is PostgreSQL 17). Schema: src/server/db/migrations/001_init.ts.

users → sessions · businesses → business_notes · customers · invoices → invoice_items · expenses · gst_filings · audit_log.

  • Money is bigint paise, never floating point; line quantities are numeric(12,3).
  • Constraints do the policing, not only the code: total = subtotal + tax_amount, due_date >= issue_date, (status = 'paid') = (paid_on IS NOT NULL), valid GSTIN pattern, positive quantities, enumerated statuses, lower-case unique emails.
  • Tenant safety in the schema: invoices reference customers through a composite foreign key (customer_id, user_id), so an invoice can never point at another account's customer. Deleting a user cascades to everything they own; deleting a customer keeps its invoices (each stores a snapshot of the customer's details).
  • Indexes match the queries: (user_id, issue_date DESC, id DESC) for lists and reports, (user_id, status, due_date) for overdue, a partial (user_id, paid_on) WHERE status = 'paid' for income, a partial index for recurring, unique (user_id, number) and unique public_token. A test asserts the planner uses them on 30,000 invoices.
  • Migrations are numbered and applied once each in a transaction, guarded by an advisory lock so several instances can start together.

Security

Google ID tokens are verified on the server (RS256 against Google's keys, issuer, audience, expiry, verified email). Sessions are random 256-bit tokens in an HttpOnly; SameSite=Lax cookie (Secure in production) and only their SHA-256 is stored. Every query is scoped by the signed-in user's id. Writes check Origin; all input goes through Zod; sign-in and public endpoints are rate limited; payment links use random 128-bit tokens and show one invoice and nothing else; CSV export neutralises spreadsheet formulas; unexpected errors return a generic 500 with no internals.

Deploying

npm run build && npm start        # NODE_ENV=production, HTTPS required for the Secure cookie

Set GOOGLE_CLIENT_ID, ADMIN_EMAILS and DATABASE_URL (PostgreSQL 15+); set TRUST_PROXY=1 behind a proxy. /api/health is the health check. Migrations run automatically on first connection (or npm run db:migrate). Without DATABASE_URL the embedded database works on a single server with a persistent disk (set DATA_DIR to a persistent disk and back it up).

Known limits

  • The rate limiter is in memory, so it protects one process; use an edge/Redis limiter when running several instances.
  • Admins can see every account's invoices; say so in your privacy policy.
  • Payments are UPI links: Bluebill does not see money arrive, and "I have paid" is the customer's word.
  • Not ported from the earlier prototype: the one-sentence/voice quick draft and seven of the ten invoice styles (three remain: standard, compact, receipt).
  • Real Google sign-in needs your Client ID and could not be exercised end to end here; the verifier is covered by tests using real RSA signatures.

Releases

Packages

Contributors

Languages