zitadel/internal/eventstore/v2/event_base.go

89 lines
2.2 KiB
Go
Raw Normal View History

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"
2020-11-06 12:47:27 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
type BaseEvent struct {
aggregateID string `json:"-"`
aggregateType AggregateType `json:"-"`
EventType EventType `json:"-"`
resourceOwner string `json:"-"`
aggregateVersion Version `json:"-"`
sequence uint64 `json:"-"`
previouseSequence uint64 `json:"-"`
creationDate time.Time `json:"-"`
//User is the user who created the event
User string `json:"-"`
//Service is the service which created the event
Service string `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
}
func (e *BaseEvent) AggregateID() string {
return e.aggregateID
}
func (e *BaseEvent) AggregateType() AggregateType {
return e.aggregateType
}
func (e *BaseEvent) ResourceOwner() string {
return e.resourceOwner
}
func (e *BaseEvent) AggregateVersion() Version {
return e.aggregateVersion
}
func (e *BaseEvent) Sequence() uint64 {
return e.sequence
}
func (e *BaseEvent) PreviousSequence() uint64 {
return e.previouseSequence
}
func (e *BaseEvent) CreationDate() time.Time {
return e.creationDate
}
func BaseEventFromRepo(event *repository.Event) *BaseEvent {
return &BaseEvent{
aggregateID: event.AggregateID,
aggregateType: AggregateType(event.AggregateType),
aggregateVersion: Version(event.Version),
EventType: EventType(event.Type),
creationDate: event.CreationDate,
sequence: event.Sequence,
previouseSequence: event.PreviousSequence,
resourceOwner: event.ResourceOwner,
Service: event.EditorService,
User: event.EditorUser,
}
}
2020-11-06 21:09:19 +00:00
func NewBaseEventForPush(ctx context.Context, typ EventType) *BaseEvent {
svcName := service.FromContext(ctx)
2020-11-06 16:25:07 +00:00
return &BaseEvent{
User: authz.GetCtxData(ctx).UserID,
2020-11-06 21:09:19 +00:00
Service: svcName,
2020-11-06 16:25:07 +00:00
EventType: typ,
}
}