zitadel/internal/query/event.go
Silvan 1bf1f335dc
feat(admin-api): list events (#4989)
* docs: update cockroachdb version to 22.2
* feat(adminAPI): ListEventTypes returns the list of event types ZITADEL implements
* feat(adminAPI): ListAggregateTypes returns the list of aggregate types ZITADEL implements
* feat(adminAPI): ListEvents allows `IAM_OWNERS` to search for events
2023-01-16 11:30:03 +00:00

74 lines
1.7 KiB
Go

package query
import (
"context"
"time"
"github.com/zitadel/zitadel/internal/eventstore"
)
type Event struct {
Editor *EventEditor
Aggregate eventstore.Aggregate
Sequence uint64
CreationDate time.Time
Type string
Payload []byte
}
type EventEditor struct {
ID string
DisplayName string
Service string
}
func (q *Queries) SearchEvents(ctx context.Context, query *eventstore.SearchQueryBuilder) ([]*Event, error) {
events, err := q.eventstore.Filter(ctx, query)
if err != nil {
return nil, err
}
return q.convertEvents(ctx, events), nil
}
func (q *Queries) SearchEventTypes(ctx context.Context) []string {
return q.eventstore.EventTypes()
}
func (q *Queries) SearchAggregateTypes(ctx context.Context) []string {
return q.eventstore.AggregateTypes()
}
func (q *Queries) convertEvents(ctx context.Context, events []eventstore.Event) []*Event {
result := make([]*Event, len(events))
for i, event := range events {
result[i] = q.convertEvent(ctx, event)
}
return result
}
func (q *Queries) convertEvent(ctx context.Context, event eventstore.Event) *Event {
displayName := event.EditorUser()
user, err := q.GetUserByID(ctx, false, event.EditorUser(), false)
if err == nil {
if user.Human != nil {
displayName = user.Human.DisplayName
} else if user.Machine != nil {
displayName = user.Machine.Name
}
}
return &Event{
Editor: &EventEditor{
ID: event.EditorUser(),
Service: event.EditorService(),
DisplayName: displayName,
},
Aggregate: event.Aggregate(),
Sequence: event.Sequence(),
CreationDate: event.CreationDate(),
Type: string(event.Type()),
Payload: event.DataAsBytes(),
}
}