2020-04-07 13:23:04 +02:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-21 17:00:32 +02:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2020-04-07 13:23:04 +02:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-04-07 18:36:37 +02:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-04-07 13:23:04 +02:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-04-23 07:54:40 +02:00
|
|
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
2020-04-07 13:23:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func ProjectByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
|
|
|
if id == "" {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dke74", "id should be filled")
|
|
|
|
}
|
|
|
|
return ProjectQuery(latestSequence).
|
|
|
|
AggregateIDFilter(id), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectQuery(latestSequence uint64) *es_models.SearchQuery {
|
|
|
|
return es_models.NewSearchQuery().
|
2020-05-11 12:16:29 +02:00
|
|
|
AggregateTypeFilter(model.ProjectAggregate).
|
2020-04-07 13:23:04 +02:00
|
|
|
LatestSequenceFilter(latestSequence)
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, project *model.Project) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
if project == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-doe93", "existing project should not be nil")
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return aggCreator.NewAggregate(ctx, project.AggregateID, model.ProjectAggregate, model.ProjectVersion, project.Sequence)
|
2020-04-07 13:23:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectCreateAggregate(aggCreator *es_models.AggregateCreator, project *model.Project) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-07 18:36:37 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if project == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-kdie6", "project should not be nil")
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:00:32 +02:00
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, project)
|
2020-04-07 18:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectAdded, project)
|
2020-04-07 13:23:04 +02:00
|
|
|
}
|
2020-04-07 18:36:37 +02:00
|
|
|
}
|
2020-04-07 13:23:04 +02:00
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectUpdateAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, new *model.Project) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-07 18:36:37 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if new == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dhr74", "new project should not be nil")
|
|
|
|
}
|
2020-04-21 17:00:32 +02:00
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
2020-04-07 18:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changes := existing.Changes(new)
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectChanged, changes)
|
2020-04-07 13:23:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectDeactivateAggregate(aggCreator *es_models.AggregateCreator, project *model.Project) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-05-11 12:16:29 +02:00
|
|
|
return projectStateAggregate(aggCreator, project, model.ProjectDeactivated)
|
2020-04-07 13:23:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectReactivateAggregate(aggCreator *es_models.AggregateCreator, project *model.Project) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-05-11 12:16:29 +02:00
|
|
|
return projectStateAggregate(aggCreator, project, model.ProjectReactivated)
|
2020-04-07 13:23:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func projectStateAggregate(aggCreator *es_models.AggregateCreator, project *model.Project, state models.EventType) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-07 18:36:37 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, project)
|
2020-04-07 18:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(state, nil)
|
2020-04-07 13:23:04 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 17:11:42 +02:00
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectMemberAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-15 17:11:42 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
if member == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-ie34f", "member should not be nil")
|
2020-04-15 17:11:42 +02:00
|
|
|
}
|
2020-04-21 17:00:32 +02:00
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
2020-04-15 17:11:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectMemberAdded, member)
|
2020-04-15 17:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectMemberChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-15 17:11:42 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
if member == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d34fs", "member should not be nil")
|
2020-04-15 17:11:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 17:00:32 +02:00
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
2020-04-15 17:11:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectMemberChanged, member)
|
2020-04-15 17:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectMemberRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-15 17:11:42 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
if member == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dieu7", "member should not be nil")
|
2020-04-15 17:11:42 +02:00
|
|
|
}
|
2020-04-21 17:00:32 +02:00
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
2020-04-15 17:11:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectMemberRemoved, member)
|
2020-04-15 17:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 17:00:32 +02:00
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectRoleAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, role *model.ProjectRole) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if role == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sleo9", "role should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectRoleAdded, role)
|
2020-04-21 17:00:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectRoleChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, role *model.ProjectRole) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if role == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-oe8sf", "member should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectRoleChanged, role)
|
2020-04-21 17:00:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ProjectRoleRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, role *model.ProjectRole) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if role == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8eis", "member should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectRoleRemoved, role)
|
2020-04-21 17:00:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ApplicationAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, app *model.Application) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if app == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-09du7", "app should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ApplicationAdded, app)
|
2020-04-21 17:00:32 +02:00
|
|
|
if app.OIDCConfig != nil {
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.OIDCConfigAdded, app.OIDCConfig)
|
2020-04-21 17:00:32 +02:00
|
|
|
}
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ApplicationChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, app *model.Application) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if app == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sleo9", "app should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var changes map[string]interface{}
|
|
|
|
for _, a := range existing.Applications {
|
|
|
|
if a.AppID == app.AppID {
|
|
|
|
changes = a.Changes(app)
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ApplicationChanged, changes)
|
2020-04-21 17:00:32 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ApplicationRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, app *model.Application) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if app == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-se23g", "app should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ApplicationRemoved, &model.ApplicationID{AppID: app.AppID})
|
2020-04-21 17:00:32 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ApplicationDeactivatedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, app *model.Application) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if app == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-slfi3", "app should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ApplicationDeactivated, &model.ApplicationID{AppID: app.AppID})
|
2020-04-21 17:00:32 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func ApplicationReactivatedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, app *model.Application) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if app == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-slf32", "app should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ApplicationReactivated, &model.ApplicationID{AppID: app.AppID})
|
2020-04-21 17:00:32 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func OIDCConfigChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, config *model.OIDCConfig) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-slf32", "config should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var changes map[string]interface{}
|
|
|
|
for _, a := range existing.Applications {
|
|
|
|
if a.AppID == config.AppID {
|
|
|
|
if a.OIDCConfig != nil {
|
|
|
|
changes = a.OIDCConfig.Changes(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.OIDCConfigChanged, changes)
|
2020-04-21 17:00:32 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:54:40 +02:00
|
|
|
func OIDCConfigSecretChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, appID string, secret *crypto.CryptoValue) func(ctx context.Context) (*es_models.Aggregate, error) {
|
2020-04-21 17:00:32 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changes := make(map[string]interface{}, 1)
|
|
|
|
changes["appId"] = appID
|
|
|
|
changes["clientSecret"] = secret
|
|
|
|
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.OIDCConfigSecretChanged, changes)
|
2020-04-21 17:00:32 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 07:54:40 +02:00
|
|
|
|
|
|
|
func ProjectGrantAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, grant *model.ProjectGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-kd89w", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ProjectGrantAdded, grant)
|
2020-04-23 07:54:40 +02:00
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, grant *model.ProjectGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d9ie2", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var changes map[string]interface{}
|
|
|
|
for _, g := range existing.Grants {
|
|
|
|
if g.GrantID == grant.GrantID {
|
|
|
|
changes = g.Changes(grant)
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ProjectGrantChanged, changes)
|
2020-04-23 07:54:40 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, grant *model.ProjectGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-kci8d", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ProjectGrantRemoved, &model.ProjectGrantID{GrantID: grant.GrantID})
|
2020-04-23 07:54:40 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantDeactivatedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, grant *model.ProjectGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-id832", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ProjectGrantDeactivated, &model.ProjectGrantID{GrantID: grant.GrantID})
|
2020-04-23 07:54:40 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantReactivatedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, grant *model.ProjectGrant) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if grant == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-8diw2", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ProjectGrantReactivated, &model.ProjectGrantID{GrantID: grant.GrantID})
|
2020-04-23 07:54:40 +02:00
|
|
|
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantMemberAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectGrantMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if member == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-4ufh6", "grant should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
agg.AppendEvent(model.ProjectGrantMemberAdded, member)
|
2020-04-23 07:54:40 +02:00
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantMemberChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectGrantMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if member == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8i4h", "member should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changes := make(map[string]interface{}, 1)
|
|
|
|
changes["grantId"] = member.GrantID
|
|
|
|
changes["userId"] = member.UserID
|
|
|
|
changes["roles"] = member.Roles
|
|
|
|
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectGrantMemberChanged, changes)
|
2020-04-23 07:54:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantMemberRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Project, member *model.ProjectGrantMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if member == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-slp0r", "member should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := ProjectAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-11 12:16:29 +02:00
|
|
|
return agg.AppendEvent(model.ProjectGrantMemberRemoved, member)
|
2020-04-23 07:54:40 +02:00
|
|
|
}
|
|
|
|
}
|