Phase 5: per-child dashboard (overdue first), item detail, delete

- internal/dashboard: classify items into overdue / due today / due this week /
  no due date / messages; render the plain-text brief (FR-6, FR-7, FR-9).
- internal/store: ItemsForChild, ItemByID, DeleteItemByID, ScanSummaryForChild
  and the stored-brief CRUD helpers (FR-10).
- internal/web: routes /?child=, /items/{id}, POST /items/{id}/delete,
  POST /summaries/build, POST /summaries/{id}/delete, plus a per-child scan
  banner (FR-12) and a phone-first server-rendered template.
- Calendar events are never "overdue": a past lesson is history, not work.
- cmd/school brief build|list; scripts/screenshot-phase5.mjs for phone evidence.
- Tests: bucket classification, stored-brief round-trip, and HTTP routes
  including the NO SCAN / SCAN FAILED / healthy banner behaviour.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-10-15 17:50:29 +11:00
parent 207a4978a9
commit 37d82b1a0a
16 changed files with 2223 additions and 65 deletions

View File

@@ -0,0 +1,240 @@
// Package dashboard turns stored items into the per-child view required by
// AGENTS.md §4 (FR-6, FR-7) and §5: overdue first, then due today, due this
// week, then messages. It holds no state; the same code feeds the web
// dashboard and the stored draft brief (FR-9/FR-10).
package dashboard
import (
"fmt"
"sort"
"strings"
"time"
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/store"
)
// Buckets is one child's items, split into the sections the dashboard shows.
// Later and NoDue are counted but not given a heading of their own unless they
// carry work, so the page stays brief (AGENTS.md §10 rule 6).
type Buckets struct {
Overdue []store.Item
DueToday []store.Item
DueWeek []store.Item
NoDue []store.Item
Messages []store.Item
// Later is work due after this week; Done is submitted work. Both are kept
// so the summary can count them and nothing is silently dropped.
Later []store.Item
Done []store.Item
}
// Actionable is the number of task items that still need doing.
func (b Buckets) Actionable() int {
return len(b.Overdue) + len(b.DueToday) + len(b.DueWeek) + len(b.NoDue)
}
// Empty reports whether the child has nothing to show at all.
func (b Buckets) Empty() bool {
return b.Actionable() == 0 && len(b.Messages) == 0
}
// Headline is the one-line summary used at the top of a child tab and in the
// brief.
func (b Buckets) Headline() string {
return fmt.Sprintf("%d overdue · %d due today · %d this week · %d message(s)",
len(b.Overdue), len(b.DueToday), len(b.DueWeek), len(b.Messages))
}
// Classify splits items into buckets for one child.
//
// A task counts as overdue when Compass says so ("Status: Overdue" or
// "Submitted: Overdue"), or when it is not submitted and its due date is
// before today. Submitted work (On Time / Received Late) is not "due", so it
// is kept out of the due lists (the parser stores every historical task; the
// dashboard shows what still needs doing).
func Classify(items []store.Item, now time.Time, loc *time.Location) Buckets {
if loc == nil {
loc = time.UTC
}
startToday := startOfDay(now, loc)
endToday := startToday.AddDate(0, 0, 1)
endWeek := startToday.AddDate(0, 0, 7)
var b Buckets
for _, it := range items {
switch it.Type {
case "message":
b.Messages = append(b.Messages, it)
continue
case "task":
// handled below
case "event":
// A lesson or event is never "overdue": a past lesson is history, not
// work to do. Only today and the coming week are shown.
if it.DueAt == nil {
continue
}
due := it.DueAt.In(loc)
switch {
case !due.Before(startToday) && due.Before(endToday):
b.DueToday = append(b.DueToday, it)
case !due.Before(endToday) && due.Before(endWeek):
b.DueWeek = append(b.DueWeek, it)
case !due.Before(endWeek):
b.Later = append(b.Later, it)
}
continue
default:
continue
}
status := Status(it.Body)
if isSubmitted(status) {
b.Done = append(b.Done, it)
continue
}
if it.DueAt == nil {
b.NoDue = append(b.NoDue, it)
continue
}
due := it.DueAt.In(loc)
switch {
case status == "overdue":
b.Overdue = append(b.Overdue, it)
case due.Before(startToday):
b.Overdue = append(b.Overdue, it)
case due.Before(endToday):
b.DueToday = append(b.DueToday, it)
case due.Before(endWeek):
b.DueWeek = append(b.DueWeek, it)
default:
b.Later = append(b.Later, it)
}
}
for _, s := range [][]store.Item{b.Overdue, b.DueToday, b.DueWeek, b.NoDue, b.Later, b.Done} {
sort.SliceStable(s, func(i, j int) bool { return less(s[i], s[j]) })
}
sort.SliceStable(b.Messages, func(i, j int) bool {
ti, tj := timeOf(b.Messages[i]), timeOf(b.Messages[j])
return ti.After(tj)
})
return b
}
// less orders actionable items by due date, then title. A nil due date sorts last.
func less(a, b store.Item) bool {
switch {
case a.DueAt == nil && b.DueAt == nil:
return a.Title < b.Title
case a.DueAt == nil:
return false
case b.DueAt == nil:
return true
case a.DueAt.Equal(*b.DueAt):
return a.Title < b.Title
default:
return a.DueAt.Before(*b.DueAt)
}
}
func timeOf(it store.Item) time.Time {
if it.DueAt == nil {
return time.Time{}
}
return *it.DueAt
}
// Status reads the status line the parser writes into a task body: the JSON
// parser writes "Status: Overdue", the HTML grid writes "Submitted: Overdue".
// It returns a lower-case label, or "" when the body carries no status.
func Status(body string) string {
for _, line := range strings.Split(body, "\n") {
line = strings.TrimSpace(line)
for _, prefix := range []string{"Status:", "Submitted:"} {
if strings.HasPrefix(line, prefix) {
v := strings.TrimSpace(strings.TrimPrefix(line, prefix))
v = strings.TrimRight(v, ".")
return strings.ToLower(v)
}
}
}
return ""
}
// submittedStatuses are the labels that mean the work no longer needs doing.
var submittedStatuses = map[string]bool{
"on time": true,
"received late": true,
"submitted": true,
"completed": true,
}
func isSubmitted(status string) bool { return submittedStatuses[status] }
func startOfDay(t time.Time, loc *time.Location) time.Time {
t = t.In(loc)
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
}
// Render writes one child's brief as plain text: overdue first, then due today,
// due this week, no due date and messages (FR-9, FR-14, AGENTS.md §4 rule 6).
func Render(child string, b Buckets, loc *time.Location) string {
if loc == nil {
loc = time.UTC
}
var sb strings.Builder
fmt.Fprintf(&sb, "%s — %s.\n", child, b.Headline())
writeSection(&sb, "OVERDUE", b.Overdue, loc)
writeSection(&sb, "DUE TODAY", b.DueToday, loc)
writeSection(&sb, "DUE THIS WEEK", b.DueWeek, loc)
writeSection(&sb, "NO DUE DATE", b.NoDue, loc)
writeMessages(&sb, b.Messages, loc)
return strings.TrimRight(sb.String(), "\n") + "\n"
}
func writeSection(sb *strings.Builder, title string, items []store.Item, loc *time.Location) {
if len(items) == 0 {
return
}
fmt.Fprintf(sb, "\n%s\n", title)
for _, it := range items {
fmt.Fprintf(sb, "- %s\n", line(it, loc))
}
}
func writeMessages(sb *strings.Builder, items []store.Item, loc *time.Location) {
if len(items) == 0 {
return
}
sb.WriteString("\nMESSAGES\n")
for _, it := range items {
when := ""
if it.DueAt != nil {
when = " (" + it.DueAt.In(loc).Format("Mon 02 Jan 15:04") + ")"
}
fmt.Fprintf(sb, "- %s%s\n", it.Title, when)
}
}
// line renders one actionable item for the brief.
func line(it store.Item, loc *time.Location) string {
subject := strings.TrimSpace(it.Subject)
title := strings.TrimSpace(it.Title)
switch {
case subject != "" && subject != title:
title = subject + ": " + title
}
due := ""
if it.DueAt != nil {
due = " (due " + it.DueAt.In(loc).Format("Mon 02 Jan 15:04") + ")"
}
status := Status(it.Body)
mark := ""
if status != "" && status != "pending" {
mark = " [" + status + "]"
}
return title + due + mark
}

