feat(quota): add quota task creation and querying endpoints with updated data structures

This commit is contained in:
Chang lue Tsen
2025-09-09 13:39:05 -04:00
parent f4c6bd919b
commit d1be5febc3
18 changed files with 610 additions and 65 deletions
+136 -21
View File
@@ -1,27 +1,142 @@
package task
import "time"
import (
"encoding/json"
"time"
)
type EmailTask struct {
Id int64 `gorm:"column:id;primaryKey;autoIncrement;comment:ID"`
Subject string `gorm:"column:subject;type:varchar(255);not null;comment:Email Subject"`
Content string `gorm:"column:content;type:text;not null;comment:Email Content"`
Recipients string `gorm:"column:recipient;type:text;not null;comment:Email Recipient"`
Scope string `gorm:"column:scope;type:varchar(50);not null;comment:Email Scope"`
RegisterStartTime time.Time `gorm:"column:register_start_time;default:null;comment:Register Start Time"`
RegisterEndTime time.Time `gorm:"column:register_end_time;default:null;comment:Register End Time"`
Additional string `gorm:"column:additional;type:text;default:null;comment:Additional Information"`
Scheduled time.Time `gorm:"column:scheduled;not null;comment:Scheduled Time"`
Interval uint8 `gorm:"column:interval;not null;comment:Interval in Seconds"`
Limit uint64 `gorm:"column:limit;not null;comment:Daily send limit"`
Status uint8 `gorm:"column:status;not null;comment:Daily Status"`
Errors string `gorm:"column:errors;type:text;not null;comment:Errors"`
Total uint64 `gorm:"column:total;not null;default:0;comment:Total Number"`
Current uint64 `gorm:"column:current;not null;default:0;comment:Current Number"`
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
type Type int8
const (
Undefined Type = -1
TypeEmail = iota
TypeQuota
)
type Task struct {
Id int64 `gorm:"primaryKey;autoIncrement;comment:ID"`
Type int8 `gorm:"not null;comment:Task Type"`
Scope string `gorm:"type:text;comment:Task Scope"`
Content string `gorm:"type:text;comment:Task Content"`
Status int8 `gorm:"not null;default:0;comment:Task Status: 0: Pending, 1: In Progress, 2: Completed, 3: Failed"`
Errors string `gorm:"type:text;comment:Task Errors"`
Total uint64 `gorm:"column:total;not null;default:0;comment:Total Number"`
Current uint64 `gorm:"column:current;not null;default:0;comment:Current Number"`
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (EmailTask) TableName() string {
return "email_task"
func (Task) TableName() string {
return "task"
}
type ScopeType int8
const (
ScopeAll ScopeType = iota + 1 // All users
ScopeActive // Active users
ScopeExpired // Expired users
ScopeNone // No Subscribe
ScopeSkip // Skip user filtering
)
func (t ScopeType) Int8() int8 {
return int8(t)
}
type EmailScope struct {
Type int8 `gorm:"not null;comment:Scope Type"`
RegisterStartTime int64 `json:"register_start_time"`
RegisterEndTime int64 `json:"register_end_time"`
Recipients []string `json:"recipients"` // list of email addresses
Additional []string `json:"additional"` // additional email addresses
Scheduled int64 `json:"scheduled"` // scheduled time (unix timestamp)
Interval uint8 `json:"interval"` // interval in seconds
Limit uint64 `json:"limit"` // daily send limit
}
func (s *EmailScope) Marshal() ([]byte, error) {
type Alias EmailScope
return json.Marshal(&struct {
*Alias
}{
Alias: (*Alias)(s),
})
}
func (s *EmailScope) Unmarshal(data []byte) error {
type Alias EmailScope
aux := (*Alias)(s)
return json.Unmarshal(data, &aux)
}
type EmailContent struct {
Subject string `json:"subject"`
Content string `json:"content"`
}
func (c *EmailContent) Marshal() ([]byte, error) {
type Alias EmailContent
return json.Marshal(&struct {
*Alias
}{
Alias: (*Alias)(c),
})
}
func (c *EmailContent) Unmarshal(data []byte) error {
type Alias EmailContent
aux := (*Alias)(c)
return json.Unmarshal(data, &aux)
}
type QuotaScope struct {
Type int8 `gorm:"not null;comment:Scope Type"`
RegisterStartTime int64 `json:"register_start_time"`
RegisterEndTime int64 `json:"register_end_time"`
Recipients []int64 `json:"recipients"` // list of user subs IDs
}
func (s *QuotaScope) Marshal() ([]byte, error) {
type Alias QuotaScope
return json.Marshal(&struct {
*Alias
}{
Alias: (*Alias)(s),
})
}
func (s *QuotaScope) Unmarshal(data []byte) error {
type Alias QuotaScope
aux := (*Alias)(s)
return json.Unmarshal(data, &aux)
}
type QuotaType int8
const (
QuotaTypeReset QuotaType = iota + 1 // Reset Subscribe Quota
QuotaTypeDays // Add Subscribe Days
QuotaTypeGift // Add Gift Amount
)
type QuotaContent struct {
Type int8 `json:"type"`
Days uint64 `json:"days,omitempty"` // days to add
Gift uint8 `json:"gift,omitempty"` // Invoice amount ratio(%) to gift amount
}
func ParseScopeType(t int8) ScopeType {
switch t {
case 1:
return ScopeAll
case 2:
return ScopeActive
case 3:
return ScopeExpired
case 4:
return ScopeNone
default:
return ScopeSkip
}
}