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
@@ -80,6 +80,100 @@ function kmSignal(currentKm, nextServiceKm) {
|
||||
// serviceStatus combines the date- and km-based signals, returning the worse of
|
||||
// the two for the badge. `latest` is the most recent service record (with
|
||||
// nextServiceDate/nextServiceKm); `car` carries the current odometer.
|
||||
// formatLiters / formatMoney / formatConsumption render the fuel figures. The
|
||||
// server sends null for anything it could not derive (a window with a missed
|
||||
// fill, a first-ever tank), which reads as "—" rather than a misleading zero.
|
||||
export function formatLiters(value) {
|
||||
if (value == null || value === "") return "—";
|
||||
return Number(value).toFixed(2) + " L";
|
||||
}
|
||||
|
||||
// Amounts are unit-less on purpose: the project stores plain numbers and has no
|
||||
// currency setting, so imposing a symbol here would be a guess.
|
||||
export function formatMoney(value) {
|
||||
if (value == null || value === "") return "—";
|
||||
return Number(value).toLocaleString(prefs.locale || undefined, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
}
|
||||
|
||||
// One decimal: the interesting differences between tanks live in tenths, and
|
||||
// rounding to whole litres collapses a best of 6.8 and a worst of 7.0 into the
|
||||
// same number.
|
||||
export function formatConsumption(value) {
|
||||
if (value == null) return "—";
|
||||
return Number(value).toFixed(1) + " L/100km";
|
||||
}
|
||||
|
||||
export function formatKmPerLiter(value) {
|
||||
if (value == null) return "—";
|
||||
return Number(value).toFixed(2) + " km/L";
|
||||
}
|
||||
|
||||
// Document renewal badge, driven by the server's expiry assessment so the client
|
||||
// never re-derives the date maths.
|
||||
const EXPIRY_STYLE = {
|
||||
no_expiry: "dh-badge dh-badge-neutral",
|
||||
valid: "dh-badge dh-badge-success",
|
||||
expiring_soon: "dh-badge dh-badge-warning",
|
||||
expired: "dh-badge dh-badge-danger",
|
||||
};
|
||||
|
||||
export function expiryStatus(doc) {
|
||||
const state = doc?.expiry?.state || "no_expiry";
|
||||
const days = doc?.expiry?.daysUntilExpiry;
|
||||
let label;
|
||||
switch (state) {
|
||||
case "expired":
|
||||
label = `Expired ${Math.abs(days)}d ago`;
|
||||
break;
|
||||
case "expiring_soon":
|
||||
label = days === 0 ? "Expires today" : `Renew in ${days}d`;
|
||||
break;
|
||||
case "valid":
|
||||
label = `Valid · ${days}d`;
|
||||
break;
|
||||
default:
|
||||
label = "No expiry";
|
||||
}
|
||||
return { key: state, label, classes: EXPIRY_STYLE[state] || EXPIRY_STYLE.no_expiry };
|
||||
}
|
||||
|
||||
// Reminder badge. The server has already picked the worse of the date and
|
||||
// odometer signals; this only chooses the wording, preferring whichever trigger
|
||||
// is actually driving the status.
|
||||
const REMINDER_STYLE = {
|
||||
done: "dh-badge dh-badge-neutral",
|
||||
no_trigger: "dh-badge dh-badge-neutral",
|
||||
upcoming: "dh-badge dh-badge-success",
|
||||
due_soon: "dh-badge dh-badge-warning",
|
||||
overdue: "dh-badge dh-badge-danger",
|
||||
};
|
||||
|
||||
export function reminderStatus(rem) {
|
||||
const state = rem?.status || "no_trigger";
|
||||
const days = rem?.daysLeft;
|
||||
const km = rem?.kmLeft;
|
||||
|
||||
let label;
|
||||
if (state === "done") label = "Done";
|
||||
else if (state === "no_trigger") label = "No trigger";
|
||||
else if (state === "overdue") {
|
||||
const parts = [];
|
||||
if (days != null && days < 0) parts.push(`${Math.abs(days)}d`);
|
||||
if (km != null && km < 0) parts.push(`${Math.abs(km).toLocaleString()} km`);
|
||||
label = parts.length ? `Overdue ${parts.join(" · ")}` : "Overdue";
|
||||
} else {
|
||||
// Lead with the trigger that is closest to firing.
|
||||
const parts = [];
|
||||
if (days != null && days >= 0) parts.push(days === 0 ? "today" : `${days}d`);
|
||||
if (km != null && km >= 0) parts.push(`${km.toLocaleString()} km`);
|
||||
label = parts.length ? `Due in ${parts.join(" · ")}` : "Upcoming";
|
||||
}
|
||||
return { key: state, label, classes: REMINDER_STYLE[state] || REMINDER_STYLE.no_trigger };
|
||||
}
|
||||
|
||||
export function serviceStatus(latest, car = null) {
|
||||
const date = dateSignal(latest?.nextServiceDate);
|
||||
const km = kmSignal(car?.currentKm, latest?.nextServiceKm);
|
||||
|
||||
Reference in New Issue
Block a user