2021-06-10 13:49:10 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
2021-12-16 15:21:37 +01:00
|
|
|
"golang.org/x/text/language"
|
2021-06-10 13:49:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) SetDefaultMessageText(ctx context.Context, messageText *domain.CustomMessageText) (*domain.ObjectDetails, error) {
|
|
|
|
iamAgg := iam.NewAggregate()
|
|
|
|
events, existingMessageText, err := c.setDefaultMessageText(ctx, &iamAgg.Aggregate, messageText)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-03 09:19:07 +01:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
2021-06-10 13:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingMessageText, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&existingMessageText.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (c *Commands) setDefaultMessageText(ctx context.Context, iamAgg *eventstore.Aggregate, msg *domain.CustomMessageText) ([]eventstore.Command, *IAMCustomMessageTextReadModel, error) {
|
2021-06-10 13:49:10 +02:00
|
|
|
if !msg.IsValid() {
|
|
|
|
return nil, nil, caos_errs.ThrowInvalidArgument(nil, "IAM-kd9fs", "Errors.CustomMessageText.Invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
existingMessageText, err := c.defaultCustomMessageTextWriteModelByID(ctx, msg.MessageTextType, msg.Language)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-01-03 09:19:07 +01:00
|
|
|
events := make([]eventstore.Command, 0)
|
2021-06-10 13:49:10 +02:00
|
|
|
if existingMessageText.Greeting != msg.Greeting {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.Greeting != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageGreeting, msg.Greeting, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageGreeting, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
if existingMessageText.Subject != msg.Subject {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.Subject != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageSubject, msg.Subject, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageSubject, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
if existingMessageText.Title != msg.Title {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.Title != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageTitle, msg.Title, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageTitle, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
if existingMessageText.PreHeader != msg.PreHeader {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.PreHeader != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessagePreHeader, msg.PreHeader, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessagePreHeader, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
if existingMessageText.Text != msg.Text {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.Text != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageText, msg.Text, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageText, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
if existingMessageText.ButtonText != msg.ButtonText {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.ButtonText != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageButtonText, msg.ButtonText, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageButtonText, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
if existingMessageText.FooterText != msg.FooterText {
|
2021-12-16 15:21:37 +01:00
|
|
|
if msg.FooterText != "" {
|
|
|
|
events = append(events, iam.NewCustomTextSetEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageFooterText, msg.FooterText, msg.Language))
|
|
|
|
} else {
|
|
|
|
events = append(events, iam.NewCustomTextRemovedEvent(ctx, iamAgg, msg.MessageTextType, domain.MessageFooterText, msg.Language))
|
|
|
|
}
|
2021-06-10 13:49:10 +02:00
|
|
|
}
|
|
|
|
return events, existingMessageText, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) defaultCustomMessageTextWriteModelByID(ctx context.Context, messageType string, lang language.Tag) (*IAMCustomMessageTextReadModel, error) {
|
|
|
|
writeModel := NewIAMCustomMessageTextWriteModel(messageType, lang)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|