MainsReady

MainsReady

A web-based management application for UK Gas and Electrical engineers. Generate UK-compliant safety certificates (CP12 Gas Safety Records, EICR, EIC, MEIWC), manage customers and properties, and handle invoicing - with PDF generation and email delivery.

Features

  • CP12 Gas Safety Records - compliant with Gas Safety (Installation and Use) Regulations 1998, Reg 36(3)
  • EICR - compliant with BS 7671:2018+A4:2026 (A3:2024 selectable until withdrawal) and Electrical Safety Standards in the Private Rented Sector (England) Regulations 2020
  • EIC - BS 7671 three-declaration form (Designer/Constructor/Inspector) with Part P
  • MEIWC - Minor works single-declaration BS 7671 certificate
  • Invoicing - line items, VAT, PDF generation, email delivery
  • Customer & Property Management
  • PWA - installable, works offline with service worker caching
  • PDF Generation - fully programmatic, no external dependencies

Requirements

  • Node.js 20+
  • npm 10+

Bare Metal Setup

# 1. Clone and configure
cp .env.example .env
# Edit .env - set JWT_SECRET at minimum (use a long random string)

# 2. Install all dependencies
npm install

# 3. Build the client
npm run build

# 4. Run database migrations
npm run migrate

# 5. Start the server
npm start
# → App available at http://localhost:3000

On first visit, you will be prompted to create your engineer account.

Development Mode

npm run dev
# Runs Fastify (with --watch) + Vite HMR concurrently
# Frontend: http://localhost:5173 (proxies API to :3000)
# Backend:  http://localhost:3000

Docker Setup

# 1. Create .env with at minimum JWT_SECRET
echo "JWT_SECRET=$(openssl rand -base64 32)" > .env

# 2. Build and start
docker compose up --build -d

# 3. View logs
docker compose logs -f

# App available at http://localhost:3000

Data (SQLite database + all PDFs) is persisted in the mainsready_storage named volume.

# Stop
docker compose down

# Stop and remove data
docker compose down -v

Environment Variables

Variable Required Default Description
JWT_SECRET Yes in production - Secret key for JWT signing. The server refuses to boot in production without one of at least 32 characters; in development it falls back to an insecure default and warns loudly.
LOG_LEVEL No info in production, warn in development Fastify logger level (e.g. debug, info, warn, error)
PORT No 3000 Server port
DATABASE_PATH No ./storage/mainsready.db SQLite database path
STORAGE_PATH No ./storage Directory for PDFs and uploads
SMTP_HOST No - SMTP server hostname
SMTP_PORT No 587 SMTP port
SMTP_USER No - SMTP username
SMTP_PASS No - SMTP password
SMTP_FROM No noreply@mainsready.app From address for emails

Media uploads (site photos/videos) and engineer logo uploads are capped separately: MEDIA_MAX_BYTES (200MB, server/src/routes/media.js) and LOGO_MAX_BYTES (5MB, same file) - a logo upload does not inherit the much larger media limit. Neither is currently an environment variable; change the constant in that file if a different cap is needed.

Every certificate type is validated and rendered against a versioned rule registry rather than fixed logic, and every guarantee below is enforced in code and covered by tests - not asserted in this document alone.

  • Every certificate is issued under a named standards ruleset (server/src/certs/rulesets.js), recorded on the record and frozen at issue. Editing a rule definition after the fact cannot retroactively change what an already-issued certificate was checked against.
  • BS 7671 certificates (EICR, EIC, MEIWC) default to BS 7671:2018+A4:2026; BS 7671:2018+A3:2024 remains selectable until it is withdrawn for new certificates on 15 October 2026. A certificate already issued under A3 keeps that ruleset permanently regardless of withdrawal.
  • EICR outcome is derived from the recorded observations and can never be supplied directly by the client or the API caller. Under A4 only C1 (Danger present) and C2 (Potentially dangerous) make a report unsatisfactory; under A3 an FI (Further investigation) observation does too - the two amendments genuinely disagree on this, and the ruleset in force at inspection decides which rule applies.
  • CP12 (Gas Safety Records) enforces all nine Regulation 36(3)(c) particulars of the Gas Safety (Installation and Use) Regulations 1998, including the engineer's signature, and requires defects and remedial action to be stated explicitly rather than left blank.
  • Unsafe gas situations use the current GIUSP categories ID (Immediately Dangerous) and AR (At Risk) only. NCS (Not to Current Standards) was removed from GIUSP in 2015 and is retained in the data model only as an optional annotation - it is never a valid category value.
  • Issued certificates are immutable. A correction is made by superseding the certificate: the original is retained and linked to a new draft, never edited or deleted in place.

