feat(notify): add percentage threshold type for balance low notification

- Add threshold_type field (fixed/percentage) to system and user settings
- Add total_recharged field to users table, auto-incremented on balance credit
- Percentage mode: effective threshold = total_recharged × percentage / 100
- User-level threshold_type inherits from system default when not set
- Update admin settings UI with radio selector (fixed amount / percentage)
- Migration: 102_add_balance_notify_threshold_type.sql
This commit is contained in:
erio
2026-04-12 13:53:02 +08:00
parent d0674e0ff9
commit f694afbbf4
27 changed files with 838 additions and 74 deletions

View File

@@ -225,6 +225,20 @@ func (_c *UserCreate) SetNillableBalanceNotifyEnabled(v *bool) *UserCreate {
return _c
}
// SetBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field.
func (_c *UserCreate) SetBalanceNotifyThresholdType(v string) *UserCreate {
_c.mutation.SetBalanceNotifyThresholdType(v)
return _c
}
// SetNillableBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field if the given value is not nil.
func (_c *UserCreate) SetNillableBalanceNotifyThresholdType(v *string) *UserCreate {
if v != nil {
_c.SetBalanceNotifyThresholdType(*v)
}
return _c
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (_c *UserCreate) SetBalanceNotifyThreshold(v float64) *UserCreate {
_c.mutation.SetBalanceNotifyThreshold(v)
@@ -253,6 +267,20 @@ func (_c *UserCreate) SetNillableBalanceNotifyExtraEmails(v *string) *UserCreate
return _c
}
// SetTotalRecharged sets the "total_recharged" field.
func (_c *UserCreate) SetTotalRecharged(v float64) *UserCreate {
_c.mutation.SetTotalRecharged(v)
return _c
}
// SetNillableTotalRecharged sets the "total_recharged" field if the given value is not nil.
func (_c *UserCreate) SetNillableTotalRecharged(v *float64) *UserCreate {
if v != nil {
_c.SetTotalRecharged(*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...)
@@ -486,10 +514,18 @@ func (_c *UserCreate) defaults() error {
v := user.DefaultBalanceNotifyEnabled
_c.mutation.SetBalanceNotifyEnabled(v)
}
if _, ok := _c.mutation.BalanceNotifyThresholdType(); !ok {
v := user.DefaultBalanceNotifyThresholdType
_c.mutation.SetBalanceNotifyThresholdType(v)
}
if _, ok := _c.mutation.BalanceNotifyExtraEmails(); !ok {
v := user.DefaultBalanceNotifyExtraEmails
_c.mutation.SetBalanceNotifyExtraEmails(v)
}
if _, ok := _c.mutation.TotalRecharged(); !ok {
v := user.DefaultTotalRecharged
_c.mutation.SetTotalRecharged(v)
}
return nil
}
@@ -556,9 +592,15 @@ func (_c *UserCreate) check() error {
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.BalanceNotifyThresholdType(); !ok {
return &ValidationError{Name: "balance_notify_threshold_type", err: errors.New(`ent: missing required field "User.balance_notify_threshold_type"`)}
}
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"`)}
}
if _, ok := _c.mutation.TotalRecharged(); !ok {
return &ValidationError{Name: "total_recharged", err: errors.New(`ent: missing required field "User.total_recharged"`)}
}
return nil
}
@@ -646,6 +688,10 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
_spec.SetField(user.FieldBalanceNotifyEnabled, field.TypeBool, value)
_node.BalanceNotifyEnabled = value
}
if value, ok := _c.mutation.BalanceNotifyThresholdType(); ok {
_spec.SetField(user.FieldBalanceNotifyThresholdType, field.TypeString, value)
_node.BalanceNotifyThresholdType = value
}
if value, ok := _c.mutation.BalanceNotifyThreshold(); ok {
_spec.SetField(user.FieldBalanceNotifyThreshold, field.TypeFloat64, value)
_node.BalanceNotifyThreshold = &value
@@ -654,6 +700,10 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
_spec.SetField(user.FieldBalanceNotifyExtraEmails, field.TypeString, value)
_node.BalanceNotifyExtraEmails = value
}
if value, ok := _c.mutation.TotalRecharged(); ok {
_spec.SetField(user.FieldTotalRecharged, field.TypeFloat64, value)
_node.TotalRecharged = value
}
if nodes := _c.mutation.APIKeysIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@@ -1068,6 +1118,18 @@ func (u *UserUpsert) UpdateBalanceNotifyEnabled() *UserUpsert {
return u
}
// SetBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field.
func (u *UserUpsert) SetBalanceNotifyThresholdType(v string) *UserUpsert {
u.Set(user.FieldBalanceNotifyThresholdType, v)
return u
}
// UpdateBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field to the value that was provided on create.
func (u *UserUpsert) UpdateBalanceNotifyThresholdType() *UserUpsert {
u.SetExcluded(user.FieldBalanceNotifyThresholdType)
return u
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (u *UserUpsert) SetBalanceNotifyThreshold(v float64) *UserUpsert {
u.Set(user.FieldBalanceNotifyThreshold, v)
@@ -1104,6 +1166,24 @@ func (u *UserUpsert) UpdateBalanceNotifyExtraEmails() *UserUpsert {
return u
}
// SetTotalRecharged sets the "total_recharged" field.
func (u *UserUpsert) SetTotalRecharged(v float64) *UserUpsert {
u.Set(user.FieldTotalRecharged, v)
return u
}
// UpdateTotalRecharged sets the "total_recharged" field to the value that was provided on create.
func (u *UserUpsert) UpdateTotalRecharged() *UserUpsert {
u.SetExcluded(user.FieldTotalRecharged)
return u
}
// AddTotalRecharged adds v to the "total_recharged" field.
func (u *UserUpsert) AddTotalRecharged(v float64) *UserUpsert {
u.Add(user.FieldTotalRecharged, v)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
@@ -1380,6 +1460,20 @@ func (u *UserUpsertOne) UpdateBalanceNotifyEnabled() *UserUpsertOne {
})
}
// SetBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field.
func (u *UserUpsertOne) SetBalanceNotifyThresholdType(v string) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyThresholdType(v)
})
}
// UpdateBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field to the value that was provided on create.
func (u *UserUpsertOne) UpdateBalanceNotifyThresholdType() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyThresholdType()
})
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (u *UserUpsertOne) SetBalanceNotifyThreshold(v float64) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
@@ -1422,6 +1516,27 @@ func (u *UserUpsertOne) UpdateBalanceNotifyExtraEmails() *UserUpsertOne {
})
}
// SetTotalRecharged sets the "total_recharged" field.
func (u *UserUpsertOne) SetTotalRecharged(v float64) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.SetTotalRecharged(v)
})
}
// AddTotalRecharged adds v to the "total_recharged" field.
func (u *UserUpsertOne) AddTotalRecharged(v float64) *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.AddTotalRecharged(v)
})
}
// UpdateTotalRecharged sets the "total_recharged" field to the value that was provided on create.
func (u *UserUpsertOne) UpdateTotalRecharged() *UserUpsertOne {
return u.Update(func(s *UserUpsert) {
s.UpdateTotalRecharged()
})
}
// Exec executes the query.
func (u *UserUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
@@ -1864,6 +1979,20 @@ func (u *UserUpsertBulk) UpdateBalanceNotifyEnabled() *UserUpsertBulk {
})
}
// SetBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field.
func (u *UserUpsertBulk) SetBalanceNotifyThresholdType(v string) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.SetBalanceNotifyThresholdType(v)
})
}
// UpdateBalanceNotifyThresholdType sets the "balance_notify_threshold_type" field to the value that was provided on create.
func (u *UserUpsertBulk) UpdateBalanceNotifyThresholdType() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.UpdateBalanceNotifyThresholdType()
})
}
// SetBalanceNotifyThreshold sets the "balance_notify_threshold" field.
func (u *UserUpsertBulk) SetBalanceNotifyThreshold(v float64) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
@@ -1906,6 +2035,27 @@ func (u *UserUpsertBulk) UpdateBalanceNotifyExtraEmails() *UserUpsertBulk {
})
}
// SetTotalRecharged sets the "total_recharged" field.
func (u *UserUpsertBulk) SetTotalRecharged(v float64) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.SetTotalRecharged(v)
})
}
// AddTotalRecharged adds v to the "total_recharged" field.
func (u *UserUpsertBulk) AddTotalRecharged(v float64) *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.AddTotalRecharged(v)
})
}
// UpdateTotalRecharged sets the "total_recharged" field to the value that was provided on create.
func (u *UserUpsertBulk) UpdateTotalRecharged() *UserUpsertBulk {
return u.Update(func(s *UserUpsert) {
s.UpdateTotalRecharged()
})
}
// Exec executes the query.
func (u *UserUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {