feat(notifications): send product notifications with a rights check right before each send

This commit is contained in:
shira committed 2026-10-09 20:38:39 +03:00
1 parent a856066316
commit 623d41b969
59 files changed
+4125 -1126

No files matched your search

+5
View File
@@ -107,6 +107,11 @@ type Config struct {
// through the configured provider account per UTC day and UTC month.
EmailQuotaDaily int
EmailQuotaMonthly int
// EmailServiceReserveDaily and EmailServiceReserveMonthly keep recipients
// of the daily and monthly quota free for service mail: product
// notifications are admitted only while the windows stay above them.
EmailServiceReserveDaily int
EmailServiceReserveMonthly int
// AppPublicURL is the public origin without a trailing slash, for links
// in mail.
AppPublicURL string
+38
View File
@@ -53,6 +53,10 @@ const (
// limits are not Resend's, so the operator states them.
defaultEmailQuotaDaily = 100
defaultEmailQuotaMonthly = 3000
// defaultEmailServiceReserveShare sets the service reserve default to one
// part of the limit of each window: a fifth, so 20 recipients a day and 600
// a month on the Resend Free plan. Product notifications share the rest.
defaultEmailServiceReserveShare = 5
// maxEmailQuota is far above any plan this project can afford; a larger
// value is a typo, not a plan.
maxEmailQuota = 1_000_000_000
@@ -124,6 +128,21 @@ func (config *Config) loadMailQuota(lookup LookupEnv) error {
if config.EmailQuotaMonthly, err = intOrDefault(setOnly, "EMAIL_QUOTA_MONTHLY", monthly); err != nil {
return err
}
return config.loadServiceReserve(setOnly)
}
// loadServiceReserve reads the recipients of each window that product
// notifications leave to service mail. An unset value takes a fifth of the
// configured limit, so the default follows the plan; a set value of 0 keeps
// the whole window for notifications too.
func (config *Config) loadServiceReserve(setOnly func(key string) (string, bool)) error {
var err error
if config.EmailServiceReserveDaily, err = intOrDefault(setOnly, "EMAIL_SERVICE_RESERVE_DAILY", config.EmailQuotaDaily/defaultEmailServiceReserveShare); err != nil {
return err
}
if config.EmailServiceReserveMonthly, err = intOrDefault(setOnly, "EMAIL_SERVICE_RESERVE_MONTHLY", config.EmailQuotaMonthly/defaultEmailServiceReserveShare); err != nil {
return err
}
return nil
}
@@ -204,6 +223,25 @@ func (config Config) validateMailQuota() error {
if config.EmailQuotaDaily > config.EmailQuotaMonthly {
return errors.New("Config.validateMailQuota - EMAIL_QUOTA_DAILY must not exceed EMAIL_QUOTA_MONTHLY")
}
return config.validateServiceReserve()
}
// validateServiceReserve accepts a reserve from 0 up to, not including, each
// limit. A reserve equal to a limit would leave product notifications no
// recipient in that window, so it is refused rather than read as "off".
func (config Config) validateServiceReserve() error {
for _, reserve := range []struct {
name string
value int
limit int
}{
{"EMAIL_SERVICE_RESERVE_DAILY", config.EmailServiceReserveDaily, config.EmailQuotaDaily},
{"EMAIL_SERVICE_RESERVE_MONTHLY", config.EmailServiceReserveMonthly, config.EmailQuotaMonthly},
} {
if reserve.value < 0 || reserve.value >= reserve.limit {
return fmt.Errorf("Config.validateServiceReserve - %s must be an integer from 0 to below its quota limit", reserve.name)
}
}
return nil
}
+40
View File
@@ -258,3 +258,43 @@ func TestLoadFromEnvRejectsMailSettings(t *testing.T) {
})
}
}
func TestLoadFromEnvServiceReserveDefaultsToAFifthOfEachLimit(t *testing.T) {
settings, err := LoadFromEnv(mailEnvironment(map[string]string{"EMAIL_PROVIDER": "resend", "RESEND_API_KEY": testResendAPIKey}))
if err != nil {
t.Fatalf("LoadFromEnv() error = %v", err)
}
if settings.EmailServiceReserveDaily != 20 || settings.EmailServiceReserveMonthly != 600 {
t.Fatalf("service reserve = %d a day and %d a month, want 20 and 600 for the Resend Free limits", settings.EmailServiceReserveDaily, settings.EmailServiceReserveMonthly)
}
// Zero is a stated reserve, not an unset one: the notifications may then use
// the whole window.
settings, err = LoadFromEnv(mailEnvironment(map[string]string{
"EMAIL_PROVIDER": "resend", "RESEND_API_KEY": testResendAPIKey,
"EMAIL_SERVICE_RESERVE_DAILY": "5", "EMAIL_SERVICE_RESERVE_MONTHLY": "0",
}))
if err != nil {
t.Fatalf("LoadFromEnv() with a stated reserve error = %v", err)
}
if settings.EmailServiceReserveDaily != 5 || settings.EmailServiceReserveMonthly != 0 {
t.Fatalf("service reserve = %d a day and %d a month, want 5 and 0", settings.EmailServiceReserveDaily, settings.EmailServiceReserveMonthly)
}
}
func TestLoadFromEnvRejectsAServiceReserveThatLeavesNoRoom(t *testing.T) {
for name, value := range map[string]map[string]string{
"daily reserve equal to the daily limit": {"EMAIL_SERVICE_RESERVE_DAILY": "100"},
"monthly reserve above the monthly limit": {"EMAIL_SERVICE_RESERVE_MONTHLY": "3001"},
"negative reserve": {"EMAIL_SERVICE_RESERVE_DAILY": "-1"},
} {
values := map[string]string{"EMAIL_PROVIDER": "resend", "RESEND_API_KEY": testResendAPIKey}
for key, reserve := range value {
values[key] = reserve
}
_, err := LoadFromEnv(mailEnvironment(values))
if err == nil || !strings.Contains(err.Error(), "EMAIL_SERVICE_RESERVE") {
t.Fatalf("%s: LoadFromEnv() error = %v, want a refusal that names the reserve", name, err)
}
}
}