2022-03-28 08:05:09 +00:00
|
|
|
package preparation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Validation of the input values of the command and if correct returns
|
|
|
|
// the function to create commands or if not valid an error
|
|
|
|
type Validation func() (CreateCommands, error)
|
|
|
|
|
|
|
|
// CreateCommands builds the commands
|
|
|
|
// the filter param is an extended version of the eventstore filter method
|
|
|
|
// it filters for events including the commands on the current context
|
|
|
|
type CreateCommands func(context.Context, FilterToQueryReducer) ([]eventstore.Command, error)
|
|
|
|
|
|
|
|
// FilterToQueryReducer is an abstraction of the eventstore method
|
|
|
|
type FilterToQueryReducer func(ctx context.Context, queryFactory *eventstore.SearchQueryBuilder) ([]eventstore.Event, error)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//ErrNotExecutable is thrown if no command creator was created
|
2023-12-08 14:30:55 +00:00
|
|
|
ErrNotExecutable = zerrors.ThrowInvalidArgument(nil, "PREPA-pH70n", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrepareCommands checks the passed validations and if ok creates the commands
|
2023-10-19 15:21:31 +00:00
|
|
|
//
|
|
|
|
// Deprecated: filter causes unneeded allocation. Use [eventstore.FilterToQueryReducer] instead.
|
2022-03-28 08:05:09 +00:00
|
|
|
func PrepareCommands(ctx context.Context, filter FilterToQueryReducer, validations ...Validation) (cmds []eventstore.Command, err error) {
|
|
|
|
commanders, err := validate(validations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return create(ctx, filter, commanders)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate(validations []Validation) ([]CreateCommands, error) {
|
|
|
|
creators := make([]CreateCommands, 0, len(validations))
|
|
|
|
|
|
|
|
for _, validate := range validations {
|
|
|
|
cmds, err := validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
creators = append(creators, cmds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(creators) == 0 {
|
|
|
|
return nil, ErrNotExecutable
|
|
|
|
}
|
|
|
|
return creators, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func create(ctx context.Context, filter FilterToQueryReducer, commanders []CreateCommands) (cmds []eventstore.Command, err error) {
|
|
|
|
for _, command := range commanders {
|
|
|
|
cmd, err := command(ctx, transactionFilter(filter, cmds))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmds = append(cmds, cmd...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func transactionFilter(filter FilterToQueryReducer, commands []eventstore.Command) FilterToQueryReducer {
|
|
|
|
return func(ctx context.Context, query *eventstore.SearchQueryBuilder) ([]eventstore.Event, error) {
|
|
|
|
events, err := filter(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-01 12:25:41 +00:00
|
|
|
matches := query.Matches(commands...)
|
|
|
|
for _, command := range matches {
|
|
|
|
events = append(events, command.(eventstore.Event))
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
}
|