2020-04-06 04:42:21 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AggregateType string
|
|
|
|
|
|
|
|
func (at AggregateType) String() string {
|
|
|
|
return string(at)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Aggregates []*Aggregate
|
|
|
|
|
|
|
|
type Aggregate struct {
|
2020-05-13 12:22:29 +00:00
|
|
|
ID string
|
2020-04-28 14:01:00 +00:00
|
|
|
typ AggregateType
|
|
|
|
PreviousSequence uint64
|
|
|
|
version Version
|
2020-04-06 04:42:21 +00:00
|
|
|
|
|
|
|
editorService string
|
|
|
|
editorUser string
|
|
|
|
resourceOwner string
|
|
|
|
Events []*Event
|
2020-04-28 14:01:00 +00:00
|
|
|
Precondition *precondition
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (a *Aggregate) Type() AggregateType {
|
|
|
|
return a.typ
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:01:00 +00:00
|
|
|
type precondition struct {
|
|
|
|
Query *SearchQuery
|
|
|
|
Validation func(...*Event) error
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Aggregate) AppendEvent(typ EventType, payload interface{}) (*Aggregate, error) {
|
|
|
|
if string(typ) == "" {
|
|
|
|
return a, errors.ThrowInvalidArgument(nil, "MODEL-TGoCb", "no event type")
|
|
|
|
}
|
|
|
|
data, err := eventData(payload)
|
|
|
|
if err != nil {
|
|
|
|
return a, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &Event{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
Data: data,
|
|
|
|
Type: typ,
|
2020-05-13 12:22:29 +00:00
|
|
|
AggregateID: a.ID,
|
2020-04-06 04:42:21 +00:00
|
|
|
AggregateType: a.typ,
|
|
|
|
AggregateVersion: a.version,
|
|
|
|
EditorService: a.editorService,
|
|
|
|
EditorUser: a.editorUser,
|
|
|
|
ResourceOwner: a.resourceOwner,
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Events = append(a.Events, e)
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:01:00 +00:00
|
|
|
func (a *Aggregate) SetPrecondition(query *SearchQuery, validateFunc func(...*Event) error) *Aggregate {
|
|
|
|
a.Precondition = &precondition{Query: query, Validation: validateFunc}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-04-06 04:42:21 +00:00
|
|
|
func (a *Aggregate) Validate() error {
|
|
|
|
if a == nil {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-yi5AC", "aggregate is nil")
|
|
|
|
}
|
2020-05-13 12:22:29 +00:00
|
|
|
if a.ID == "" {
|
2020-04-06 04:42:21 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-FSjKV", "id not set")
|
|
|
|
}
|
|
|
|
if string(a.typ) == "" {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-aj4t2", "type not set")
|
|
|
|
}
|
|
|
|
if err := a.version.Validate(); err != nil {
|
|
|
|
return errors.ThrowPreconditionFailed(err, "MODEL-PupjX", "invalid version")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.editorService == "" {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-clYbY", "editor service not set")
|
|
|
|
}
|
|
|
|
if a.editorUser == "" {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-Xcssi", "editor user not set")
|
|
|
|
}
|
|
|
|
if a.resourceOwner == "" {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-eBYUW", "resource owner not set")
|
|
|
|
}
|
2020-06-15 12:57:19 +00:00
|
|
|
if a.Precondition != nil && (a.Precondition.Query == nil || a.Precondition.Validation == nil) {
|
2020-04-28 14:01:00 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "MODEL-EEUvA", "invalid precondition")
|
|
|
|
}
|
2020-04-06 04:42:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|