zitadel/internal/command/iam_policy_mail_text.go

107 lines
3.4 KiB
Go
Raw Normal View History

2021-02-10 10:48:40 +01:00
package command
import (
"context"
"github.com/caos/zitadel/internal/domain"
2021-02-10 10:48:40 +01:00
caos_errs "github.com/caos/zitadel/internal/errors"
"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"
)
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)
event, err := c.addDefaultMailText(ctx, iamAgg, addedPolicy, policy)
2021-02-10 10:48:40 +01:00
if err != nil {
return nil, err
}
pushedEvents, err := c.eventstore.PushEvents(ctx, event)
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
}
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() {
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-3n8fs", "Errors.IAM.MailText.Invalid")
2021-02-10 10:48:40 +01:00
}
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
2021-02-10 10:48:40 +01:00
if err != nil {
return nil, err
2021-02-10 10:48:40 +01:00
}
if addedPolicy.State == domain.PolicyStateActive {
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-9o0pM", "Errors.IAM.MailText.AlreadyExists")
2021-02-10 10:48:40 +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
}
func (c *Commands) ChangeDefaultMailText(ctx context.Context, mailText *domain.MailText) (*domain.MailText, error) {
2021-02-10 10:48:40 +01:00
if !mailText.IsValid() {
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-kd9fs", "Errors.IAM.MailText.Invalid")
2021-02-10 10:48:40 +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")
}
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.MailTextWriteModel.WriteModel)
2021-02-10 10:48:40 +01:00
changedEvent, hasChanged := existingPolicy.NewChangedEvent(
ctx,
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")
}
pushedEvents, err := c.eventstore.PushEvents(ctx, changedEvent)
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
}
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)
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
2021-02-10 10:48:40 +01:00
if err != nil {
return nil, err
}
return writeModel, nil
}