2020-11-06 13:47:27 +01:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
2020-11-06 17:25:07 +01:00
|
|
|
"context"
|
2020-11-06 13:47:27 +01:00
|
|
|
"time"
|
|
|
|
|
2020-11-06 17:25:07 +01:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-11-06 22:09:19 +01:00
|
|
|
"github.com/caos/zitadel/internal/api/service"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-11-06 13:47:27 +01:00
|
|
|
)
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
//BaseEvent represents the minimum metadata of an event
|
2020-11-06 13:47:27 +01:00
|
|
|
type BaseEvent struct {
|
2021-02-18 14:48:27 +01:00
|
|
|
EventType EventType
|
2020-11-06 13:47:27 +01:00
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate Aggregate
|
|
|
|
|
|
|
|
sequence uint64
|
|
|
|
creationDate time.Time
|
2020-11-06 13:47:27 +01: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 13:24:33 +01:00
|
|
|
Data []byte `json:"-"`
|
2020-11-06 13:47:27 +01: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 14:48:27 +01:00
|
|
|
//Type implements EventPusher
|
2020-11-06 13:47:27 +01:00
|
|
|
func (e *BaseEvent) Type() EventType {
|
|
|
|
return e.EventType
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
//Sequence is an upcounting unique number of the event
|
2020-11-06 13:47:27 +01:00
|
|
|
func (e *BaseEvent) Sequence() uint64 {
|
|
|
|
return e.sequence
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
|
|
|
|
//CreationDate is the the time, the event is inserted into the eventstore
|
2020-11-06 13:47:27 +01:00
|
|
|
func (e *BaseEvent) CreationDate() time.Time {
|
|
|
|
return e.creationDate
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
//Aggregate represents the metadata of the event's aggregate
|
|
|
|
func (e *BaseEvent) Aggregate() Aggregate {
|
|
|
|
return e.aggregate
|
|
|
|
}
|
|
|
|
|
2021-02-24 13:24:33 +01: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 14:48:27 +01:00
|
|
|
//BaseEventFromRepo maps a stored event to a BaseEvent
|
2020-11-06 13:47:27 +01:00
|
|
|
func BaseEventFromRepo(event *repository.Event) *BaseEvent {
|
|
|
|
return &BaseEvent{
|
2021-02-18 14:48:27 +01: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 13:24:33 +01:00
|
|
|
Data: event.Data,
|
2020-11-06 13:47:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01: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 17:25:07 +01:00
|
|
|
return &BaseEvent{
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate: *aggregate,
|
2020-11-06 17:25:07 +01:00
|
|
|
User: authz.GetCtxData(ctx).UserID,
|
2021-02-18 14:48:27 +01:00
|
|
|
Service: service.FromContext(ctx),
|
2020-11-06 17:25:07 +01:00
|
|
|
EventType: typ,
|
|
|
|
}
|
|
|
|
}
|