zitadel/internal/command/instance_policy_mail_template.go

96 lines
3.5 KiB
Go
Raw Normal View History

2021-02-10 09:48:40 +00:00
package command
import (
"context"
"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/repository/instance"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
2021-02-10 09:48:40 +00:00
)
func (c *Commands) AddDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*domain.MailTemplate, error) {
addedPolicy := NewInstanceMailTemplateWriteModel(ctx)
instanceAgg := InstanceAggregateFromWriteModel(&addedPolicy.MailTemplateWriteModel.WriteModel)
event, err := c.addDefaultMailTemplate(ctx, instanceAgg, addedPolicy, policy)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, err
}
pushedEvents, err := c.eventstore.Push(ctx, event)
if err != nil {
return nil, err
}
err = AppendAndReduce(addedPolicy, pushedEvents...)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, err
}
return writeModelToMailTemplatePolicy(&addedPolicy.MailTemplateWriteModel), nil
}
func (c *Commands) addDefaultMailTemplate(ctx context.Context, instanceAgg *eventstore.Aggregate, addedPolicy *InstanceMailTemplateWriteModel, policy *domain.MailTemplate) (eventstore.Command, error) {
2021-02-10 09:48:40 +00:00
if !policy.IsValid() {
return nil, caos_errs.ThrowInvalidArgument(nil, "INSTANCE-fm9sd", "Errors.IAM.MailTemplate.Invalid")
2021-02-10 09:48:40 +00:00
}
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, err
2021-02-10 09:48:40 +00:00
}
if addedPolicy.State == domain.PolicyStateActive {
return nil, caos_errs.ThrowAlreadyExists(nil, "INSTANCE-5n8fs", "Errors.IAM.MailTemplate.AlreadyExists")
2021-02-10 09:48:40 +00:00
}
return instance.NewMailTemplateAddedEvent(ctx, instanceAgg, policy.Template), nil
2021-02-10 09:48:40 +00:00
}
func (c *Commands) ChangeDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*domain.MailTemplate, error) {
existingPolicy, changedEvent, err := c.changeDefaultMailTemplate(ctx, policy)
if err != nil {
return nil, err
}
pushedEvents, err := c.eventstore.Push(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) (*InstanceMailTemplateWriteModel, eventstore.Command, error) {
2021-02-10 09:48:40 +00:00
if !policy.IsValid() {
return nil, nil, caos_errs.ThrowInvalidArgument(nil, "INSTANCE-4m9ds", "Errors.IAM.MailTemplate.Invalid")
2021-02-10 09:48:40 +00:00
}
existingPolicy, err := c.defaultMailTemplateWriteModelByID(ctx)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, nil, err
2021-02-10 09:48:40 +00:00
}
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
return nil, nil, caos_errs.ThrowNotFound(nil, "INSTANCE-2N8fs", "Errors.IAM.MailTemplate.NotFound")
2021-02-10 09:48:40 +00:00
}
instanceAgg := InstanceAggregateFromWriteModel(&existingPolicy.MailTemplateWriteModel.WriteModel)
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, instanceAgg, policy.Template)
2021-02-10 09:48:40 +00:00
if !hasChanged {
return nil, nil, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-3nfsG", "Errors.IAM.MailTemplate.NotChanged")
2021-02-10 09:48:40 +00:00
}
return existingPolicy, changedEvent, nil
2021-02-10 09:48:40 +00:00
}
func (c *Commands) defaultMailTemplateWriteModelByID(ctx context.Context) (policy *InstanceMailTemplateWriteModel, err error) {
2021-02-10 09:48:40 +00:00
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
writeModel := NewInstanceMailTemplateWriteModel(ctx)
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
2021-02-10 09:48:40 +00:00
if err != nil {
return nil, err
}
return writeModel, nil
}