Phase 7: notify and send via Apprise (Email + WhatsApp)

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>
This commit is contained in:
2026-10-15 19:28:01 +11:00
parent da57efad73
commit 09639c5125
31 changed files with 4266 additions and 169 deletions

View File

@@ -7,16 +7,18 @@ import (
"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. Phase 5 builds and deletes the
// stored brief (the summary half of FR-10); sending it is Phase 7.
// 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 list")
usage("school brief build --child NAME | school brief build --combined | school brief list")
}
switch args[0] {
case "build":
@@ -24,26 +26,30 @@ func briefCommand(args []string) {
case "list":
briefList()
default:
usage("school brief build --child NAME | school brief list")
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
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 == "" {
fmt.Fprintln(os.Stderr, "school brief build: --child is required")
if child == "" && !combined {
fmt.Fprintln(os.Stderr, "school brief build: --child NAME or --combined is required")
os.Exit(2)
}
if err := runBriefBuild(child); err != nil {
if err := runBriefBuild(child, combined); err != nil {
fmt.Fprintln(os.Stderr, "school brief:", err)
os.Exit(1)
}
}
func runBriefBuild(child string) error {
func runBriefBuild(child string, combined bool) error {
ctx := context.Background()
cfg, err := config.LoadDB()
if err != nil {
@@ -57,6 +63,33 @@ func runBriefBuild(child string) error {
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
@@ -90,21 +123,26 @@ func briefList() {
os.Exit(1)
}
defer st.Close()
summaries, err := st.ListSummaries(ctx, 20)
rows, err := st.RecentNotifications(ctx, 20)
if err != nil {
fmt.Fprintln(os.Stderr, "school brief:", err)
os.Exit(1)
}
if len(summaries) == 0 {
if len(rows) == 0 {
fmt.Println("no briefs stored")
return
}
for _, sm := range summaries {
status := "draft"
if sm.SentAt != nil {
status = "sent " + sm.SentAt.Format("2006-01-02 15:04")
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\n", sm.ID, sm.Recipient, status, firstLine(sm.Text))
fmt.Printf("%d\t%s\t%s\t%s\t%s\n", n.ID, n.Scope, n.Recipient, state, firstLine(n.Text))
}
}