hub (unreleased): review fix 1 — „told” is told at an address: a changed notification address is told again and waits again (the hole D84 exists for)
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
cbfeeb626e
commit
a6f4aa0b64
10 files changed
+412
-44
No files matched your search
@@ -84,49 +84,53 @@ func (d *Dispatcher) MailHeld() bool { return d.mailHold.Held() }
|
||||
|
||||
// SendEndNotice tries ONCE to mail the household that the off-site copies of `app` are deleted from the day `ends`
|
||||
// (the hub's own day: the caller passes its clock + 30 days). It returns what the try came to and, when nobody was
|
||||
// told, the hub's own words for why. It keeps no state: the caller's record decides whether there is a next try.
|
||||
// told, the hub's own words for why; for a sent mail, the key of the address it went to (store.NoticeAddressKey).
|
||||
// It keeps no state: the caller's record decides whether there is a next try.
|
||||
// app must have passed store.ValidNoticeApp (a plain app slug); a name that has not is refused here again.
|
||||
func (d *Dispatcher) SendEndNotice(customerID, app string, ends time.Time) (EndNoticeOutcome, string) {
|
||||
func (d *Dispatcher) SendEndNotice(customerID, app string, ends time.Time) (outcome EndNoticeOutcome, why string, addrKey string) {
|
||||
if !store.ValidNoticeApp(app) {
|
||||
return EndNoticeFailed, "not an app name"
|
||||
return EndNoticeFailed, "not an app name", ""
|
||||
}
|
||||
if d.resendAPIKey == "" {
|
||||
return EndNoticeFailed, "the hub has no mail service configured"
|
||||
return EndNoticeFailed, "the hub has no mail service configured", ""
|
||||
}
|
||||
if d.store.IsCustomerBlocked(customerID) {
|
||||
return EndNoticeNoAddress, "the household is blocked"
|
||||
return EndNoticeNoAddress, "the household is blocked", ""
|
||||
}
|
||||
prefs, err := d.store.GetNotificationPrefs(customerID)
|
||||
if err != nil {
|
||||
return EndNoticeFailed, "the household's notification address could not be read"
|
||||
return EndNoticeFailed, "the household's notification address could not be read", ""
|
||||
}
|
||||
if prefs == nil || strings.TrimSpace(prefs.Email) == "" {
|
||||
return EndNoticeNoAddress, "the household has no notification address"
|
||||
return EndNoticeNoAddress, "the household has no notification address", ""
|
||||
}
|
||||
now := time.Now()
|
||||
sentToday, cerr := d.store.CountCustomerTriesSince(customerID, EndNoticeEvent, now.Add(-24*time.Hour))
|
||||
if cerr != nil {
|
||||
// A log that cannot be read is not „nothing sent”: no receipt goes, and the notice stays to be tried again.
|
||||
return EndNoticeLimit, "the hub's sent log could not be read"
|
||||
return EndNoticeLimit, "the hub's sent log could not be read", ""
|
||||
}
|
||||
if sentToday >= receiptsPerDay {
|
||||
return EndNoticeLimit, fmt.Sprintf("the household already got %d of these notices in the last 24 hours (the daily limit) — this one goes later", receiptsPerDay)
|
||||
return EndNoticeLimit, fmt.Sprintf("the household already got %d of these notices in the last 24 hours (the daily limit) — this one goes later", receiptsPerDay), ""
|
||||
}
|
||||
// The address the mail goes to, read ONCE: its key is returned so that the caller's record says which address
|
||||
// was told (the address may change while the mail is on its way — review finding 1).
|
||||
to := strings.TrimSpace(prefs.Email)
|
||||
subject, body := FormatReceiptEmail(d.store.CustomerLanguage(customerID), customerID, EndNoticeEvent, "info", app, ends)
|
||||
logMsg := EndNoticeEvent + " " + app
|
||||
if err := d.deliver("customer receipt "+EndNoticeEvent, customerID, strings.TrimSpace(prefs.Email), subject, body, nil); isHeld(err) {
|
||||
if err := d.deliver("customer receipt "+EndNoticeEvent, customerID, to, subject, body, nil); isHeld(err) {
|
||||
d.store.LogNotification(customerID, EndNoticeEvent, "info", logMsg, "held", "MAIL-HOLD marker present — not sent; the notice stays to be sent", "customer")
|
||||
return EndNoticeHeld, "the hub is not sending mail (the MAIL-HOLD marker is present)"
|
||||
return EndNoticeHeld, "the hub is not sending mail (the MAIL-HOLD marker is present)", ""
|
||||
} else if err != nil {
|
||||
d.logger.Printf("[ERROR] Notice %s failed for %s/%s: %v — it stays to be sent", EndNoticeEvent, customerID, app, err)
|
||||
d.store.LogNotification(customerID, EndNoticeEvent, "info", logMsg, "failed", err.Error(), "customer")
|
||||
return EndNoticeFailed, "the mail service did not accept the mail"
|
||||
return EndNoticeFailed, "the mail service did not accept the mail", ""
|
||||
}
|
||||
d.logger.Printf("[INFO] Notice %s sent for %s/%s", EndNoticeEvent, customerID, app) // never the address (R-922)
|
||||
if lerr := d.store.LogNotification(customerID, EndNoticeEvent, "info", logMsg, "sent", "", "customer"); lerr != nil {
|
||||
d.logger.Printf("[WARN] Notice %s for %s/%s was sent, but the sent log could not be written: %v", EndNoticeEvent, customerID, app, lerr)
|
||||
}
|
||||
return EndNoticeSent, ""
|
||||
return EndNoticeSent, "", store.NoticeAddressKey(to)
|
||||
}
|
||||
|
||||
// SendOperatorNow mails the operator ONE hub-made event and returns only when the mail service has answered: nil
|
||||
|
||||
@@ -46,7 +46,7 @@ const receiptDetails = `{"stack_name":"vikunja","removed_at":"2026-11-15T10:00:0
|
||||
// for this. The operator is not mailed, and no raw details are in it.
|
||||
func TestNotice_ReachesTheHouseholdWhateverItsSwitches(t *testing.T) {
|
||||
d, mails := receiptDispatcher(t, "customer@example.com", []string{})
|
||||
if out, why := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent {
|
||||
if out, why, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent {
|
||||
t.Fatalf("outcome = %s (%s), want sent", out, why)
|
||||
}
|
||||
if len(*mails) != 1 {
|
||||
@@ -80,7 +80,7 @@ func TestNotice_FollowsTheHouseholdsLanguage(t *testing.T) {
|
||||
if err := d.store.SaveReport("c1", []byte(`{"customer_id":"c1","language":"en"}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
|
||||
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
|
||||
t.Fatalf("outcome %s, mails %d", out, len(*mails))
|
||||
}
|
||||
m := (*mails)[0]
|
||||
@@ -126,20 +126,20 @@ func TestNotice_TheBoxesEventMailsNobody(t *testing.T) {
|
||||
// the caller tells the operator in its place.
|
||||
func TestNotice_NoAddressOrBlocked_SaysSoAndMailsNobody(t *testing.T) {
|
||||
d, mails := receiptDispatcher(t, "", nil)
|
||||
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
|
||||
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
|
||||
t.Fatalf("no address: outcome %s, %d mail(s)", out, len(*mails))
|
||||
}
|
||||
d, mails = receiptDispatcher(t, "customer@example.com", nil)
|
||||
if err := d.store.SetCustomerConfigStatus("c1", "blocked"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
|
||||
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
|
||||
t.Fatalf("blocked: outcome %s, %d mail(s)", out, len(*mails))
|
||||
}
|
||||
if err := d.store.SetCustomerConfigStatus("c1", "active"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
|
||||
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
|
||||
t.Fatalf("positive control: an unblocked household must get the notice: outcome %s, %d mail(s)", out, len(*mails))
|
||||
}
|
||||
}
|
||||
@@ -149,17 +149,17 @@ func TestNotice_NoAddressOrBlocked_SaysSoAndMailsNobody(t *testing.T) {
|
||||
func TestNotice_TheDailyLimitDelaysAndSurvivesARestart(t *testing.T) {
|
||||
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
||||
for i := 0; i < receiptsPerDay; i++ {
|
||||
if out, why := d.SendEndNotice("c1", fmt.Sprintf("app-%d", i), noticeDay); out != EndNoticeSent {
|
||||
if out, why, _ := d.SendEndNotice("c1", fmt.Sprintf("app-%d", i), noticeDay); out != EndNoticeSent {
|
||||
t.Fatalf("notice %d: %s (%s)", i, out, why)
|
||||
}
|
||||
}
|
||||
if out, why := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || !strings.Contains(why, "later") {
|
||||
if out, why, _ := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || !strings.Contains(why, "later") {
|
||||
t.Fatalf("the sixth notice: outcome %s (%q), want limit", out, why)
|
||||
}
|
||||
d2 := opOnlyDispatcher(t, d.store, &sentTo{}) // the restart: nothing in memory
|
||||
n2 := 0
|
||||
d2.sendEmailFn = func(string, string, string, map[string]string) error { n2++; return nil }
|
||||
if out, _ := d2.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || n2 != 0 {
|
||||
if out, _, _ := d2.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || n2 != 0 {
|
||||
t.Fatalf("after a restart: outcome %s, %d mail(s) — the limit must be the hub's sent log", out, n2)
|
||||
}
|
||||
if len(*mails) != receiptsPerDay {
|
||||
@@ -169,7 +169,7 @@ func TestNotice_TheDailyLimitDelaysAndSurvivesARestart(t *testing.T) {
|
||||
if err := d.store.BackdateNotificationLogForTest("c1", 25*time.Hour); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, why := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeSent {
|
||||
if out, why, _ := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeSent {
|
||||
t.Fatalf("a day later: %s (%s), want sent — the limit delays, it does not drop", out, why)
|
||||
}
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d.SetMailHold(mailhold.New(dir, nil))
|
||||
if out, why := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeHeld || !strings.Contains(why, "MAIL-HOLD") || len(*mails) != 0 {
|
||||
if out, why, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeHeld || !strings.Contains(why, "MAIL-HOLD") || len(*mails) != 0 {
|
||||
t.Fatalf("held: outcome %s (%q), %d mail(s)", out, why, len(*mails))
|
||||
}
|
||||
if !d.MailHeld() {
|
||||
@@ -190,7 +190,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
|
||||
}
|
||||
d.SetMailHold(nil)
|
||||
d.sendEmailFn = func(string, string, string, map[string]string) error { return errors.New("resend: 503") }
|
||||
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
|
||||
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
|
||||
t.Fatalf("failed send: outcome %s", out)
|
||||
}
|
||||
// neither left a „sent” row: the limit must not count a mail that did not go
|
||||
@@ -199,7 +199,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
|
||||
}
|
||||
// no mail service at all
|
||||
d.resendAPIKey = ""
|
||||
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
|
||||
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
|
||||
t.Fatalf("no mail service: outcome %s", out)
|
||||
}
|
||||
}
|
||||
@@ -208,7 +208,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
|
||||
func TestNotice_OnlyAnAppNameIsMailed(t *testing.T) {
|
||||
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
||||
for _, app := range []string{"", "pay now at evil.example", "<b>x</b>", "traefik", "_shares", strings.Repeat("a", 65)} {
|
||||
if out, _ := d.SendEndNotice("c1", app, noticeDay); out == EndNoticeSent || len(*mails) != 0 {
|
||||
if out, _, _ := d.SendEndNotice("c1", app, noticeDay); out == EndNoticeSent || len(*mails) != 0 {
|
||||
t.Fatalf("%q: outcome %s, %d mail(s)", app, out, len(*mails))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user