2021-01-12 11:59:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/project"
|
|
|
|
)
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func (r *CommandSide) ChangeApplication(ctx context.Context, projectID string, appChange domain.Application, resourceOwner string) (domain.Application, error) {
|
|
|
|
if appChange.GetAppID() == "" || appChange.GetApplicationName() == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-4m9vS", "Errors.Project.App.Invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
existingApp, err := r.getApplicationWriteModel(ctx, projectID, appChange.GetAppID(), resourceOwner)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
if existingApp.State == domain.AppStateUnspecified || existingApp.State == domain.AppStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-28di9", "Errors.Project.App.NotExisting")
|
|
|
|
}
|
|
|
|
if existingApp.Name == appChange.GetApplicationName() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2m8vx", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingApp.WriteModel)
|
|
|
|
projectAgg.PushEvents(
|
|
|
|
project.NewApplicationChangedEvent(ctx, appChange.GetAppID(), existingApp.Name, appChange.GetApplicationName(), projectID),
|
|
|
|
)
|
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingApp, projectAgg)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
|
|
|
return applicationWriteModelToApplication(existingApp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) DeactivateApplication(ctx context.Context, projectID, appID, resourceOwner string) error {
|
|
|
|
if projectID == "" || appID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-88fi0", "Errors.IDMissing")
|
|
|
|
}
|
|
|
|
|
|
|
|
existingApp, err := r.getApplicationWriteModel(ctx, projectID, appID, resourceOwner)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
2021-01-28 05:35:26 +00:00
|
|
|
return err
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
if existingApp.State == domain.AppStateUnspecified || existingApp.State == domain.AppStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-ov9d3", "Errors.Project.App.NotExisting")
|
|
|
|
}
|
|
|
|
if existingApp.State != domain.AppStateActive {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-dsh35", "Errors.Project.App.NotActive")
|
|
|
|
}
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingApp.WriteModel)
|
|
|
|
projectAgg.PushEvents(project.NewApplicationDeactivatedEvent(ctx, appID))
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
return r.eventstore.PushAggregate(ctx, existingApp, projectAgg)
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func (r *CommandSide) ReactivateApplication(ctx context.Context, projectID, appID, resourceOwner string) error {
|
|
|
|
if projectID == "" || appID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-983dF", "Errors.IDMissing")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
|
|
|
existingApp, err := r.getApplicationWriteModel(ctx, projectID, appID, resourceOwner)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
if existingApp.State == domain.AppStateUnspecified || existingApp.State == domain.AppStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-ov9d3", "Errors.Project.App.NotExisting")
|
|
|
|
}
|
|
|
|
if existingApp.State != domain.AppStateInactive {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-1n8cM", "Errors.Project.App.NotInactive")
|
|
|
|
}
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingApp.WriteModel)
|
|
|
|
projectAgg.PushEvents(project.NewApplicationReactivatedEvent(ctx, appID))
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingApp, projectAgg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) RemoveApplication(ctx context.Context, projectID, appID, resourceOwner string) error {
|
|
|
|
if projectID == "" || appID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "COMMAND-1b7Jf", "Errors.IDMissing")
|
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
existingApp, err := r.getApplicationWriteModel(ctx, projectID, appID, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingApp.State == domain.AppStateUnspecified || existingApp.State == domain.AppStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "COMMAND-0po9s", "Errors.Project.App.NotExisting")
|
|
|
|
}
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingApp.WriteModel)
|
|
|
|
projectAgg.PushEvents(project.NewApplicationRemovedEvent(ctx, appID, existingApp.Name, projectID))
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingApp, projectAgg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) getApplicationWriteModel(ctx context.Context, projectID, appID, resourceOwner string) (*ApplicationWriteModel, error) {
|
|
|
|
appWriteModel := NewApplicationWriteModelWithAppIDC(projectID, appID, resourceOwner)
|
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, appWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return appWriteModel, nil
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|