From ec3076c3c86e5b87c2118e39e2e63e1ef0663037 Mon Sep 17 00:00:00 2001 From: Silvan Date: Tue, 5 Mar 2024 16:44:51 +0100 Subject: [PATCH] fix(ListEvents): add aggregate types to filter if not set (#7490) --- internal/api/grpc/admin/event.go | 18 +++++++++ internal/api/grpc/admin/event_test.go | 58 +++++++++++++++++++++++++++ internal/eventstore/eventstore.go | 7 ++++ 3 files changed, 83 insertions(+) create mode 100644 internal/api/grpc/admin/event_test.go diff --git a/internal/api/grpc/admin/event.go b/internal/api/grpc/admin/event.go index 576fce8af1..79a5aae487 100644 --- a/internal/api/grpc/admin/event.go +++ b/internal/api/grpc/admin/event.go @@ -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 +} diff --git a/internal/api/grpc/admin/event_test.go b/internal/api/grpc/admin/event_test.go new file mode 100644 index 0000000000..926d48ffe8 --- /dev/null +++ b/internal/api/grpc/admin/event_test.go @@ -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) + } + }) + } +} diff --git a/internal/eventstore/eventstore.go b/internal/eventstore/eventstore.go index 17b570ab64..2602683cca 100644 --- a/internal/eventstore/eventstore.go +++ b/internal/eventstore/eventstore.go @@ -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 }