2021-10-29 16:11:47 +02:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-10-29 16:11:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-30 17:01:17 +01:00
|
|
|
MessageTextTable = "projections.message_texts2"
|
2021-10-29 16:11:47 +02:00
|
|
|
|
|
|
|
MessageTextAggregateIDCol = "aggregate_id"
|
2022-03-23 09:02:39 +01:00
|
|
|
MessageTextInstanceIDCol = "instance_id"
|
2021-10-29 16:11:47 +02:00
|
|
|
MessageTextCreationDateCol = "creation_date"
|
|
|
|
MessageTextChangeDateCol = "change_date"
|
|
|
|
MessageTextSequenceCol = "sequence"
|
|
|
|
MessageTextStateCol = "state"
|
|
|
|
MessageTextTypeCol = "type"
|
|
|
|
MessageTextLanguageCol = "language"
|
|
|
|
MessageTextTitleCol = "title"
|
|
|
|
MessageTextPreHeaderCol = "pre_header"
|
|
|
|
MessageTextSubjectCol = "subject"
|
|
|
|
MessageTextGreetingCol = "greeting"
|
|
|
|
MessageTextTextCol = "text"
|
|
|
|
MessageTextButtonTextCol = "button_text"
|
|
|
|
MessageTextFooterCol = "footer_text"
|
2022-11-30 17:01:17 +01:00
|
|
|
MessageTextOwnerRemovedCol = "owner_removed"
|
2021-10-29 16:11:47 +02:00
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type messageTextProjection struct{}
|
|
|
|
|
|
|
|
func newMessageTextProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(messageTextProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*messageTextProjection) Name() string {
|
|
|
|
return MessageTextTable
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (*messageTextProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(MessageTextAggregateIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MessageTextInstanceIDCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MessageTextCreationDateCol, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(MessageTextChangeDateCol, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(MessageTextSequenceCol, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(MessageTextStateCol, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(MessageTextTypeCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MessageTextLanguageCol, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(MessageTextTitleCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextPreHeaderCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextSubjectCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextGreetingCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextTextCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextButtonTextCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextFooterCol, handler.ColumnTypeText, handler.Nullable()),
|
|
|
|
handler.NewColumn(MessageTextOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
|
2022-03-23 09:02:39 +01:00
|
|
|
},
|
2023-10-19 12:19:10 +02:00
|
|
|
handler.NewPrimaryKey(MessageTextInstanceIDCol, MessageTextAggregateIDCol, MessageTextTypeCol, MessageTextLanguageCol),
|
|
|
|
handler.WithIndex(handler.NewIndex("owner_removed", []string{MessageTextOwnerRemovedCol})),
|
2022-03-23 09:02:39 +01:00
|
|
|
),
|
|
|
|
)
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (p *messageTextProjection) Reducers() []handler.AggregateReducer {
|
2021-10-29 16:11:47 +02:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
2023-10-19 12:19:10 +02:00
|
|
|
EventReducers: []handler.EventReducer{
|
2021-10-29 16:11:47 +02:00
|
|
|
{
|
|
|
|
Event: org.CustomTextSetEventType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.CustomTextRemovedEventType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.CustomTextTemplateRemovedEventType,
|
|
|
|
Reduce: p.reduceTemplateRemoved,
|
|
|
|
},
|
2022-11-30 17:01:17 +01:00
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOwnerRemoved,
|
|
|
|
},
|
2021-10-29 16:11:47 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 12:19:10 +02:00
|
|
|
EventReducers: []handler.EventReducer{
|
2021-10-29 16:11:47 +02:00
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Event: instance.CustomTextSetEventType,
|
2021-10-29 16:11:47 +02:00
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Event: instance.CustomTextRemovedEventType,
|
2021-10-29 16:11:47 +02:00
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Event: instance.CustomTextTemplateRemovedEventType,
|
2021-10-29 16:11:47 +02:00
|
|
|
Reduce: p.reduceTemplateRemoved,
|
|
|
|
},
|
2022-10-20 13:36:52 +01:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(MessageTextInstanceIDCol),
|
|
|
|
},
|
2021-10-29 16:11:47 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *messageTextProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 16:11:47 +02:00
|
|
|
var templateEvent policy.CustomTextSetEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextSetEvent:
|
|
|
|
templateEvent = e.CustomTextSetEvent
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.CustomTextSetEvent:
|
2021-10-29 16:11:47 +02:00
|
|
|
templateEvent = e.CustomTextSetEvent
|
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-2n90r", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextSetEventType, instance.CustomTextSetEventType})
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
if !isMessageTemplate(templateEvent.Template) {
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cols := []handler.Column{
|
|
|
|
handler.NewCol(MessageTextAggregateIDCol, templateEvent.Aggregate().ID),
|
2022-03-23 09:02:39 +01:00
|
|
|
handler.NewCol(MessageTextInstanceIDCol, templateEvent.Aggregate().InstanceID),
|
2021-10-29 16:11:47 +02:00
|
|
|
handler.NewCol(MessageTextCreationDateCol, templateEvent.CreationDate()),
|
|
|
|
handler.NewCol(MessageTextChangeDateCol, templateEvent.CreationDate()),
|
|
|
|
handler.NewCol(MessageTextSequenceCol, templateEvent.Sequence()),
|
|
|
|
handler.NewCol(MessageTextStateCol, domain.PolicyStateActive),
|
|
|
|
handler.NewCol(MessageTextTypeCol, templateEvent.Template),
|
|
|
|
handler.NewCol(MessageTextLanguageCol, templateEvent.Language.String()),
|
|
|
|
}
|
|
|
|
if isTitle(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextTitleCol, templateEvent.Text))
|
|
|
|
}
|
|
|
|
if isPreHeader(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextPreHeaderCol, templateEvent.Text))
|
|
|
|
}
|
|
|
|
if isSubject(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextSubjectCol, templateEvent.Text))
|
|
|
|
}
|
|
|
|
if isGreeting(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextGreetingCol, templateEvent.Text))
|
|
|
|
}
|
|
|
|
if isText(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextTextCol, templateEvent.Text))
|
|
|
|
}
|
|
|
|
if isButtonText(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextButtonTextCol, templateEvent.Text))
|
|
|
|
}
|
|
|
|
if isFooterText(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextFooterCol, templateEvent.Text))
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpsertStatement(
|
2021-10-29 16:11:47 +02:00
|
|
|
&templateEvent,
|
2022-08-31 09:52:43 +02:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(MessageTextInstanceIDCol, nil),
|
|
|
|
handler.NewCol(MessageTextAggregateIDCol, nil),
|
|
|
|
handler.NewCol(MessageTextTypeCol, nil),
|
|
|
|
handler.NewCol(MessageTextLanguageCol, nil),
|
|
|
|
},
|
|
|
|
cols,
|
|
|
|
), nil
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *messageTextProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 16:11:47 +02:00
|
|
|
var templateEvent policy.CustomTextRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextRemovedEvent:
|
|
|
|
templateEvent = e.CustomTextRemovedEvent
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.CustomTextRemovedEvent:
|
2021-10-29 16:11:47 +02:00
|
|
|
templateEvent = e.CustomTextRemovedEvent
|
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-fm0ge", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextRemovedEventType, instance.CustomTextRemovedEventType})
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
if !isMessageTemplate(templateEvent.Template) {
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
cols := []handler.Column{
|
|
|
|
handler.NewCol(MessageTextChangeDateCol, templateEvent.CreationDate()),
|
|
|
|
handler.NewCol(MessageTextSequenceCol, templateEvent.Sequence()),
|
|
|
|
}
|
|
|
|
if isTitle(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextTitleCol, ""))
|
|
|
|
}
|
|
|
|
if isPreHeader(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextPreHeaderCol, ""))
|
|
|
|
}
|
|
|
|
if isSubject(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextSubjectCol, ""))
|
|
|
|
}
|
|
|
|
if isGreeting(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextGreetingCol, ""))
|
|
|
|
}
|
|
|
|
if isText(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextTextCol, ""))
|
|
|
|
}
|
|
|
|
if isButtonText(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextButtonTextCol, ""))
|
|
|
|
}
|
|
|
|
if isFooterText(templateEvent.Key) {
|
|
|
|
cols = append(cols, handler.NewCol(MessageTextFooterCol, ""))
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2021-10-29 16:11:47 +02:00
|
|
|
&templateEvent,
|
|
|
|
cols,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MessageTextAggregateIDCol, templateEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(MessageTextTypeCol, templateEvent.Template),
|
|
|
|
handler.NewCond(MessageTextLanguageCol, templateEvent.Language.String()),
|
2022-11-10 11:59:33 +01:00
|
|
|
handler.NewCond(MessageTextInstanceIDCol, templateEvent.Aggregate().InstanceID),
|
2021-10-29 16:11:47 +02:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *messageTextProjection) reduceTemplateRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-21 15:18:05 +01:00
|
|
|
var templateEvent policy.CustomTextTemplateRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextTemplateRemovedEvent:
|
|
|
|
templateEvent = e.CustomTextTemplateRemovedEvent
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.CustomTextTemplateRemovedEvent:
|
2021-12-21 15:18:05 +01:00
|
|
|
templateEvent = e.CustomTextTemplateRemovedEvent
|
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-2n9rs", "reduce.wrong.event.type %s", org.CustomTextTemplateRemovedEventType)
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
if !isMessageTemplate(templateEvent.Template) {
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewDeleteStatement(
|
2021-12-21 15:18:05 +01:00
|
|
|
event,
|
2021-10-29 16:11:47 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MessageTextAggregateIDCol, templateEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(MessageTextTypeCol, templateEvent.Template),
|
2021-12-17 09:31:39 +01:00
|
|
|
handler.NewCond(MessageTextLanguageCol, templateEvent.Language.String()),
|
2022-11-10 11:59:33 +01:00
|
|
|
handler.NewCond(MessageTextInstanceIDCol, templateEvent.Aggregate().InstanceID),
|
2021-10-29 16:11:47 +02:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:01:17 +01:00
|
|
|
func (p *messageTextProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-mLsQw", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2022-11-30 17:01:17 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewDeleteStatement(
|
2022-11-30 17:01:17 +01:00
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MessageTextInstanceIDCol, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(MessageTextAggregateIDCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:11:47 +02:00
|
|
|
func isMessageTemplate(template string) bool {
|
|
|
|
return template == domain.InitCodeMessageType ||
|
|
|
|
template == domain.PasswordResetMessageType ||
|
|
|
|
template == domain.VerifyEmailMessageType ||
|
|
|
|
template == domain.VerifyPhoneMessageType ||
|
2023-08-09 07:49:12 +02:00
|
|
|
template == domain.VerifySMSOTPMessageType ||
|
|
|
|
template == domain.VerifyEmailOTPMessageType ||
|
2021-10-29 16:11:47 +02:00
|
|
|
template == domain.DomainClaimedMessageType ||
|
2023-01-25 09:49:41 +01:00
|
|
|
template == domain.PasswordlessRegistrationMessageType ||
|
|
|
|
template == domain.PasswordChangeMessageType
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
func isTitle(key string) bool {
|
|
|
|
return key == domain.MessageTitle
|
|
|
|
}
|
|
|
|
func isPreHeader(key string) bool {
|
|
|
|
return key == domain.MessagePreHeader
|
|
|
|
}
|
|
|
|
func isSubject(key string) bool {
|
|
|
|
return key == domain.MessageSubject
|
|
|
|
}
|
|
|
|
func isGreeting(key string) bool {
|
|
|
|
return key == domain.MessageGreeting
|
|
|
|
}
|
|
|
|
func isText(key string) bool {
|
|
|
|
return key == domain.MessageText
|
|
|
|
}
|
|
|
|
func isButtonText(key string) bool {
|
|
|
|
return key == domain.MessageButtonText
|
|
|
|
}
|
|
|
|
func isFooterText(key string) bool {
|
|
|
|
return key == domain.MessageFooterText
|
|
|
|
}
|