package eventsourcing import ( "context" "github.com/caos/zitadel/internal/errors" es_models "github.com/caos/zitadel/internal/eventstore/models" iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model" "github.com/caos/zitadel/internal/org/repository/eventsourcing/model" ) func LabelPolicyAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Org, policy *iam_es_model.LabelPolicy) func(ctx context.Context) (*es_models.Aggregate, error) { return func(ctx context.Context) (*es_models.Aggregate, error) { if policy == nil { return nil, errors.ThrowPreconditionFailed(nil, "EVENT-TUWod", "Errors.Internal") } agg, err := OrgAggregate(ctx, aggCreator, existing.AggregateID, existing.Sequence) if err != nil { return nil, err } validationQuery := es_models.NewSearchQuery(). AggregateTypeFilter(model.OrgAggregate). AggregateIDFilter(existing.AggregateID) validation := checkExistingLabelPolicyValidation() agg.SetPrecondition(validationQuery, validation) return agg.AppendEvent(model.LabelPolicyAdded, policy) } } func LabelPolicyChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Org, policy *iam_es_model.LabelPolicy) func(ctx context.Context) (*es_models.Aggregate, error) { return func(ctx context.Context) (*es_models.Aggregate, error) { if policy == nil { return nil, errors.ThrowPreconditionFailed(nil, "EVENT-unRI2", "Errors.Internal") } agg, err := OrgAggregate(ctx, aggCreator, existing.AggregateID, existing.Sequence) if err != nil { return nil, err } changes := existing.LabelPolicy.Changes(policy) if len(changes) == 0 { return nil, errors.ThrowPreconditionFailed(nil, "EVENT-Tz130", "Errors.NoChangesFound") } return agg.AppendEvent(model.LabelPolicyChanged, changes) } } func LabelPolicyRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Org) func(ctx context.Context) (*es_models.Aggregate, error) { return func(ctx context.Context) (*es_models.Aggregate, error) { if existing == nil { return nil, errors.ThrowPreconditionFailed(nil, "EVENT-v7E9b", "Errors.Internal") } agg, err := OrgAggregate(ctx, aggCreator, existing.AggregateID, existing.Sequence) if err != nil { return nil, err } return agg.AppendEvent(model.LabelPolicyRemoved, nil) } } func checkExistingLabelPolicyValidation() func(...*es_models.Event) error { return func(events ...*es_models.Event) error { existing := false for _, event := range events { switch event.Type { case model.LabelPolicyAdded: existing = true case model.LabelPolicyRemoved: existing = false } } if existing { return errors.ThrowPreconditionFailed(nil, "EVENT-g9mCI", "Errors.Org.LabelPolicy.AlreadyExists") } return nil } }