2020-11-06 12:47:27 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
2020-11-06 16:25:07 +00:00
|
|
|
"context"
|
2020-11-06 12:47:27 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-06 16:25:07 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-11-06 21:09:19 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/service"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-11-06 12:47:27 +00:00
|
|
|
)
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//BaseEvent represents the minimum metadata of an event
|
2020-11-06 12:47:27 +00:00
|
|
|
type BaseEvent struct {
|
2021-02-18 13:48:27 +00:00
|
|
|
EventType EventType
|
2020-11-06 12:47:27 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate Aggregate
|
|
|
|
|
|
|
|
sequence uint64
|
|
|
|
creationDate time.Time
|
2020-11-06 12:47:27 +00:00
|
|
|
|
|
|
|
//User is the user who created the event
|
|
|
|
User string `json:"-"`
|
|
|
|
//Service is the service which created the event
|
|
|
|
Service string `json:"-"`
|
2021-02-24 12:24:33 +00:00
|
|
|
Data []byte `json:"-"`
|
2020-11-06 12:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditorService implements EventPusher
|
|
|
|
func (e *BaseEvent) EditorService() string {
|
|
|
|
return e.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
//EditorUser implements EventPusher
|
|
|
|
func (e *BaseEvent) EditorUser() string {
|
|
|
|
return e.User
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//Type implements EventPusher
|
2020-11-06 12:47:27 +00:00
|
|
|
func (e *BaseEvent) Type() EventType {
|
|
|
|
return e.EventType
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//Sequence is an upcounting unique number of the event
|
2020-11-06 12:47:27 +00:00
|
|
|
func (e *BaseEvent) Sequence() uint64 {
|
|
|
|
return e.sequence
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
|
|
|
//CreationDate is the the time, the event is inserted into the eventstore
|
2020-11-06 12:47:27 +00:00
|
|
|
func (e *BaseEvent) CreationDate() time.Time {
|
|
|
|
return e.creationDate
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//Aggregate represents the metadata of the event's aggregate
|
|
|
|
func (e *BaseEvent) Aggregate() Aggregate {
|
|
|
|
return e.aggregate
|
|
|
|
}
|
|
|
|
|
2021-02-24 12:24:33 +00:00
|
|
|
//Data returns the payload of the event. It represent the changed fields by the event
|
|
|
|
func (e *BaseEvent) DataAsBytes() []byte {
|
|
|
|
return e.Data
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//BaseEventFromRepo maps a stored event to a BaseEvent
|
2020-11-06 12:47:27 +00:00
|
|
|
func BaseEventFromRepo(event *repository.Event) *BaseEvent {
|
|
|
|
return &BaseEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate: Aggregate{
|
|
|
|
ID: event.AggregateID,
|
|
|
|
Typ: AggregateType(event.AggregateType),
|
|
|
|
ResourceOwner: event.ResourceOwner,
|
|
|
|
Version: Version(event.Version),
|
|
|
|
},
|
|
|
|
EventType: EventType(event.Type),
|
|
|
|
creationDate: event.CreationDate,
|
|
|
|
sequence: event.Sequence,
|
|
|
|
Service: event.EditorService,
|
|
|
|
User: event.EditorUser,
|
2021-02-24 12:24:33 +00:00
|
|
|
Data: event.Data,
|
2020-11-06 12:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//NewBaseEventForPush is the constructor for event's which will be pushed into the eventstore
|
|
|
|
// the resource owner of the aggregate is only used if it's the first event of this aggregateroot
|
|
|
|
// afterwards the resource owner of the first previous events is taken
|
|
|
|
func NewBaseEventForPush(ctx context.Context, aggregate *Aggregate, typ EventType) *BaseEvent {
|
2020-11-06 16:25:07 +00:00
|
|
|
return &BaseEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate: *aggregate,
|
2020-11-06 16:25:07 +00:00
|
|
|
User: authz.GetCtxData(ctx).UserID,
|
2021-02-18 13:48:27 +00:00
|
|
|
Service: service.FromContext(ctx),
|
2020-11-06 16:25:07 +00:00
|
|
|
EventType: typ,
|
|
|
|
}
|
|
|
|
}
|