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

+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
}