42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package inviteemailratelimit
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gocql/gocql"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// IncrementDailyEmailCount increments the counter for emails sent by a user on the given date.
|
|
// Uses Cassandra COUNTER type for atomic increment operations.
|
|
// TTL of 2 days (172800 seconds) is applied at the UPDATE level since counter tables
|
|
// do not support default_time_to_live in Cassandra.
|
|
func (r *repositoryImpl) IncrementDailyEmailCount(ctx context.Context, userID gocql.UUID, date time.Time) error {
|
|
r.logger.Debug("Incrementing daily email count",
|
|
zap.String("user_id", userID.String()),
|
|
zap.Time("date", date))
|
|
|
|
// Normalize date to midnight UTC
|
|
dateOnly := date.UTC().Truncate(24 * time.Hour)
|
|
|
|
// TTL of 172800 seconds = 2 days for automatic cleanup
|
|
query := r.session.Query(`
|
|
UPDATE invite_email_rate_limits_by_user_id_and_date
|
|
USING TTL 172800
|
|
SET emails_sent_today = emails_sent_today + 1
|
|
WHERE user_id = ? AND date = ?
|
|
`, userID, dateOnly).WithContext(ctx)
|
|
|
|
if err := query.Exec(); err != nil {
|
|
r.logger.Error("Failed to increment daily email count",
|
|
zap.String("user_id", userID.String()),
|
|
zap.Error(err))
|
|
return err
|
|
}
|
|
|
|
r.logger.Debug("Successfully incremented daily email count",
|
|
zap.String("user_id", userID.String()))
|
|
|
|
return nil
|
|
}
|