The morning briefs go out through the house Apprise API on `.35:8210`: one brief per boy and one combined brief to Jo and Sam (FR-9, FR-14). The service names an Apprise tag (`harry_email`, `joanna_whatsapp`, ...) and never holds an address, a phone number or an SMTP password (AGENTS.md §10 rule 3). - internal/apprise: keyed and stateless client, tag filtering, and error redaction so nothing sensitive reaches a log, a row or a screen. - internal/brief: builds the per-boy and the combined brief, delivers on email and WhatsApp, records every attempt in `notifications` (migration 004 adds scope, channel, tag, ok and error) and fails loudly. - Send panel (§5 screen 4): recipients, channels, an exact preview, Send now, plus the Send action on a stored draft (FR-10). The dashboard shows a Morning briefs banner and the Sends list. - POST /notify/morning: the bearer-token endpoint the 6:00am n8n schedule calls. 207 when a channel or a scan failed; `dry_run=1` checks the wiring without messaging the family. - scripts/morning-brief.sh: scan both children, then send. It is the n8n job; deploy/n8n-morning-brief.json is the ready-to-import workflow (06:00 daily). - FR-12: a failed scan is never "nothing due" -- the brief says so, and the run exits non-zero. A failed delivery is recorded with no sent_at. - The long no-due-date tail is capped in the sent brief so it stays brief (§10 rule 6); overdue work is never capped. Verified live: 8 deliveries recorded (4 email, 4 WhatsApp) with HTTP 200 and sent_at, and the endpoint answers 401/200/207 as documented. Evidence in docs/phase-7-notify-notes.md. Co-Authored-By: Paperclip <noreply@paperclip.ing>
162 lines
3.9 KiB
Go
162 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/brief"
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/config"
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/dashboard"
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/store"
|
|
)
|
|
|
|
// briefCommand is the `school brief` entry point: build and delete the stored
|
|
// brief (the summary half of FR-10), list what is stored, and send it. Sending
|
|
// through Apprise is Phase 7 and lives in `school notify` too.
|
|
func briefCommand(args []string) {
|
|
if len(args) == 0 {
|
|
usage("school brief build --child NAME | school brief build --combined | school brief list")
|
|
}
|
|
switch args[0] {
|
|
case "build":
|
|
briefBuild(args[1:])
|
|
case "list":
|
|
briefList()
|
|
default:
|
|
usage("school brief build --child NAME | school brief build --combined | school brief list")
|
|
}
|
|
}
|
|
|
|
func briefBuild(args []string) {
|
|
fs := flag.NewFlagSet("brief build", flag.ContinueOnError)
|
|
var (
|
|
child string
|
|
combined bool
|
|
)
|
|
fs.StringVar(&child, "child", "", "child name (Harry or Finn)")
|
|
fs.BoolVar(&combined, "combined", false, "build the combined brief for Jo and Sam")
|
|
_ = fs.Parse(args)
|
|
if child == "" && !combined {
|
|
fmt.Fprintln(os.Stderr, "school brief build: --child NAME or --combined is required")
|
|
os.Exit(2)
|
|
}
|
|
if err := runBriefBuild(child, combined); err != nil {
|
|
fmt.Fprintln(os.Stderr, "school brief:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runBriefBuild(child string, combined bool) error {
|
|
ctx := context.Background()
|
|
cfg, err := config.LoadDB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
st, err := store.Open(cfg.DBPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer st.Close()
|
|
if err := st.Migrate(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
if combined {
|
|
children, err := st.ListChildren(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
parts := make([]dashboard.Combined, 0, len(children))
|
|
for _, c := range children {
|
|
items, err := st.ItemsForChild(ctx, c.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
parts = append(parts, dashboard.Combined{
|
|
Child: c.Name,
|
|
Buckets: dashboard.Classify(items, time.Now(), cfg.Location),
|
|
})
|
|
}
|
|
text := dashboard.RenderCombined(parts, cfg.Location)
|
|
recipient := brief.CombinedNames(children)
|
|
id, err := st.CreateDraft(ctx, brief.ScopeCombined, recipient, text)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("brief=%d scope=combined recipient=%s bytes=%d\n", id, recipient, len(text))
|
|
return nil
|
|
}
|
|
|
|
c, err := st.ChildByName(ctx, child)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if c == nil {
|
|
return fmt.Errorf("unknown child %q", child)
|
|
}
|
|
items, err := st.ItemsForChild(ctx, c.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
text := dashboard.Render(c.Name, dashboard.Classify(items, time.Now(), cfg.Location), cfg.Location)
|
|
id, err := st.CreateSummary(ctx, c.Name, text)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("brief=%d recipient=%s bytes=%d\n", id, c.Name, len(text))
|
|
return nil
|
|
}
|
|
|
|
func briefList() {
|
|
ctx := context.Background()
|
|
cfg, err := config.LoadDB()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "school brief:", err)
|
|
os.Exit(1)
|
|
}
|
|
st, err := store.Open(cfg.DBPath)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "school brief:", err)
|
|
os.Exit(1)
|
|
}
|
|
defer st.Close()
|
|
rows, err := st.RecentNotifications(ctx, 20)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "school brief:", err)
|
|
os.Exit(1)
|
|
}
|
|
if len(rows) == 0 {
|
|
fmt.Println("no briefs stored")
|
|
return
|
|
}
|
|
for _, n := range rows {
|
|
state := "draft"
|
|
switch {
|
|
case n.Channel != "" && n.OK && n.SentAt != nil:
|
|
state = "sent " + n.SentAt.In(cfg.Location).Format("2006-01-02 15:04")
|
|
case n.Channel != "" && n.OK:
|
|
state = "sent"
|
|
case n.Channel != "":
|
|
state = "FAILED"
|
|
}
|
|
fmt.Printf("%d\t%s\t%s\t%s\t%s\n", n.ID, n.Scope, n.Recipient, state, firstLine(n.Text))
|
|
}
|
|
}
|
|
|
|
func firstLine(s string) string {
|
|
for i, r := range s {
|
|
if r == '\n' {
|
|
return s[:i]
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
func usage(msg string) {
|
|
fmt.Fprintln(os.Stderr, "usage:", msg)
|
|
os.Exit(2)
|
|
}
|