View File

@@ -0,0 +1,169 @@
package dashboard
import (
"strings"
"testing"
"time"
_ "time/tzdata"
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/store"
)
// sydney is the family timezone used in the tests.
func sydney(t *testing.T) *time.Location {
t.Helper()
loc, err := time.LoadLocation("Australia/Sydney")
if err != nil {
t.Fatalf("load location: %v", err)
}
return loc
}
func due(t *testing.T, loc *time.Location, s string) *time.Time {
t.Helper()
v, err := time.ParseInLocation("2006-01-02 15:04", s, loc)
if err != nil {
t.Fatalf("parse %q: %v", s, err)
}
return &v
}
// TestClassifyBuckets proves FR-6/FR-7: overdue first, then due today, due this
// week, no due date and messages, and that submitted work is not "due".
func TestClassifyBuckets(t *testing.T) {
loc := sydney(t)
now := time.Date(2026, 10, 15, 9, 0, 0, 0, loc)
items := []store.Item{
{Type: "task", Title: "Old essay", DueAt: due(t, loc, "2026-10-10 09:00"), Body: "Status: Overdue"},
{Type: "task", Title: "Past pending", DueAt: due(t, loc, "2026-10-14 09:00"), Body: "Submitted: Pending"},
{Type: "task", Title: "Today task", DueAt: due(t, loc, "2026-10-15 15:00"), Body: "Status: Pending"},
{Type: "event", Title: "Today lesson", DueAt: due(t, loc, "2026-10-15 10:00")},
{Type: "task", Title: "Friday task", DueAt: due(t, loc, "2026-10-17 09:00"), Body: "Status: Pending"},
{Type: "task", Title: "Next month", DueAt: due(t, loc, "2026-11-02 09:00"), Body: "Status: Pending"},
{Type: "task", Title: "No date", Body: "Status: Pending"},
{Type: "task", Title: "Already done", DueAt: due(t, loc, "2026-10-01 09:00"), Body: "Status: On Time"},
{Type: "task", Title: "Late but in", DueAt: due(t, loc, "2026-10-02 09:00"), Body: "Status: Received Late"},
{Type: "event", Title: "Past lesson", DueAt: due(t, loc, "2026-10-12 09:00")},
{Type: "message", Title: "Notice", DueAt: due(t, loc, "2026-10-14 07:00")},
}
b := Classify(items, now, loc)
want := map[string][]string{
"overdue": {"Old essay", "Past pending"},
"today": {"Today lesson", "Today task"},
"week": {"Friday task"},
"later": {"Next month"},
"nodue": {"No date"},
"done": {"Already done", "Late but in"},
"messages": {"Notice"},
}
got := map[string][]string{
"overdue": titles(b.Overdue),
"today": titles(b.DueToday),
"week": titles(b.DueWeek),
"later": titles(b.Later),
"nodue": titles(b.NoDue),
"done": titles(b.Done),
"messages": titles(b.Messages),
}
for k, w := range want {
if !equal(got[k], w) {
t.Errorf("%s = %v, want %v", k, got[k], w)
}
}
// A past event is history, not overdue work: it must not appear anywhere.
for k, rows := range got {
for _, title := range rows {
if title == "Past lesson" {
t.Errorf("past event appeared in %s", k)
}
}
}
if b.Actionable() != 6 {
t.Errorf("Actionable = %d, want 6", b.Actionable())
}
if b.Empty() {
t.Error("Empty = true with items present")
}
}
// TestOverdueFromDueDateOnly proves a task with no status line still lands in
// overdue when it is past due.
func TestOverdueFromDueDateOnly(t *testing.T) {
loc := sydney(t)
now := time.Date(2026, 10, 15, 9, 0, 0, 0, loc)
b := Classify([]store.Item{
{Type: "task", Title: "No status", DueAt: due(t, loc, "2026-10-01 09:00")},
}, now, loc)
if len(b.Overdue) != 1 {
t.Fatalf("overdue = %v, want the no-status past task", titles(b.Overdue))
}
}
func TestStatusParsing(t *testing.T) {
cases := map[string]string{
"Status: Overdue\nClass: X": "overdue",
"Class: X\nSubmitted: On Time": "on time",
"Submitted: Received Late\nx": "received late",
"Status: Pending": "pending",
"no status here": "",
"Submitted: Overdue.": "overdue",
}
for body, want := range cases {
if got := Status(body); got != want {
t.Errorf("Status(%q) = %q, want %q", body, got, want)
}
}
}
// TestRenderOverdueFirst proves the brief lists overdue before anything else and
// stays list-shaped (rule 6).
func TestRenderOverdueFirst(t *testing.T) {
loc := sydney(t)
now := time.Date(2026, 10, 15, 9, 0, 0, 0, loc)
b := Classify([]store.Item{
{Type: "task", Subject: "11EENG", Title: "Essay", DueAt: due(t, loc, "2026-10-10 09:00"), Body: "Status: Overdue"},
{Type: "task", Subject: "11MGMA", Title: "Chapter 4", DueAt: due(t, loc, "2026-10-15 15:00"), Body: "Status: Pending"},
{Type: "message", Title: "Sports day"},
}, now, loc)
text := Render("Harry", b, loc)
if !strings.Contains(text, "Harry — 1 overdue · 1 due today") {
t.Errorf("headline missing: %q", text)
}
oi := strings.Index(text, "OVERDUE")
ti := strings.Index(text, "DUE TODAY")
if oi < 0 || ti < 0 || oi > ti {
t.Errorf("overdue must come first:\n%s", text)
}
if !strings.Contains(text, "- 11EENG: Essay (due Sat 10 Oct 09:00) [overdue]") {
t.Errorf("overdue line wrong:\n%s", text)
}
if !strings.Contains(text, "MESSAGES\n- Sports day") {
t.Errorf("messages missing:\n%s", text)
}
}
func titles(items []store.Item) []string {
out := make([]string, 0, len(items))
for _, it := range items {
out = append(out, it.Title)
}
return out
}
func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}