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
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package usagewatermark
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/util/val"
|
|
"github.com/hanzoai/orm"
|
|
)
|
|
|
|
func init() { orm.Register[UsageWatermark]("usage-watermark") }
|
|
|
|
// UsageWatermark records the last aggregated position for a subscription item
|
|
// within an invoice period. This prevents double-invoicing of usage events
|
|
// and supports late-arriving event handling.
|
|
type UsageWatermark struct {
|
|
mixin.Model[UsageWatermark]
|
|
|
|
SubscriptionItemId string `json:"subscriptionItemId"`
|
|
MeterId string `json:"meterId"`
|
|
InvoiceId string `json:"invoiceId"`
|
|
|
|
PeriodStart time.Time `json:"periodStart"`
|
|
PeriodEnd time.Time `json:"periodEnd"`
|
|
|
|
// Usage value that was aggregated into the invoice
|
|
AggregatedValue int64 `json:"aggregatedValue"`
|
|
|
|
// Number of events included in the aggregation
|
|
EventCount int64 `json:"eventCount"`
|
|
|
|
// Latest event timestamp included in this watermark
|
|
LastEventTimestamp time.Time `json:"lastEventTimestamp"`
|
|
}
|
|
|
|
func (w *UsageWatermark) Load(ps []datastore.Property) (err error) {
|
|
return datastore.LoadStruct(w, ps)
|
|
}
|
|
|
|
func (w *UsageWatermark) Save() (ps []datastore.Property, err error) {
|
|
return datastore.SaveStruct(w)
|
|
}
|
|
|
|
func (w *UsageWatermark) Validator() *val.Validator {
|
|
return nil
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *UsageWatermark {
|
|
w := new(UsageWatermark)
|
|
w.Init(db)
|
|
w.Parent = db.NewKey("synckey", "", 1, nil)
|
|
return w
|
|
}
|
|
|
|
func Query(db *datastore.Datastore) datastore.Query {
|
|
return db.Query("usage-watermark")
|
|
}
|