2021-07-05 13:10:49 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"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-07-05 13:10:49 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) SetInstanceCustomText(ctx context.Context, customText *domain.CustomText) (*domain.CustomText, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
setText := NewInstanceCustomTextWriteModel(ctx, customText.Key, customText.Language)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&setText.CustomTextWriteModel.WriteModel)
|
|
|
|
event, err := c.setDefaultCustomText(ctx, instanceAgg, setText, customText)
|
2021-07-05 13:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, event)
|
2021-07-05 13:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(setText, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToCustomText(&setText.CustomTextWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) setDefaultCustomText(ctx context.Context, instanceAgg *eventstore.Aggregate, addedPolicy *InstanceCustomTextWriteModel, text *domain.CustomText) (eventstore.Command, error) {
|
2021-07-05 13:10:49 +00:00
|
|
|
if !text.IsValid() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "INSTANCE-3MN0s", "Errors.CustomText.Invalid")
|
2021-07-05 13:10:49 +00:00
|
|
|
}
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
return instance.NewCustomTextSetEvent(
|
2021-07-05 13:10:49 +00:00
|
|
|
ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2021-07-05 13:10:49 +00:00
|
|
|
text.Template,
|
|
|
|
text.Key,
|
|
|
|
text.Text,
|
|
|
|
text.Language), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) defaultCustomTextWriteModelByID(ctx context.Context, key string, language language.Tag) (policy *InstanceCustomTextWriteModel, err error) {
|
2021-07-05 13:10:49 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceCustomTextWriteModel(ctx, key, language)
|
2021-07-05 13:10:49 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|