fix(ListEvents): add aggregate types to filter if not set (#7490)

This commit is contained in:
Silvan 2024-03-05 16:44:51 +01:00 committed by GitHub
parent dfcc26de1e
commit ec3076c3c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 83 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package admin
import (
"context"
"slices"
"time"
"github.com/zitadel/zitadel/internal/api/authz"
@ -60,14 +61,21 @@ func eventRequestToFilter(ctx context.Context, req *admin_pb.ListEventsRequest)
for i, eventType := range req.EventTypes {
eventTypes[i] = eventstore.EventType(eventType)
}
aggregateIDs := make([]string, 0, 1)
if req.AggregateId != "" {
aggregateIDs = append(aggregateIDs, req.AggregateId)
}
aggregateTypes := make([]eventstore.AggregateType, len(req.AggregateTypes))
for i, aggregateType := range req.AggregateTypes {
aggregateTypes[i] = eventstore.AggregateType(aggregateType)
}
if len(aggregateTypes) == 0 {
aggregateTypes = aggregateTypesFromEventTypes(eventTypes)
}
aggregateTypes = slices.Compact(aggregateTypes)
limit := uint64(req.Limit)
if limit == 0 || limit > maxLimit {
limit = maxLimit
@ -100,3 +108,13 @@ func eventRequestToFilter(ctx context.Context, req *admin_pb.ListEventsRequest)
}
return builder, nil
}
func aggregateTypesFromEventTypes(eventTypes []eventstore.EventType) []eventstore.AggregateType {
aggregateTypes := make([]eventstore.AggregateType, 0, len(eventTypes))
for _, eventType := range eventTypes {
aggregateTypes = append(aggregateTypes, eventstore.AggregateTypeFromEventType(eventType))
}
return aggregateTypes
}

View File

@ -0,0 +1,58 @@
package admin
import (
"reflect"
"testing"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/deviceauth"
"github.com/zitadel/zitadel/internal/repository/org"
"github.com/zitadel/zitadel/internal/repository/user"
)
func Test_aggregateTypesFromEventTypes(t *testing.T) {
type args struct {
eventTypes []eventstore.EventType
}
tests := []struct {
name string
args args
want []eventstore.AggregateType
}{
{
name: "no event types",
args: args{
eventTypes: []eventstore.EventType{},
},
want: []eventstore.AggregateType{},
},
{
name: "only by prefix",
args: args{
eventTypes: []eventstore.EventType{user.MachineAddedEventType, org.OrgAddedEventType},
},
want: []eventstore.AggregateType{user.AggregateType, org.AggregateType},
},
{
name: "with special",
args: args{
eventTypes: []eventstore.EventType{deviceauth.ApprovedEventType, org.OrgAddedEventType},
},
want: []eventstore.AggregateType{deviceauth.AggregateType, org.AggregateType},
},
{
name: "duplicates",
args: args{
eventTypes: []eventstore.EventType{org.OrgAddedEventType, org.OrgChangedEventType},
},
want: []eventstore.AggregateType{org.AggregateType, org.AggregateType},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := aggregateTypesFromEventTypes(tt.args.eventTypes); !reflect.DeepEqual(got, tt.want) {
t.Errorf("aggregateTypesFromEventTypes() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -31,6 +31,7 @@ var (
eventInterceptors map[EventType]eventTypeInterceptors
eventTypes []string
aggregateTypes []string
eventTypeMapping = map[EventType]AggregateType{}
)
// RegisterFilterEventMapper registers a function for mapping an eventstore event to an event
@ -45,9 +46,11 @@ func RegisterFilterEventMapper(aggregateType AggregateType, eventType EventType,
if eventInterceptors == nil {
eventInterceptors = make(map[EventType]eventTypeInterceptors)
}
interceptor := eventInterceptors[eventType]
interceptor.eventMapper = mapper
eventInterceptors[eventType] = interceptor
eventTypeMapping[eventType] = aggregateType
}
type eventTypeInterceptors struct {
@ -112,6 +115,10 @@ retry:
return mappedEvents, nil
}
func AggregateTypeFromEventType(typ EventType) AggregateType {
return eventTypeMapping[typ]
}
func (es *Eventstore) EventTypes() []string {
return eventTypes
}