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) AddDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*domain.MailTemplate, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
addedPolicy := NewIAMMailTemplateWriteModel()
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedPolicy.MailTemplateWriteModel.WriteModel)
|
2021-02-24 11:17:39 +01:00
|
|
|
event, err := c.addDefaultMailTemplate(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 writeModelToMailTemplatePolicy(&addedPolicy.MailTemplateWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) addDefaultMailTemplate(ctx context.Context, iamAgg *eventstore.Aggregate, addedPolicy *IAMMailTemplateWriteModel, policy *domain.MailTemplate) (eventstore.EventPusher, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
if !policy.IsValid() {
|
2021-03-15 12:51:15 +01:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-fm9sd", "Errors.IAM.MailTemplate.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-5n8fs", "Errors.IAM.MailTemplate.AlreadyExists")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
return iam_repo.NewMailTemplateAddedEvent(ctx, iamAgg, policy.Template), nil
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) ChangeDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*domain.MailTemplate, error) {
|
2021-04-16 14:55:55 +02:00
|
|
|
existingPolicy, changedEvent, err := c.changeDefaultMailTemplate(ctx, policy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToMailTemplatePolicy(&existingPolicy.MailTemplateWriteModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) changeDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*IAMMailTemplateWriteModel, eventstore.EventPusher, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
if !policy.IsValid() {
|
2021-04-16 14:55:55 +02:00
|
|
|
return nil, nil, caos_errs.ThrowInvalidArgument(nil, "IAM-4m9ds", "Errors.IAM.MailTemplate.Invalid")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
2021-02-24 11:17:39 +01:00
|
|
|
existingPolicy, err := c.defaultMailTemplateWriteModelByID(ctx)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
2021-04-16 14:55:55 +02:00
|
|
|
return nil, nil, err
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
2021-04-16 14:55:55 +02:00
|
|
|
return nil, nil, caos_errs.ThrowNotFound(nil, "IAM-2N8fs", "Errors.IAM.MailTemplate.NotFound")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.MailTemplateWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, iamAgg, policy.Template)
|
2021-02-10 10:48:40 +01:00
|
|
|
if !hasChanged {
|
2021-04-16 14:55:55 +02:00
|
|
|
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-3nfsG", "Errors.IAM.MailTemplate.NotChanged")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:55 +02:00
|
|
|
return existingPolicy, changedEvent, nil
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) defaultMailTemplateWriteModelByID(ctx context.Context) (policy *IAMMailTemplateWriteModel, err error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel := NewIAMMailTemplateWriteModel()
|
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
|
|
|
|
}
|