feat(notification): add Resend quota gate and waiting-for-reset state

This commit is contained in:
shira committed 2026-10-06 11:50:38 +03:00
1 parent 3b917befa9
commit b856d4ff54
40 files changed
+4116 -48

No files matched your search

+3 -2
View File
@@ -102,8 +102,8 @@ func run(ctx context.Context, config config.Config, logger *slog.Logger) error {
// start prepares the application without opening listeners. The mail
// transport is selected before migrations, so a configuration that cannot
// start never changes the database. The demo policy seed runs after the
// migrations that create its table.
// start never changes the database. The demo policy seed and the mail quota
// report run after the migrations that create their tables.
func start(ctx context.Context, config config.Config, logger *slog.Logger) (*Application, func(), error) {
if err := config.Validate(); err != nil {
return nil, nil, fmt.Errorf("bootstrap.start - validate configuration: %w", err)
@@ -118,6 +118,7 @@ func start(ctx context.Context, config config.Config, logger *slog.Logger) (*App
if err := seedDemoPolicy(ctx, config, logger); err != nil {
return nil, nil, fmt.Errorf("bootstrap.start - seed demo policy: %w", err)
}
reportMailQuota(ctx, config, logger)
application, cleanup, err := initialize(ctx, config, logger, mailSender)
if err != nil {
return nil, nil, fmt.Errorf("bootstrap.start - initialize application: %w", err)
+53
View File
@@ -0,0 +1,53 @@
package bootstrap
import (
"context"
"log/slog"
"time"
"lincodex/backend/config"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/usecase/notification"
)
// startupQuotaReportTimeout bounds the two small reads of the startup quota
// report.
const startupQuotaReportTimeout = 10 * time.Second
// reportMailQuota logs the configured provider account with its current day
// and month usage, and warns when the month has usage under another account
// label of the same provider, which is what a renamed EMAIL_ACCOUNT looks
// like. It runs after the startup migrations and under the same switch, is
// skipped for EMAIL_PROVIDER=disabled, and never stops startup: a failed
// read is logged as a warning.
func reportMailQuota(ctx context.Context, settings config.Config, logger *slog.Logger) {
if !settings.DatabaseMigrateOnStart || settings.DatabaseURL == "" || settings.EmailProvider == config.EmailProviderDisabled {
return
}
warn := func(message string, err error) {
logger.Warn(message, slog.String("error", err.Error()))
}
account, err := domain.NewMailProviderAccount(string(settings.EmailProvider), settings.EmailAccount)
if err != nil {
warn("mail quota usage was not reported: invalid provider account", err)
return
}
limits, err := domain.NewMailQuotaLimits(int64(settings.EmailQuotaDaily), int64(settings.EmailQuotaMonthly))
if err != nil {
warn("mail quota usage was not reported: invalid limits", err)
return
}
pool, err := postgres.NewPool(ctx, settings.DatabaseURL)
if err != nil {
warn("mail quota usage was not reported: create pool", err)
return
}
defer pool.Close()
ctx, cancel := context.WithTimeout(ctx, startupQuotaReportTimeout)
defer cancel()
reader := postgres.NewMailQuotaStore(pool).MailQuotas()
if err := notification.ReportQuotaUsage(ctx, reader, account, limits, time.Now(), logger); err != nil {
warn("mail quota usage was not reported", err)
}
}
+4
View File
@@ -154,6 +154,9 @@ func TestStartRejectsTransportsThatThisBuildDoesNotProvide(t *testing.T) {
settings := loadMailConfig(t, map[string]string{
"EMAIL_PROVIDER": "smtp",
"DATABASE_URL": unreachableDatabaseURL,
// smtp has no quota default; see config.validateMailQuota.
"EMAIL_QUOTA_DAILY": "100",
"EMAIL_QUOTA_MONTHLY": "3000",
})
logger, logs := captureLogs()
@@ -186,6 +189,7 @@ func TestStartRejectsInvalidMailSettingsBeforeMigrations(t *testing.T) {
{name: "timeout out of range", change: func(settings *config.Config) { settings.EmailTimeout = 0 }, want: "Config.ValidateMail - EMAIL_TIMEOUT must be between 1s and 1m0s"},
{name: "Resend without key", change: func(settings *config.Config) { settings.EmailProvider = config.EmailProviderResend }, want: "config.validateResendAPIKey - RESEND_API_KEY must be set when EMAIL_PROVIDER is resend"},
{name: "SMTP without settings", change: func(settings *config.Config) { settings.EmailProvider = config.EmailProviderSMTP }, want: "Config.validateSMTP - SMTP_HOST must be set when EMAIL_PROVIDER is smtp"},
{name: "quota turned off", change: func(settings *config.Config) { settings.EmailQuotaDaily = 0 }, want: "Config.validateMailQuota - EMAIL_QUOTA_DAILY must be an integer from 1 to 1000000000"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {