2020-09-24 06:52:10 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-09-30 08:00:05 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
2020-09-24 06:52:10 +00:00
|
|
|
)
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
//SearchQueryBuilder represents the builder for your filter
|
2020-10-06 19:28:09 +00:00
|
|
|
// if invalid data are set the filter will fail
|
2020-12-01 13:44:19 +00:00
|
|
|
type SearchQueryBuilder struct {
|
2020-09-30 08:00:05 +00:00
|
|
|
columns repository.Columns
|
2020-09-24 06:52:10 +00:00
|
|
|
limit uint64
|
|
|
|
desc bool
|
|
|
|
aggregateTypes []AggregateType
|
|
|
|
aggregateIDs []string
|
|
|
|
eventSequence uint64
|
|
|
|
eventTypes []EventType
|
2020-11-23 18:31:12 +00:00
|
|
|
eventData map[string]interface{}
|
2020-09-24 06:52:10 +00:00
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
|
2020-10-06 19:28:09 +00:00
|
|
|
// Columns defines which fields of the event are needed for the query
|
2020-09-30 08:00:05 +00:00
|
|
|
type Columns repository.Columns
|
2020-09-24 06:52:10 +00:00
|
|
|
|
|
|
|
const (
|
2020-10-06 19:28:09 +00:00
|
|
|
//ColumnsEvent represents all fields of an event
|
|
|
|
ColumnsEvent Columns = repository.ColumnsEvent
|
|
|
|
// ColumnsMaxSequence represents the latest sequence of the filtered events
|
|
|
|
ColumnsMaxSequence Columns = repository.ColumnsMaxSequence
|
2020-09-24 06:52:10 +00:00
|
|
|
)
|
|
|
|
|
2020-10-06 19:28:09 +00:00
|
|
|
// AggregateType is the object name
|
2020-09-30 08:00:05 +00:00
|
|
|
type AggregateType repository.AggregateType
|
2020-10-06 19:28:09 +00:00
|
|
|
|
|
|
|
// EventType is the description of the change
|
2020-09-30 08:00:05 +00:00
|
|
|
type EventType repository.EventType
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
// NewSearchQueryBuilder creates a new factory for event filters
|
2020-10-06 19:28:09 +00:00
|
|
|
// aggregateTypes must contain at least one aggregate type
|
2020-12-01 13:44:19 +00:00
|
|
|
func NewSearchQueryBuilder(columns Columns, aggregateTypes ...AggregateType) *SearchQueryBuilder {
|
|
|
|
return &SearchQueryBuilder{
|
2020-10-06 19:28:09 +00:00
|
|
|
columns: repository.Columns(columns),
|
2020-09-24 06:52:10 +00:00
|
|
|
aggregateTypes: aggregateTypes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) Columns(columns Columns) *SearchQueryBuilder {
|
2020-09-30 08:00:05 +00:00
|
|
|
factory.columns = repository.Columns(columns)
|
2020-09-24 06:52:10 +00:00
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) Limit(limit uint64) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.limit = limit
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) SequenceGreater(sequence uint64) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.eventSequence = sequence
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) AggregateIDs(ids ...string) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.aggregateIDs = ids
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) EventTypes(types ...EventType) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.eventTypes = types
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) ResourceOwner(resourceOwner string) *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.resourceOwner = resourceOwner
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) OrderDesc() *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.desc = true
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) OrderAsc() *SearchQueryBuilder {
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.desc = false
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) EventData(query map[string]interface{}) *SearchQueryBuilder {
|
2020-11-23 18:31:12 +00:00
|
|
|
factory.eventData = query
|
|
|
|
return factory
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) build() (*repository.SearchQuery, error) {
|
2020-09-24 06:52:10 +00:00
|
|
|
if factory == nil ||
|
|
|
|
len(factory.aggregateTypes) < 1 ||
|
2020-10-06 19:28:09 +00:00
|
|
|
factory.columns.Validate() != nil {
|
2020-09-24 06:52:10 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "MODEL-tGAD3", "factory invalid")
|
|
|
|
}
|
2020-09-30 08:00:05 +00:00
|
|
|
filters := []*repository.Filter{
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.aggregateTypeFilter(),
|
|
|
|
}
|
|
|
|
|
2020-09-30 08:00:05 +00:00
|
|
|
for _, f := range []func() *repository.Filter{
|
2020-09-24 06:52:10 +00:00
|
|
|
factory.aggregateIDFilter,
|
|
|
|
factory.eventSequenceFilter,
|
|
|
|
factory.eventTypeFilter,
|
|
|
|
factory.resourceOwnerFilter,
|
2020-11-23 18:31:12 +00:00
|
|
|
factory.eventDataFilter,
|
2020-09-24 06:52:10 +00:00
|
|
|
} {
|
|
|
|
if filter := f(); filter != nil {
|
2020-10-06 19:28:09 +00:00
|
|
|
if err := filter.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-24 06:52:10 +00:00
|
|
|
filters = append(filters, filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 08:00:05 +00:00
|
|
|
return &repository.SearchQuery{
|
|
|
|
Columns: repository.Columns(factory.columns),
|
2020-09-24 06:52:10 +00:00
|
|
|
Limit: factory.limit,
|
|
|
|
Desc: factory.desc,
|
|
|
|
Filters: filters,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) aggregateIDFilter() *repository.Filter {
|
2020-09-24 06:52:10 +00:00
|
|
|
if len(factory.aggregateIDs) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(factory.aggregateIDs) == 1 {
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldAggregateID, factory.aggregateIDs[0], repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldAggregateID, factory.aggregateIDs, repository.OperationIn)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) eventTypeFilter() *repository.Filter {
|
2020-09-24 06:52:10 +00:00
|
|
|
if len(factory.eventTypes) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(factory.eventTypes) == 1 {
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldEventType, factory.eventTypes[0], repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldEventType, factory.eventTypes, repository.OperationIn)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) aggregateTypeFilter() *repository.Filter {
|
2020-09-24 06:52:10 +00:00
|
|
|
if len(factory.aggregateTypes) == 1 {
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldAggregateType, factory.aggregateTypes[0], repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldAggregateType, factory.aggregateTypes, repository.OperationIn)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) eventSequenceFilter() *repository.Filter {
|
2020-09-24 06:52:10 +00:00
|
|
|
if factory.eventSequence == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
sortOrder := repository.OperationGreater
|
2020-09-24 06:52:10 +00:00
|
|
|
if factory.desc {
|
2020-10-06 19:28:09 +00:00
|
|
|
sortOrder = repository.OperationLess
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldSequence, factory.eventSequence, sortOrder)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) resourceOwnerFilter() *repository.Filter {
|
2020-09-24 06:52:10 +00:00
|
|
|
if factory.resourceOwner == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-06 19:28:09 +00:00
|
|
|
return repository.NewFilter(repository.FieldResourceOwner, factory.resourceOwner, repository.OperationEquals)
|
2020-09-24 06:52:10 +00:00
|
|
|
}
|
2020-11-23 18:31:12 +00:00
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (factory *SearchQueryBuilder) eventDataFilter() *repository.Filter {
|
2020-11-23 18:31:12 +00:00
|
|
|
if len(factory.eventData) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return repository.NewFilter(repository.FieldEventData, factory.eventData, repository.OperationJSONContains)
|
|
|
|
}
|