zitadel/internal/eventstore/event_base.go
Fabi c0f55e7209
feat: pub sub (#1341)
* fix: pub sub

* fix: adaot config to commands (and queries)

* remove dependency on vv2 in v1

* fix: pub sub in new eventstore

* fix tests

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2021-02-24 13:24:33 +01:00

92 lines
2.4 KiB
Go

package eventstore
import (
"context"
"time"
"github.com/caos/zitadel/internal/api/authz"
"github.com/caos/zitadel/internal/api/service"
"github.com/caos/zitadel/internal/eventstore/repository"
)
//BaseEvent represents the minimum metadata of an event
type BaseEvent struct {
EventType EventType
aggregate Aggregate
sequence uint64
creationDate time.Time
//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:"-"`
}
// 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
func (e *BaseEvent) Type() EventType {
return e.EventType
}
//Sequence is an upcounting unique number of the event
func (e *BaseEvent) Sequence() uint64 {
return e.sequence
}
//CreationDate is the the time, the event is inserted into the eventstore
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
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,
}
}
//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 {
return &BaseEvent{
aggregate: *aggregate,
User: authz.GetCtxData(ctx).UserID,
Service: service.FromContext(ctx),
EventType: typ,
}
}