zitadel/internal/eventstore/event_base.go

92 lines
2.4 KiB
Go
Raw Normal View History

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"
"github.com/caos/zitadel/internal/eventstore/repository"
2020-11-06 13:47:27 +01:00
)
//BaseEvent represents the minimum metadata of an event
2020-11-06 13:47:27 +01:00
type BaseEvent struct {
EventType EventType
2020-11-06 13:47: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:"-"`
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
}
//Type implements EventPusher
2020-11-06 13:47:27 +01:00
func (e *BaseEvent) Type() EventType {
return e.EventType
}
//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
}
//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
}
//Aggregate represents the metadata of the event's aggregate
func (e *BaseEvent) Aggregate() Aggregate {
return e.aggregate
}
//Data returns the payload of the event. It represent the changed fields by the event
func (e *BaseEvent) DataAsBytes() []byte {
return e.Data
}
//BaseEventFromRepo maps a stored event to a BaseEvent
2020-11-06 13:47:27 +01:00
func BaseEventFromRepo(event *repository.Event) *BaseEvent {
return &BaseEvent{
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,
Data: event.Data,
2020-11-06 13:47: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{
aggregate: *aggregate,
2020-11-06 17:25:07 +01:00
User: authz.GetCtxData(ctx).UserID,
Service: service.FromContext(ctx),
2020-11-06 17:25:07 +01:00
EventType: typ,
}
}