Initial commit

This commit is contained in:
Michael committed 2026-08-06 18:26:45 +12:00
commit b6014c6ffc
47 files changed
+4329

No files matched your search

+153
View File
@@ -0,0 +1,153 @@
package handlers
import (
"context"
"sort"
"time"
"vlog/internal/models"
)
// reminderWindowDays is how far ahead items are surfaced as "upcoming"
// (overdue items always show regardless of age).
const reminderWindowDays = 30
func today() time.Time {
now := time.Now().UTC()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
}
// statusFor computes overdue/upcoming/future for a due date and/or due
// odometer against today and the car's current odometer reading. Either
// dueDate or dueOdometer (or both) may be nil.
func statusFor(dueDate *time.Time, dueOdometer, currentOdometer *int, today time.Time) string {
overdue, upcoming := false, false
if dueDate != nil {
if dueDate.Before(today) {
overdue = true
} else if !dueDate.After(today.AddDate(0, 0, reminderWindowDays)) {
upcoming = true
}
}
if dueOdometer != nil && currentOdometer != nil {
if *currentOdometer >= *dueOdometer {
overdue = true
} else if *dueOdometer-*currentOdometer <= 1000 {
upcoming = true
}
}
if overdue {
return "overdue"
}
if upcoming {
return "upcoming"
}
return "future"
}
// reminderToView converts a stored Reminder row into the unified view shape.
func reminderToView(r models.Reminder, carRego string, currentOdometer *int, todayDate time.Time) models.ReminderView {
status := "done"
if r.CompletedAt == nil {
status = statusFor(r.DueDate, r.DueOdometer, currentOdometer, todayDate)
}
id := r.ID
return models.ReminderView{
ID: &id, CarID: r.CarID, CarRego: carRego, ReminderType: r.ReminderType,
Label: r.Label, DueDate: r.DueDate, DueOdometer: r.DueOdometer,
Notes: r.Notes, CompletedAt: r.CompletedAt, Status: status,
}
}
// synthesizedReminders returns the rego/WoF/next-service-due items derived
// from a car's own fields — never stored, always computed fresh, so there's
// nothing to keep in sync when the car (or its service history) changes.
// ID is left nil, which is how the frontend knows these aren't editable
// directly — change the car's rego/WoF/service-interval fields instead.
func synthesizedReminders(ctx context.Context, h *Handlers, car *models.Car, todayDate time.Time) []models.ReminderView {
var views []models.ReminderView
if car.RegoExpiry != nil {
views = append(views, models.ReminderView{
CarID: car.ID, CarRego: car.Rego, ReminderType: "rego",
DueDate: car.RegoExpiry, Status: statusFor(car.RegoExpiry, nil, nil, todayDate),
})
}
if car.WofExpiry != nil {
views = append(views, models.ReminderView{
CarID: car.ID, CarRego: car.Rego, ReminderType: "wof",
DueDate: car.WofExpiry, Status: statusFor(car.WofExpiry, nil, nil, todayDate),
})
}
if car.ServiceIntervalKM != nil || car.ServiceIntervalMonths != nil {
var last models.ServiceEntry
err := h.dbGet(ctx, &last, `
SELECT * FROM service_entries
WHERE car_id = ? AND is_service = ?
ORDER BY entry_date DESC, created_at DESC
LIMIT 1
`, car.ID, true)
if err == nil {
var dueDate *time.Time
if car.ServiceIntervalMonths != nil {
d := last.EntryDate.AddDate(0, *car.ServiceIntervalMonths, 0)
dueDate = &d
}
var dueOdometer *int
if car.ServiceIntervalKM != nil && last.Odometer != nil {
o := *last.Odometer + *car.ServiceIntervalKM
dueOdometer = &o
}
if dueDate != nil || dueOdometer != nil {
views = append(views, models.ReminderView{
CarID: car.ID, CarRego: car.Rego, ReminderType: "service",
DueDate: dueDate, DueOdometer: dueOdometer,
Status: statusFor(dueDate, dueOdometer, car.Odometer, todayDate),
})
}
}
}
return views
}
// sortReminderViews orders overdue-first, then by due date (nulls last),
// then by due odometer (nulls last), with completed items always last.
func sortReminderViews(views []models.ReminderView) {
rank := func(v models.ReminderView) int {
switch v.Status {
case "overdue":
return 0
case "upcoming":
return 1
case "future":
return 2
default: // "done"
return 3
}
}
sort.SliceStable(views, func(i, j int) bool {
a, b := views[i], views[j]
if ra, rb := rank(a), rank(b); ra != rb {
return ra < rb
}
switch {
case a.DueDate == nil && b.DueDate != nil:
return false
case a.DueDate != nil && b.DueDate == nil:
return true
case a.DueDate != nil && b.DueDate != nil && !a.DueDate.Equal(*b.DueDate):
return a.DueDate.Before(*b.DueDate)
}
switch {
case a.DueOdometer == nil && b.DueOdometer != nil:
return false
case a.DueOdometer != nil && b.DueOdometer == nil:
return true
case a.DueOdometer != nil && b.DueOdometer != nil:
return *a.DueOdometer < *b.DueOdometer
}
return false
})
}