CP12 (Gas Safety Records)

  • Compliant with Reg 36(3) and Reg 36A of the Gas Safety (Installation and Use) Regulations 1998, including the reg 36A gas interval and PRS inspection-date logic
  • Regulation 26(9) compliance confirmation required before issue
  • Legal footer on PDF

EICR

  • BS 7671:2018+A3:2024 and +A4:2026, selectable per the standards-currency rules above
  • Observation codes: C1 (Danger present), C2 (Potentially dangerous), C3 (Improvement recommended), FI (Further investigation)
  • All three schedules (Inspection, Circuit Details, Test Results) required before issue
  • Electrical Safety Standards in the Private Rented Sector (England) Regulations 2020 inspection-date logic

EIC

  • BS 7671 three-declaration form (Designer / Constructor / Inspector)
  • Schedule of Circuit Details and Schedule of Test Results required
  • Part P (Building Regulations) notification field

MEIWC

  • Single combined BS 7671 declaration
  • For additions/alterations not extending to new circuits
  • One certificate per circuit modified

Standards Currency

Certificate rules are versioned, not hard-coded: each ruleset is declared in server/src/certs/rulesets.js (id, standard, publication date, withdrawal date) and paired with a definition file under server/src/certs/types/<cert_type>/. Adding support for a new amendment or a new certificate type means adding a ruleset entry and a definition file, not editing existing validation logic in place.

The compliance research behind the rulesets and rules currently in the registry is dated 8 September 2026. Treat it as current for that date; re-verify against the source regulations and BS 7671 amendments before relying on it to add a new cert type or ruleset later.

Operational Notes

  • Rebuilding your database on this branch: the schema was consolidated (four per-type certificate tables replaced by one unified certificates table) and startup now records a content hash for every applied migration. If you have a local mainsready.db from before this change, delete it before starting - migrations will recreate the correct schema from scratch. If a database instead reports a migration hash mismatch on startup, do not attempt to force it past the check: that check exists specifically to stop a rewritten migration from silently skipping and leaving the schema incomplete.
  • Orphaned PDFs: the certificate PDF storage path was renamed from a per-type gas-certs directory to the cert_type-keyed layout under storage/pdfs/. Old CP12 PDFs left in storage/pdfs/gas-certs/ are stale and unreferenced by any certificate row; nothing reads from that directory anymore, and any PDF still needed regenerates on demand under the new path. It is safe to delete, but nothing does so automatically.

Phase 2 Prerequisites

The frontend has not yet been updated to match the corrected server contract from this phase, and Phase 2 (the frontend rewrite) must start from it rather than the pre-overhaul assumptions still in the client today:

  • client/src/pages/gas-certs/GasCertDetail.js, client/src/components/MediaGallery.js and client/src/api.js still reference the certificate type as 'gas' and the field names cert_type/cert_id from the old per-type tables. The server now uses 'cp12' and the unified certificates table (see server/src/routes/certificates.js); the client needs updating to match before it will work against this branch.
  • CSRF protection (spec §6) was deliberately deferred to Phase 2. Cookie auth with sameSite: 'lax' blocks the cross-site form POST that matters most today, and adding CSRF tokens changes the client request contract, so it is scoped together with the frontend rewrite rather than bolted on separately.

Tech Stack

  • Backend: Node.js (ESM) + Fastify 4
  • Database: SQLite via better-sqlite3
  • PDF: pdf-lib (pure JS, no native deps)
  • Frontend: Vanilla JS + Tailwind CSS (via Vite)
  • PWA: vite-plugin-pwa + Workbox
  • Auth: bcryptjs + @fastify/jwt (httpOnly cookie)
S
Description
No description provided
Readme
2.3 MiB
0 Stars 1 Watchers 0 Forks
Languages
TypeScript 59.4%
JavaScript 37.6%
HTML 2.3%
CSS 0.5%
Dockerfile 0.2%