2022-03-07 13:22:37 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-03-07 13:22:37 +00:00
|
|
|
)
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (c *Commands) AddDebugNotificationProviderLog(ctx context.Context, fileSystemProvider *fs.Config) (*domain.ObjectDetails, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceDebugNotificationLogWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&writeModel.WriteModel)
|
|
|
|
events, err := c.addDefaultDebugNotificationLog(ctx, instanceAgg, writeModel, fileSystemProvider)
|
2022-03-07 13:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(writeModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&writeModel.DebugNotificationWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (c *Commands) addDefaultDebugNotificationLog(ctx context.Context, instanceAgg *eventstore.Aggregate, addedWriteModel *InstanceDebugNotificationLogWriteModel, fileSystemProvider *fs.Config) ([]eventstore.Command, error) {
|
2022-03-07 13:22:37 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if addedWriteModel.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "INSTANCE-3h0fs", "Errors.IAM.DebugNotificationProvider.AlreadyExists")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
events := []eventstore.Command{
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.NewDebugNotificationProviderLogAddedEvent(ctx,
|
|
|
|
instanceAgg,
|
2022-03-07 13:22:37 +00:00
|
|
|
fileSystemProvider.Compact),
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (c *Commands) ChangeDefaultNotificationLog(ctx context.Context, fileSystemProvider *fs.Config) (*domain.ObjectDetails, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceDebugNotificationLogWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&writeModel.WriteModel)
|
|
|
|
event, err := c.changeDefaultDebugNotificationProviderLog(ctx, instanceAgg, writeModel, fileSystemProvider)
|
2022-03-07 13:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(writeModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&writeModel.DebugNotificationWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:24:30 +00:00
|
|
|
func (c *Commands) changeDefaultDebugNotificationProviderLog(ctx context.Context, instanceAgg *eventstore.Aggregate, existingProvider *InstanceDebugNotificationLogWriteModel, fileSystemProvider *fs.Config) (eventstore.Command, error) {
|
2022-03-07 13:22:37 +00:00
|
|
|
err := c.defaultDebugNotificationProviderLogWriteModelByID(ctx, existingProvider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !existingProvider.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "INSTANCE-2h0s3", "Errors.IAM.DebugNotificationProvider.NotFound")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
changedEvent, hasChanged := existingProvider.NewChangedEvent(ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2022-03-07 13:22:37 +00:00
|
|
|
fileSystemProvider.Compact)
|
|
|
|
if !hasChanged {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-fn9p3", "Errors.IAM.LoginPolicy.NotChanged")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
return changedEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) RemoveDefaultNotificationLog(ctx context.Context) (*domain.ObjectDetails, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
existingProvider := NewInstanceDebugNotificationLogWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingProvider.WriteModel)
|
2022-03-07 13:22:37 +00:00
|
|
|
err := c.defaultDebugNotificationProviderLogWriteModelByID(ctx, existingProvider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !existingProvider.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "INSTANCE-39lse", "Errors.IAM.DebugNotificationProvider.NotFound")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, instance.NewDebugNotificationProviderLogRemovedEvent(ctx, instanceAgg))
|
2022-03-07 13:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingProvider, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&existingProvider.DebugNotificationWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) defaultDebugNotificationProviderLogWriteModelByID(ctx context.Context, writeModel *InstanceDebugNotificationLogWriteModel) (err error) {
|
2022-03-07 13:22:37 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|