package entities import ( "time" "github.com/google/uuid" "gorm.io/gorm" ) type OtpSession struct { ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"` Token string `gorm:"type:varchar(255);uniqueIndex;not null" json:"token"` Code string `gorm:"type:varchar(10);not null" json:"code"` PhoneNumber string `gorm:"type:varchar(20);not null;index" json:"phone_number"` Purpose string `gorm:"type:varchar(50);not null;index" json:"purpose"` ExpiresAt time.Time `gorm:"not null;index" json:"expires_at"` IsUsed bool `gorm:"default:false;index" json:"is_used"` AttemptsCount int `gorm:"default:0" json:"attempts_count"` MaxAttempts int `gorm:"default:3" json:"max_attempts"` Metadata Metadata `gorm:"type:jsonb;default:'{}'" json:"metadata"` CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"` UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"` } func (o *OtpSession) BeforeCreate(tx *gorm.DB) error { if o.ID == uuid.Nil { o.ID = uuid.New() } return nil } func (OtpSession) TableName() string { return "otp_sessions" } func (o *OtpSession) IsExpired() bool { return time.Now().After(o.ExpiresAt) } func (o *OtpSession) IsMaxAttemptsReached() bool { return o.AttemptsCount >= o.MaxAttempts } func (o *OtpSession) CanBeUsed() bool { return !o.IsUsed && !o.IsExpired() && !o.IsMaxAttemptsReached() } func (o *OtpSession) IncrementAttempts() { o.AttemptsCount++ } func (o *OtpSession) MarkAsUsed() { o.IsUsed = true }