mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package promotionrule
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
"github.com/hanzoai/orm"
|
|
)
|
|
|
|
func init() { orm.Register[PromotionRule]("promotionrule") }
|
|
|
|
type PromotionRule struct {
|
|
mixin.Model[PromotionRule]
|
|
|
|
PromotionId string `json:"promotionId"`
|
|
Attribute string `json:"attribute"`
|
|
Operator string `json:"operator"`
|
|
|
|
// Values stored as JSON-encoded string in datastore
|
|
Values []string `json:"values" datastore:"-"`
|
|
Values_ string `json:"-" datastore:",noindex"`
|
|
}
|
|
|
|
func (p *PromotionRule) Load(ps []datastore.Property) (err error) {
|
|
if err = datastore.LoadStruct(p, ps); err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(p.Values_) > 0 {
|
|
err = json.DecodeBytes([]byte(p.Values_), &p.Values)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (p *PromotionRule) Save() ([]datastore.Property, error) {
|
|
p.Values_ = string(json.EncodeBytes(&p.Values))
|
|
|
|
return datastore.SaveStruct(p)
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *PromotionRule {
|
|
p := new(PromotionRule)
|
|
p.Init(db)
|
|
return p
|
|
}
|
|
|
|
func Query(db *datastore.Datastore) datastore.Query {
|
|
return db.Query("promotionrule")
|
|
}
|