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
@@ -190,3 +190,238 @@ func partPayload(p models.Part) map[string]any {
|
||||
"category": p.Category,
|
||||
}
|
||||
}
|
||||
|
||||
// parsePBDatePtr is parsePBDate for optional dates: a blank or unparseable
|
||||
// value yields nil rather than the zero time, so "no expiry" stays
|
||||
// distinguishable from "expired in year zero".
|
||||
func parsePBDatePtr(s string) *time.Time {
|
||||
t := parsePBDate(s)
|
||||
if t.IsZero() {
|
||||
return nil
|
||||
}
|
||||
return &t
|
||||
}
|
||||
|
||||
// formatPBDatePtr is formatPBDate for optional dates; nil writes an empty value,
|
||||
// which is how PocketBase stores "unset".
|
||||
func formatPBDatePtr(t *time.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return formatPBDate(*t)
|
||||
}
|
||||
|
||||
// --- fuel entries ---
|
||||
|
||||
type fuelRecord struct {
|
||||
ID string `json:"id"`
|
||||
Car string `json:"car"`
|
||||
Date string `json:"date"`
|
||||
Km int `json:"km"`
|
||||
Liters float64 `json:"liters"`
|
||||
Cost float64 `json:"cost"`
|
||||
FullTank bool `json:"full_tank"`
|
||||
MissedFill bool `json:"missed_fill"`
|
||||
Station string `json:"station"`
|
||||
Notes string `json:"notes"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
}
|
||||
|
||||
func (rec fuelRecord) toModel() models.FuelEntry {
|
||||
return models.FuelEntry{
|
||||
ID: rec.ID,
|
||||
Car: rec.Car,
|
||||
Date: parsePBDate(rec.Date),
|
||||
Km: rec.Km,
|
||||
Liters: rec.Liters,
|
||||
Cost: rec.Cost,
|
||||
FullTank: rec.FullTank,
|
||||
MissedFill: rec.MissedFill,
|
||||
Station: rec.Station,
|
||||
Notes: rec.Notes,
|
||||
Created: rec.Created,
|
||||
Updated: rec.Updated,
|
||||
}
|
||||
}
|
||||
|
||||
func fuelPayload(f models.FuelEntry) map[string]any {
|
||||
return map[string]any{
|
||||
"car": f.Car,
|
||||
"date": formatPBDate(f.Date),
|
||||
"km": f.Km,
|
||||
"liters": f.Liters,
|
||||
"cost": f.Cost,
|
||||
"full_tank": f.FullTank,
|
||||
"missed_fill": f.MissedFill,
|
||||
"station": f.Station,
|
||||
"notes": f.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
// --- maintenance entries ---
|
||||
|
||||
type maintenanceRecord struct {
|
||||
ID string `json:"id"`
|
||||
Car string `json:"car"`
|
||||
Date string `json:"date"`
|
||||
Km int `json:"km"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Workshop string `json:"workshop"`
|
||||
Location string `json:"location"`
|
||||
Description string `json:"description"`
|
||||
PartsUsed string `json:"parts_used"`
|
||||
LaborCost float64 `json:"labor_cost"`
|
||||
PartsCost float64 `json:"parts_cost"`
|
||||
InvoiceNumber string `json:"invoice_number"`
|
||||
WarrantyUntil string `json:"warranty_until"`
|
||||
Notes string `json:"notes"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
}
|
||||
|
||||
func (rec maintenanceRecord) toModel() models.MaintenanceEntry {
|
||||
return models.MaintenanceEntry{
|
||||
ID: rec.ID,
|
||||
Car: rec.Car,
|
||||
Date: parsePBDate(rec.Date),
|
||||
Km: rec.Km,
|
||||
Type: rec.Type,
|
||||
Status: rec.Status,
|
||||
Workshop: rec.Workshop,
|
||||
Location: rec.Location,
|
||||
Description: rec.Description,
|
||||
PartsUsed: rec.PartsUsed,
|
||||
LaborCost: rec.LaborCost,
|
||||
PartsCost: rec.PartsCost,
|
||||
InvoiceNumber: rec.InvoiceNumber,
|
||||
WarrantyUntil: parsePBDatePtr(rec.WarrantyUntil),
|
||||
Notes: rec.Notes,
|
||||
Created: rec.Created,
|
||||
Updated: rec.Updated,
|
||||
}
|
||||
}
|
||||
|
||||
func maintenancePayload(m models.MaintenanceEntry) map[string]any {
|
||||
return map[string]any{
|
||||
"car": m.Car,
|
||||
"date": formatPBDate(m.Date),
|
||||
"km": m.Km,
|
||||
"type": m.Type,
|
||||
"status": m.Status,
|
||||
"workshop": m.Workshop,
|
||||
"location": m.Location,
|
||||
"description": m.Description,
|
||||
"parts_used": m.PartsUsed,
|
||||
"labor_cost": m.LaborCost,
|
||||
"parts_cost": m.PartsCost,
|
||||
"invoice_number": m.InvoiceNumber,
|
||||
"warranty_until": formatPBDatePtr(m.WarrantyUntil),
|
||||
"notes": m.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
// --- car documents ---
|
||||
|
||||
type documentRecord struct {
|
||||
ID string `json:"id"`
|
||||
Car string `json:"car"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Provider string `json:"provider"`
|
||||
Reference string `json:"reference"`
|
||||
IssueDate string `json:"issue_date"`
|
||||
ExpiryDate string `json:"expiry_date"`
|
||||
Cost float64 `json:"cost"`
|
||||
Notes string `json:"notes"`
|
||||
File string `json:"file"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
}
|
||||
|
||||
func (rec documentRecord) toModel() models.CarDocument {
|
||||
return models.CarDocument{
|
||||
ID: rec.ID,
|
||||
Car: rec.Car,
|
||||
Type: rec.Type,
|
||||
Title: rec.Title,
|
||||
Provider: rec.Provider,
|
||||
Reference: rec.Reference,
|
||||
IssueDate: parsePBDatePtr(rec.IssueDate),
|
||||
ExpiryDate: parsePBDatePtr(rec.ExpiryDate),
|
||||
Cost: rec.Cost,
|
||||
Notes: rec.Notes,
|
||||
FileName: rec.File,
|
||||
HasFile: rec.File != "",
|
||||
Created: rec.Created,
|
||||
Updated: rec.Updated,
|
||||
}
|
||||
}
|
||||
|
||||
// documentPayload omits the file field: attachments move over multipart, never
|
||||
// as JSON, so a metadata write must not blank an existing upload.
|
||||
func documentPayload(d models.CarDocument) map[string]any {
|
||||
return map[string]any{
|
||||
"car": d.Car,
|
||||
"type": d.Type,
|
||||
"title": d.Title,
|
||||
"provider": d.Provider,
|
||||
"reference": d.Reference,
|
||||
"issue_date": formatPBDatePtr(d.IssueDate),
|
||||
"expiry_date": formatPBDatePtr(d.ExpiryDate),
|
||||
"cost": d.Cost,
|
||||
"notes": d.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
// --- reminders ---
|
||||
|
||||
type reminderRecord struct {
|
||||
ID string `json:"id"`
|
||||
Car string `json:"car"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
DueDate string `json:"due_date"`
|
||||
DueKm int `json:"due_km"`
|
||||
RepeatDays int `json:"repeat_days"`
|
||||
RepeatKm int `json:"repeat_km"`
|
||||
Done bool `json:"done"`
|
||||
DoneAt string `json:"done_at"`
|
||||
Notes string `json:"notes"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
}
|
||||
|
||||
func (rec reminderRecord) toModel() models.Reminder {
|
||||
return models.Reminder{
|
||||
ID: rec.ID,
|
||||
Car: rec.Car,
|
||||
Title: rec.Title,
|
||||
Type: rec.Type,
|
||||
DueDate: parsePBDatePtr(rec.DueDate),
|
||||
DueKm: rec.DueKm,
|
||||
RepeatDays: rec.RepeatDays,
|
||||
RepeatKm: rec.RepeatKm,
|
||||
Done: rec.Done,
|
||||
DoneAt: parsePBDatePtr(rec.DoneAt),
|
||||
Notes: rec.Notes,
|
||||
Created: rec.Created,
|
||||
Updated: rec.Updated,
|
||||
}
|
||||
}
|
||||
|
||||
func reminderPayload(r models.Reminder) map[string]any {
|
||||
return map[string]any{
|
||||
"car": r.Car,
|
||||
"title": r.Title,
|
||||
"type": r.Type,
|
||||
"due_date": formatPBDatePtr(r.DueDate),
|
||||
"due_km": r.DueKm,
|
||||
"repeat_days": r.RepeatDays,
|
||||
"repeat_km": r.RepeatKm,
|
||||
"done": r.Done,
|
||||
"done_at": formatPBDatePtr(r.DoneAt),
|
||||
"notes": r.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user