Add fuel, maintenance, document and reminder tracking
Four features layered onto cars, each following the existing parts/services pattern: a Go handler gated on requireCarAccess, snake_case PocketBase mappers, a Vue form modal, and a tab on CarDetail (now driven by an array rather than repeated markup). Fuel: refills logged with odometer, litres and cost. Consumption is derived on read from the whole history rather than stored, so correcting an old fill re-derives every window it touches with no rows to migrate. Efficiency uses the full-tank method — two consecutive full tanks are the same known level, so the fuel burned between them is exactly what was poured in. Partial fills roll into the window that closes them; a missed-fill flag leaves that window uncomputed rather than reporting an implausibly good figure. Averages in the stats rollup are distance-weighted, so a long motorway run counts for more than a trip across town — which is what actually happened to the fuel. Maintenance: workshop visits and repairs, deliberately separate from service_records. That collection is the routine interval schedule and drives next-service-due; this one is unplanned garage work with a workshop, an invoice and a labour bill, and no bearing on the interval. Documents: insurance, pollution certificates and registration papers. The renewal date is the point of the record, so expiry is assessed live on every read instead of stored and left to go stale. Scans are proxied through the API — PocketBase's collections have no public read rule, so an attachment is never a public URL and car access is re-checked per fetch. Reminders: fire on a date, an odometer reading, or both (whichever comes first). Stored reminders sit alongside read-only ones derived from document expiry and next-service-due, so a renewal date is never typed twice and can never drift from the document it came from. Derived ids are namespaced "auto:" and every write endpoint rejects them. A refill or a completed visit also writes the car's odometer forward, since it is the freshest reading there is — never backwards, so backfilling old history can't rewind the car. Adds fuel_entries, maintenance_entries, car_documents and reminders to the idempotent schema script, plus a file-field builder for attachments. Verified end-to-end against a live PocketBase with a throwaway account: 39 checks covering the efficiency maths, expiry states, the derived reminders, the upload/download round-trip, and that a stranger can reach none of it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
ae6ed4ac1e
commit
e64c89a564
@@ -1,9 +1,12 @@
|
||||
// Idempotent PocketBase schema setup for the Car Control project.
|
||||
//
|
||||
// Creates three collections — cars, service_records, parts — matching the
|
||||
// original "Car Service.xlsx". Access rules are left admin-only (null) on
|
||||
// purpose: every client goes through the API Server, which authenticates as a
|
||||
// superuser, so the database is never exposed directly.
|
||||
// Creates the collections behind the app: cars, service_records and parts (which
|
||||
// match the original "Car Service.xlsx"), the sharing/tenancy tables, and the
|
||||
// fuel, maintenance, document and reminder logs layered on top. Access rules are
|
||||
// left admin-only (null) on purpose: every client goes through the API Server,
|
||||
// which authenticates as a superuser, so the database is never exposed directly
|
||||
// — including document attachments, which are proxied by the API rather than
|
||||
// served as public file URLs.
|
||||
//
|
||||
// Usage (PowerShell):
|
||||
// $env:PB_URL="http://10.2.1.10:8027"
|
||||
@@ -70,6 +73,8 @@ const F = {
|
||||
relation: (name, relTo, required = false, cascadeDelete = true) => ({ name, type: "relation", required, relTo, cascadeDelete }),
|
||||
select: (name, values, required = false) => ({ name, type: "select", required, values }),
|
||||
autodate: (name, onCreate = false, onUpdate = false) => ({ name, type: "autodate", required: false, onCreate, onUpdate }),
|
||||
// Single-file attachment. maxSize is in bytes; mimeTypes [] means "any".
|
||||
file: (name, maxSize, mimeTypes = []) => ({ name, type: "file", required: false, maxSize, mimeTypes }),
|
||||
};
|
||||
|
||||
function renderField(def, format, idByName) {
|
||||
@@ -86,6 +91,11 @@ function renderField(def, format, idByName) {
|
||||
options.values = def.values;
|
||||
options.maxSelect = 1;
|
||||
}
|
||||
if (def.type === "file") {
|
||||
options.maxSelect = 1;
|
||||
options.maxSize = def.maxSize;
|
||||
options.mimeTypes = def.mimeTypes || [];
|
||||
}
|
||||
return { name: def.name, type: def.type, required: def.required, options };
|
||||
}
|
||||
// Modern: options flattened onto the field.
|
||||
@@ -104,6 +114,11 @@ function renderField(def, format, idByName) {
|
||||
field.onCreate = def.onCreate;
|
||||
field.onUpdate = def.onUpdate;
|
||||
}
|
||||
if (def.type === "file") {
|
||||
field.maxSelect = 1;
|
||||
field.maxSize = def.maxSize;
|
||||
field.mimeTypes = def.mimeTypes || [];
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
@@ -238,6 +253,81 @@ const DESIRED = {
|
||||
F.text("part_number"),
|
||||
F.text("category"),
|
||||
],
|
||||
// Fuel refills. Consumption is NOT stored — the API derives it from the whole
|
||||
// history on read (models.ComputeFuelDerived), so correcting an old fill fixes
|
||||
// every figure it affects with no rows to migrate.
|
||||
fuel_entries: [
|
||||
F.relation("car", "cars", true),
|
||||
F.date("date", true),
|
||||
F.number("km"), // odometer at the pump
|
||||
F.number("liters"),
|
||||
F.number("cost"),
|
||||
// Filled to the brim — the reference point efficiency is measured between.
|
||||
F.bool("full_tank"),
|
||||
// A refill happened before this one without being logged, so any window
|
||||
// containing it is left uncomputed rather than reported as implausibly good.
|
||||
F.bool("missed_fill"),
|
||||
F.text("station"),
|
||||
F.text("notes"),
|
||||
],
|
||||
// Workshop visits and repairs. Deliberately separate from service_records:
|
||||
// that collection is the routine interval schedule (and drives next-service
|
||||
// due), this one is unplanned/one-off garage work with a labour bill.
|
||||
maintenance_entries: [
|
||||
F.relation("car", "cars", true),
|
||||
F.date("date", true),
|
||||
F.number("km"),
|
||||
F.select("type", ["repair", "inspection", "bodywork", "tyres", "diagnostics", "recall", "warranty", "other"]),
|
||||
F.select("status", ["scheduled", "in_progress", "completed"]),
|
||||
F.text("workshop"),
|
||||
F.text("location"),
|
||||
F.text("description"),
|
||||
F.text("parts_used"),
|
||||
F.number("labor_cost"),
|
||||
F.number("parts_cost"),
|
||||
F.text("invoice_number"),
|
||||
F.date("warranty_until"),
|
||||
F.text("notes"),
|
||||
],
|
||||
// Insurance, pollution certificates, registration papers … The expiry date is
|
||||
// the point of the record: it drives the renewal status badges and the
|
||||
// auto-derived reminders. Blank expiry = never expires.
|
||||
car_documents: [
|
||||
F.relation("car", "cars", true),
|
||||
F.select("type", ["insurance", "pollution", "registration", "inspection", "roadTax", "warranty", "other"]),
|
||||
F.text("title", true),
|
||||
F.text("provider"),
|
||||
F.text("reference"),
|
||||
F.date("issue_date"),
|
||||
F.date("expiry_date"),
|
||||
F.number("cost"),
|
||||
F.text("notes"),
|
||||
// The scan/PDF. 10MB cap, matching maxDocumentUpload in the API server.
|
||||
// Reached only via the API's own file endpoint, never as a public URL.
|
||||
F.file("file", 10485760, [
|
||||
"application/pdf",
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/webp",
|
||||
"image/heic",
|
||||
]),
|
||||
],
|
||||
// User-set reminders. The API additionally synthesises read-only ones from
|
||||
// document expiry dates and the next service due — those are derived on read
|
||||
// and have no rows here.
|
||||
reminders: [
|
||||
F.relation("car", "cars", true),
|
||||
F.text("title", true),
|
||||
F.select("type", ["maintenance", "document", "service", "inspection", "other"]),
|
||||
F.date("due_date"),
|
||||
F.number("due_km"),
|
||||
// Non-zero => recurring: completing rolls the trigger forward by this much.
|
||||
F.number("repeat_days"),
|
||||
F.number("repeat_km"),
|
||||
F.bool("done"),
|
||||
F.date("done_at"),
|
||||
F.text("notes"),
|
||||
],
|
||||
// Per-car sharing grants. One row = "this user may access this car" at the
|
||||
// given permission. Cascades on both relations so grants disappear when
|
||||
// either the car or the user is deleted. (Owner access is NOT stored here —
|
||||
@@ -276,6 +366,12 @@ const DESIRED = {
|
||||
// unique so the API can rely on PocketBase rejecting a duplicate.
|
||||
const INDEXES = {
|
||||
organizations: ["CREATE UNIQUE INDEX `idx_organizations_name` ON `organizations` (`name`)"],
|
||||
// Every read of these is "…for this car", and the fuel history is walked in
|
||||
// odometer order to build its efficiency windows.
|
||||
fuel_entries: ["CREATE INDEX `idx_fuel_entries_car_km` ON `fuel_entries` (`car`, `km`)"],
|
||||
maintenance_entries: ["CREATE INDEX `idx_maintenance_entries_car_date` ON `maintenance_entries` (`car`, `date`)"],
|
||||
car_documents: ["CREATE INDEX `idx_car_documents_car_expiry` ON `car_documents` (`car`, `expiry_date`)"],
|
||||
reminders: ["CREATE INDEX `idx_reminders_car_due` ON `reminders` (`car`, `due_date`)"],
|
||||
};
|
||||
|
||||
async function main() {
|
||||
@@ -293,7 +389,17 @@ async function main() {
|
||||
// Create in dependency order (organizations before users references it; cars
|
||||
// before its relations; "users" already exists as PocketBase's built-in auth
|
||||
// collection, so it's never created here — only reconciled below).
|
||||
for (const name of ["organizations", "cars", "service_records", "parts", "car_shares"]) {
|
||||
for (const name of [
|
||||
"organizations",
|
||||
"cars",
|
||||
"service_records",
|
||||
"parts",
|
||||
"car_shares",
|
||||
"fuel_entries",
|
||||
"maintenance_entries",
|
||||
"car_documents",
|
||||
"reminders",
|
||||
]) {
|
||||
if (collections.some((c) => c.name === name)) continue;
|
||||
await createCollection(token, name, DESIRED[name], format, idByName);
|
||||
console.log(`✓ ${name} — created`);
|
||||
@@ -305,12 +411,24 @@ async function main() {
|
||||
// Reconcile fields on existing collections (add missing + fix relation options
|
||||
// and select values — this is what grows users.role to include "superadmin"
|
||||
// and adds users.organization on an existing deployment).
|
||||
for (const name of ["organizations", "users", "cars", "service_records", "parts", "car_shares"]) {
|
||||
for (const name of [
|
||||
"organizations",
|
||||
"users",
|
||||
"cars",
|
||||
"service_records",
|
||||
"parts",
|
||||
"car_shares",
|
||||
"fuel_entries",
|
||||
"maintenance_entries",
|
||||
"car_documents",
|
||||
"reminders",
|
||||
]) {
|
||||
await reconcileFields(token, name, DESIRED[name], format, idByName);
|
||||
}
|
||||
|
||||
console.log(
|
||||
"\nDone. Collections ready: organizations, users, cars, service_records, parts, car_shares.",
|
||||
"\nDone. Collections ready: organizations, users, cars, service_records, parts,\n" +
|
||||
"car_shares, fuel_entries, maintenance_entries, car_documents, reminders.",
|
||||
);
|
||||
console.log(
|
||||
"Note: the legacy `sessions` collection is no longer used (auth moved to PocketBase\n" +
|
||||
|
||||
Reference in New Issue
Block a user