package domain import ( "errors" "fmt" "time" ) // ErrInvalidMailQuota reports quota limits or a quota window that break a // quota rule. var ErrInvalidMailQuota = errors.New("invalid mail quota") // MailQuotaPeriod is a quota window kind. Both windows are UTC calendar // periods, as the Resend Free plan counts them: a day starts at 00:00 UTC and // a month on its first day at 00:00 UTC. type MailQuotaPeriod string const ( MailQuotaPeriodDay MailQuotaPeriod = "day" MailQuotaPeriodMonth MailQuotaPeriod = "month" ) // MailQuotaLimits are the recipients that one provider account may be sent // per UTC day and per UTC calendar month. Every recipient counts, as // providers count them. The values come from configuration, never from // domain constants, because they follow the provider plan of the stand. type MailQuotaLimits struct { daily int64 monthly int64 } // NewMailQuotaLimits accepts positive limits where the daily limit does not // exceed the monthly one. A zero limit is refused rather than read as // "unlimited": no setting turns the quota off. func NewMailQuotaLimits(daily, monthly int64) (MailQuotaLimits, error) { switch { case daily <= 0 || monthly <= 0: return MailQuotaLimits{}, fmt.Errorf("domain.NewMailQuotaLimits - limits must be positive: %w", ErrInvalidMailQuota) case daily > monthly: return MailQuotaLimits{}, fmt.Errorf("domain.NewMailQuotaLimits - the daily limit must not exceed the monthly limit: %w", ErrInvalidMailQuota) } return MailQuotaLimits{daily: daily, monthly: monthly}, nil } func (limits MailQuotaLimits) IsZero() bool { return limits.daily == 0 } // WithReserve returns the limits that a sender may use when daily and monthly // recipients stay free for other mail. Each window keeps its own reserve, so // the result is checked window by window; it may not keep the rule that the // daily limit does not exceed the monthly one, which the configuration keeps. // A reserve must be zero or more and leave at least one recipient per window. func (limits MailQuotaLimits) WithReserve(daily, monthly int64) (MailQuotaLimits, error) { const location = "domain.MailQuotaLimits.WithReserve" switch { case daily < 0 || monthly < 0: return MailQuotaLimits{}, fmt.Errorf("%s - reserves must not be negative: %w", location, ErrInvalidMailQuota) case daily >= limits.daily || monthly >= limits.monthly: return MailQuotaLimits{}, fmt.Errorf("%s - a reserve must leave room in its window: %w", location, ErrInvalidMailQuota) } return MailQuotaLimits{daily: limits.daily - daily, monthly: limits.monthly - monthly}, nil } func (limits MailQuotaLimits) Daily() int64 { return limits.daily } func (limits MailQuotaLimits) Monthly() int64 { return limits.monthly } // Of returns the limit of a period; zero for an unknown period. func (limits MailQuotaLimits) Of(period MailQuotaPeriod) int64 { switch period { case MailQuotaPeriodDay: return limits.daily case MailQuotaPeriodMonth: return limits.monthly default: return 0 } } // MailQuotaWindows are the UTC day and the UTC calendar month that contain // one instant. A reservation counts against both. type MailQuotaWindows struct { dayStart time.Time monthStart time.Time } // MailQuotaWindowsAt returns the windows that contain at, in UTC whatever // the location of at. func MailQuotaWindowsAt(at time.Time) MailQuotaWindows { utc := at.UTC() return MailQuotaWindows{ dayStart: time.Date(utc.Year(), utc.Month(), utc.Day(), 0, 0, 0, 0, time.UTC), monthStart: time.Date(utc.Year(), utc.Month(), 1, 0, 0, 0, 0, time.UTC), } } func (windows MailQuotaWindows) IsZero() bool { return windows.dayStart.IsZero() } // DayStart is 00:00 UTC of the day. func (windows MailQuotaWindows) DayStart() time.Time { return windows.dayStart } // MonthStart is 00:00 UTC of the first day of the month. func (windows MailQuotaWindows) MonthStart() time.Time { return windows.monthStart } // Start returns the start of a period; the zero time for an unknown period. func (windows MailQuotaWindows) Start(period MailQuotaPeriod) time.Time { switch period { case MailQuotaPeriodDay: return windows.dayStart case MailQuotaPeriodMonth: return windows.monthStart default: return time.Time{} } } // ResetAt returns when a period ends and its count starts again from zero: // the next 00:00 UTC for the day, the first day of the next month at 00:00 // UTC for the month. The zero time for an unknown period. func (windows MailQuotaWindows) ResetAt(period MailQuotaPeriod) time.Time { switch period { case MailQuotaPeriodDay: return windows.dayStart.AddDate(0, 0, 1) case MailQuotaPeriodMonth: return windows.monthStart.AddDate(0, 1, 0) default: return time.Time{} } }