feat(notify): add balance low & account quota notification system

- User balance low notification: email alert when balance drops below
  configurable threshold (user email + verified extra emails)
- Account quota notification: broadcast email to admin-configured
  recipients when daily/weekly/total quota usage exceeds alert threshold
- Admin settings: global enable/disable, default threshold, quota
  notification email list (Email Settings tab)
- User profile: enable/disable, custom threshold, add/remove extra
  notification emails with verification code flow
- Account quota: per-dimension alert toggle and threshold in quota
  control card
- Trigger logic: first-crossing only (old >= threshold && new < threshold
  for balance; old < threshold && new >= threshold for quota), naturally
  prevents duplicate notifications without Redis dedup
This commit is contained in:
erio
2026-04-12 02:48:57 +08:00
parent 60b0fa81ec
commit b32d1a2c9f
47 changed files with 2375 additions and 121 deletions

View File

@@ -211,6 +211,48 @@ func (_c *UserCreate) SetNillableTotpEnabledAt(v *time.Time) *UserCreate {
return _c
}
// SetBalanceNotifyEnabled sets the "balance_notify_enabled" field.
func (_c *UserCreate) SetBalanceNotifyEnabled(v bool) *UserCreate {
_c.mutation.SetBalanceNotifyEnabled(v)
return _c
}
// SetNillableBalanceNotifyEnabled sets the "balance_notify_enabled" field if the given value is not nil.
func (_c *UserCreate) SetNillableBalanceNotifyEnabled(v *bool) *UserCreate {
if v != nil {
_c.SetBalanceNotifyEnabled(*v)
}
return _c
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (_c *UserCreate) SetBalanceNotifyThreshold(v float64) *UserCreate {
_c.mutation.SetBalanceNotifyThreshold(v)
return _c
}
// SetNillableBalanceNotifyThreshold sets the "balance_notify_threshold" field if the given value is not nil.
func (_c *UserCreate) SetNillableBalanceNotifyThreshold(v *float64) *UserCreate {
if v != nil {
_c.SetBalanceNotifyThreshold(*v)
}
return _c
}
// SetBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field.
func (_c *UserCreate) SetBalanceNotifyExtraEmails(v string) *UserCreate {
_c.mutation.SetBalanceNotifyExtraEmails(v)
return _c
}
// SetNillableBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field if the given value is not nil.
func (_c *UserCreate) SetNillableBalanceNotifyExtraEmails(v *string) *UserCreate {
if v != nil {
_c.SetBalanceNotifyExtraEmails(*v)
}
return _c
}
// AddAPIKeyIDs adds the "api_keys" edge to the APIKey entity by IDs.
func (_c *UserCreate) AddAPIKeyIDs(ids ...int64) *UserCreate {
_c.mutation.AddAPIKeyIDs(ids...)
@@ -440,6 +482,14 @@ func (_c *UserCreate) defaults() error {
v := user.DefaultTotpEnabled
_c.mutation.SetTotpEnabled(v)
}
if _, ok := _c.mutation.BalanceNotifyEnabled(); !ok {
v := user.DefaultBalanceNotifyEnabled
_c.mutation.SetBalanceNotifyEnabled(v)
}
if _, ok := _c.mutation.BalanceNotifyExtraEmails(); !ok {
v := user.DefaultBalanceNotifyExtraEmails
_c.mutation.SetBalanceNotifyExtraEmails(v)
}
return nil
}
@@ -503,6 +553,12 @@ func (_c *UserCreate) check() error {
if _, ok := _c.mutation.TotpEnabled(); !ok {
return &ValidationError{Name: "totp_enabled", err: errors.New(`ent: missing required field "User.totp_enabled"`)}
}
if _, ok := _c.mutation.BalanceNotifyEnabled(); !ok {
return &ValidationError{Name: "balance_notify_enabled", err: errors.New(`ent: missing required field "User.balance_notify_enabled"`)}
}
if _, ok := _c.mutation.BalanceNotifyExtraEmails(); !ok {
return &ValidationError{Name: "balance_notify_extra_emails", err: errors.New(`ent: missing required field "User.balance_notify_extra_emails"`)}
}
return nil
}
@@ -586,6 +642,18 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
_spec.SetField(user.FieldTotpEnabledAt, field.TypeTime, value)
_node.TotpEnabledAt = &value
}
if value, ok := _c.mutation.BalanceNotifyEnabled(); ok {
_spec.SetField(user.FieldBalanceNotifyEnabled, field.TypeBool, value)
_node.BalanceNotifyEnabled = value
}
if value, ok := _c.mutation.BalanceNotifyThreshold(); ok {
_spec.SetField(user.FieldBalanceNotifyThreshold, field.TypeFloat64, value)
_node.BalanceNotifyThreshold = &value
}
if value, ok := _c.mutation.BalanceNotifyExtraEmails(); ok {
_spec.SetField(user.FieldBalanceNotifyExtraEmails, field.TypeString, value)
_node.BalanceNotifyExtraEmails = value
}
if nodes := _c.mutation.APIKeysIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -988,6 +1056,54 @@ func (u *UserUpsert) ClearTotpEnabledAt() *UserUpsert {
return u
}
// SetBalanceNotifyEnabled sets the "balance_notify_enabled" field.
func (u *UserUpsert) SetBalanceNotifyEnabled(v bool) *UserUpsert {
u.Set(user.FieldBalanceNotifyEnabled, v)
return u
}
// UpdateBalanceNotifyEnabled sets the "balance_notify_enabled" field to the value that was provided on create.
func (u *UserUpsert) UpdateBalanceNotifyEnabled() *UserUpsert {
u.SetExcluded(user.FieldBalanceNotifyEnabled)
return u
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (u *UserUpsert) SetBalanceNotifyThreshold(v float64) *UserUpsert {
u.Set(user.FieldBalanceNotifyThreshold, v)
return u
}
// UpdateBalanceNotifyThreshold sets the "balance_notify_threshold" field to the value that was provided on create.
func (u *UserUpsert) UpdateBalanceNotifyThreshold() *UserUpsert {
u.SetExcluded(user.FieldBalanceNotifyThreshold)
return u
}
// AddBalanceNotifyThreshold adds v to the "balance_notify_threshold" field.
func (u *UserUpsert) AddBalanceNotifyThreshold(v float64) *UserUpsert {
u.Add(user.FieldBalanceNotifyThreshold, v)
return u
}
// ClearBalanceNotifyThreshold clears the value of the "balance_notify_threshold" field.
func (u *UserUpsert) ClearBalanceNotifyThreshold() *UserUpsert {
u.SetNull(user.FieldBalanceNotifyThreshold)
return u
}
// SetBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field.
func (u *UserUpsert) SetBalanceNotifyExtraEmails(v string) *UserUpsert {
u.Set(user.FieldBalanceNotifyExtraEmails, v)
return u
}
// UpdateBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field to the value that was provided on create.
func (u *UserUpsert) UpdateBalanceNotifyExtraEmails() *UserUpsert {
u.SetExcluded(user.FieldBalanceNotifyExtraEmails)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
@@ -1250,6 +1366,62 @@ func (u *UserUpsertOne) ClearTotpEnabledAt() *UserUpsertOne {
})
}
// SetBalanceNotifyEnabled sets the "balance_notify_enabled" field.
func (u *UserUpsertOne) SetBalanceNotifyEnabled(v bool) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyEnabled(v)
})
}
// UpdateBalanceNotifyEnabled sets the "balance_notify_enabled" field to the value that was provided on create.
func (u *UserUpsertOne) UpdateBalanceNotifyEnabled() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyEnabled()
})
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (u *UserUpsertOne) SetBalanceNotifyThreshold(v float64) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyThreshold(v)
})
}
// AddBalanceNotifyThreshold adds v to the "balance_notify_threshold" field.
func (u *UserUpsertOne) AddBalanceNotifyThreshold(v float64) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.AddBalanceNotifyThreshold(v)
})
}
// UpdateBalanceNotifyThreshold sets the "balance_notify_threshold" field to the value that was provided on create.
func (u *UserUpsertOne) UpdateBalanceNotifyThreshold() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyThreshold()
})
}
// ClearBalanceNotifyThreshold clears the value of the "balance_notify_threshold" field.
func (u *UserUpsertOne) ClearBalanceNotifyThreshold() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.ClearBalanceNotifyThreshold()
})
}
// SetBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field.
func (u *UserUpsertOne) SetBalanceNotifyExtraEmails(v string) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyExtraEmails(v)
})
}
// UpdateBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field to the value that was provided on create.
func (u *UserUpsertOne) UpdateBalanceNotifyExtraEmails() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyExtraEmails()
})
}
// Exec executes the query.
func (u *UserUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
@@ -1678,6 +1850,62 @@ func (u *UserUpsertBulk) ClearTotpEnabledAt() *UserUpsertBulk {
})
}
// SetBalanceNotifyEnabled sets the "balance_notify_enabled" field.
func (u *UserUpsertBulk) SetBalanceNotifyEnabled(v bool) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyEnabled(v)
})
}
// UpdateBalanceNotifyEnabled sets the "balance_notify_enabled" field to the value that was provided on create.
func (u *UserUpsertBulk) UpdateBalanceNotifyEnabled() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyEnabled()
})
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (u *UserUpsertBulk) SetBalanceNotifyThreshold(v float64) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyThreshold(v)
})
}
// AddBalanceNotifyThreshold adds v to the "balance_notify_threshold" field.
func (u *UserUpsertBulk) AddBalanceNotifyThreshold(v float64) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.AddBalanceNotifyThreshold(v)
})
}
// UpdateBalanceNotifyThreshold sets the "balance_notify_threshold" field to the value that was provided on create.
func (u *UserUpsertBulk) UpdateBalanceNotifyThreshold() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyThreshold()
})
}
// ClearBalanceNotifyThreshold clears the value of the "balance_notify_threshold" field.
func (u *UserUpsertBulk) ClearBalanceNotifyThreshold() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.ClearBalanceNotifyThreshold()
})
}
// SetBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field.
func (u *UserUpsertBulk) SetBalanceNotifyExtraEmails(v string) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyExtraEmails(v)
})
}
// UpdateBalanceNotifyExtraEmails sets the "balance_notify_extra_emails" field to the value that was provided on create.
func (u *UserUpsertBulk) UpdateBalanceNotifyExtraEmails() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyExtraEmails()
})
}
// Exec executes the query.
func (u *UserUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {