2021-02-10 10:48:40 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-02-10 10:48:40 +01:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
iam_repo "github.com/caos/zitadel/internal/repository/iam"
|
2021-02-10 10:48:40 +01:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) AddDefaultMailText(ctx context.Context, policy *domain.MailText) (*domain.MailText, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
addedPolicy := NewIAMMailTextWriteModel(policy.MailTextType, policy.Language)
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedPolicy.MailTextWriteModel.WriteModel)
|
2021-02-24 11:17:39 +01:00
|
|
|
event, err := c.addDefaultMailText(ctx, iamAgg, addedPolicy, policy)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, event)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedPolicy, pushedEvents...)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToMailTextPolicy(&addedPolicy.MailTextWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) addDefaultMailText(ctx context.Context, iamAgg *eventstore.Aggregate, addedPolicy *IAMMailTextWriteModel, mailText *domain.MailText) (eventstore.EventPusher, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
if !mailText.IsValid() {
|
2021-03-15 12:51:15 +01:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-3n8fs", "Errors.IAM.MailText.Invalid")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
2021-02-24 11:17:39 +01:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
2021-02-18 14:48:27 +01:00
|
|
|
return nil, err
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-02-18 14:48:27 +01:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-9o0pM", "Errors.IAM.MailText.AlreadyExists")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
return iam_repo.NewMailTextAddedEvent(
|
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
mailText.MailTextType,
|
|
|
|
mailText.Language,
|
|
|
|
mailText.Title,
|
|
|
|
mailText.PreHeader,
|
|
|
|
mailText.Subject,
|
|
|
|
mailText.Greeting,
|
|
|
|
mailText.Text,
|
|
|
|
mailText.ButtonText), nil
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) ChangeDefaultMailText(ctx context.Context, mailText *domain.MailText) (*domain.MailText, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
if !mailText.IsValid() {
|
2021-03-15 12:51:15 +01:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-kd9fs", "Errors.IAM.MailText.Invalid")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
2021-02-24 11:17:39 +01:00
|
|
|
existingPolicy, err := c.defaultMailTextWriteModelByID(ctx, mailText.MailTextType, mailText.Language)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "IAM-2N8fs", "Errors.IAM.MailText.NotFound")
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.MailTextWriteModel.WriteModel)
|
2021-02-10 10:48:40 +01:00
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
iamAgg,
|
2021-02-10 10:48:40 +01:00
|
|
|
mailText.MailTextType,
|
|
|
|
mailText.Language,
|
|
|
|
mailText.Title,
|
|
|
|
mailText.PreHeader,
|
|
|
|
mailText.Subject,
|
|
|
|
mailText.Greeting,
|
|
|
|
mailText.Text,
|
|
|
|
mailText.ButtonText)
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-m9L0s", "Errors.IAM.MailText.NotChanged")
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, changedEvent)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToMailTextPolicy(&existingPolicy.MailTextWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) defaultMailTextWriteModelByID(ctx context.Context, mailTextType, language string) (policy *IAMMailTextWriteModel, err error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel := NewIAMMailTextWriteModel(mailTextType, language)
|
2021-02-24 11:17:39 +01:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|