2021-02-10 10:48:40 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2022-05-24 11:28:17 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-02-10 10:48:40 +01:00
|
|
|
)
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) AddDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*domain.MailTemplate, error) {
|
2022-04-05 07:58:09 +02:00
|
|
|
addedPolicy := NewInstanceMailTemplateWriteModel(ctx)
|
2022-03-24 17:21:34 +01:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&addedPolicy.MailTemplateWriteModel.WriteModel)
|
|
|
|
event, err := c.addDefaultMailTemplate(ctx, instanceAgg, addedPolicy, policy)
|
2021-02-10 10:48:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
pushedEvents, err := c.eventstore.Push(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
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (c *Commands) addDefaultMailTemplate(ctx context.Context, instanceAgg *eventstore.Aggregate, addedPolicy *InstanceMailTemplateWriteModel, policy *domain.MailTemplate) (eventstore.Command, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
if !policy.IsValid() {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "INSTANCE-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 {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowAlreadyExists(nil, "INSTANCE-5n8fs", "Errors.IAM.MailTemplate.AlreadyExists")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
return instance.NewMailTemplateAddedEvent(ctx, instanceAgg, 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
|
|
|
|
}
|
2022-01-03 09:19:07 +01:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-04-16 14:55:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToMailTemplatePolicy(&existingPolicy.MailTemplateWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (c *Commands) changeDefaultMailTemplate(ctx context.Context, policy *domain.MailTemplate) (*InstanceMailTemplateWriteModel, eventstore.Command, error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
if !policy.IsValid() {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, nil, zerrors.ThrowInvalidArgument(nil, "INSTANCE-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 {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, nil, zerrors.ThrowNotFound(nil, "INSTANCE-2N8fs", "Errors.IAM.MailTemplate.NotFound")
|
2021-02-10 10:48:40 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingPolicy.MailTemplateWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, instanceAgg, policy.Template)
|
2021-02-10 10:48:40 +01:00
|
|
|
if !hasChanged {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, nil, zerrors.ThrowPreconditionFailed(nil, "INSTANCE-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
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (c *Commands) defaultMailTemplateWriteModelByID(ctx context.Context) (policy *InstanceMailTemplateWriteModel, err error) {
|
2021-02-10 10:48:40 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-04-05 07:58:09 +02:00
|
|
|
writeModel := NewInstanceMailTemplateWriteModel(ctx)
|
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
|
|
|
|
}
|
2022-05-24 11:28:17 +02:00
|
|
|
|
|
|
|
func prepareAddDefaultEmailTemplate(
|
|
|
|
a *instance.Aggregate,
|
|
|
|
template []byte,
|
|
|
|
) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if template == nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "INSTANCE-fm9sd", "Errors.Instance.MailTemplate.Invalid")
|
2022-05-24 11:28:17 +02:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel := NewInstanceMailTemplateWriteModel(ctx)
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
if err = writeModel.Reduce(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if writeModel.State == domain.PolicyStateActive {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowAlreadyExists(nil, "INSTANCE-5n8fs", "Errors.Instance.MailTemplate.AlreadyExists")
|
2022-05-24 11:28:17 +02:00
|
|
|
}
|
|
|
|
return []eventstore.Command{
|
|
|
|
instance.NewMailTemplateAddedEvent(ctx, &a.Aggregate,
|
|
|
|
template,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|