2021-10-29 14:11:47 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-10-29 14:11:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-23 08:02:39 +00:00
|
|
|
MessageTextTable = "projections.message_texts"
|
2021-10-29 14:11:47 +00:00
|
|
|
|
|
|
|
MessageTextAggregateIDCol = "aggregate_id"
|
2022-03-23 08:02:39 +00:00
|
|
|
MessageTextInstanceIDCol = "instance_id"
|
2021-10-29 14:11:47 +00: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-06-14 05:51:00 +00:00
|
|
|
type messageTextProjection struct {
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newMessageTextProjection(ctx context.Context, config crdb.StatementHandlerConfig) *messageTextProjection {
|
|
|
|
p := new(messageTextProjection)
|
2021-10-29 14:11:47 +00:00
|
|
|
config.ProjectionName = MessageTextTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(MessageTextAggregateIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(MessageTextInstanceIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(MessageTextCreationDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(MessageTextChangeDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(MessageTextSequenceCol, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(MessageTextStateCol, crdb.ColumnTypeEnum),
|
|
|
|
crdb.NewColumn(MessageTextTypeCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(MessageTextLanguageCol, crdb.ColumnTypeText),
|
2022-03-29 09:53:19 +00:00
|
|
|
crdb.NewColumn(MessageTextTitleCol, crdb.ColumnTypeText, crdb.Nullable()),
|
|
|
|
crdb.NewColumn(MessageTextPreHeaderCol, crdb.ColumnTypeText, crdb.Nullable()),
|
|
|
|
crdb.NewColumn(MessageTextSubjectCol, crdb.ColumnTypeText, crdb.Nullable()),
|
|
|
|
crdb.NewColumn(MessageTextGreetingCol, crdb.ColumnTypeText, crdb.Nullable()),
|
|
|
|
crdb.NewColumn(MessageTextTextCol, crdb.ColumnTypeText, crdb.Nullable()),
|
|
|
|
crdb.NewColumn(MessageTextButtonTextCol, crdb.ColumnTypeText, crdb.Nullable()),
|
|
|
|
crdb.NewColumn(MessageTextFooterCol, crdb.ColumnTypeText, crdb.Nullable()),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(MessageTextInstanceIDCol, MessageTextAggregateIDCol, MessageTextTypeCol, MessageTextLanguageCol),
|
|
|
|
),
|
|
|
|
)
|
2021-10-29 14:11:47 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *messageTextProjection) reducers() []handler.AggregateReducer {
|
2021-10-29 14:11:47 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.CustomTextSetEventType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.CustomTextRemovedEventType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.CustomTextTemplateRemovedEventType,
|
|
|
|
Reduce: p.reduceTemplateRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Aggregate: instance.AggregateType,
|
2021-10-29 14:11:47 +00:00
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.CustomTextSetEventType,
|
2021-10-29 14:11:47 +00:00
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.CustomTextRemovedEventType,
|
2021-10-29 14:11:47 +00:00
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.CustomTextTemplateRemovedEventType,
|
2021-10-29 14:11:47 +00:00
|
|
|
Reduce: p.reduceTemplateRemoved,
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(MessageTextInstanceIDCol),
|
|
|
|
},
|
2021-10-29 14:11:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *messageTextProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var templateEvent policy.CustomTextSetEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextSetEvent:
|
|
|
|
templateEvent = e.CustomTextSetEvent
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.CustomTextSetEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
templateEvent = e.CustomTextSetEvent
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-2n90r", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextSetEventType, instance.CustomTextSetEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
if !isMessageTemplate(templateEvent.Template) {
|
2021-12-16 14:21:37 +00:00
|
|
|
return crdb.NewNoOpStatement(event), nil
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cols := []handler.Column{
|
|
|
|
handler.NewCol(MessageTextAggregateIDCol, templateEvent.Aggregate().ID),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(MessageTextInstanceIDCol, templateEvent.Aggregate().InstanceID),
|
2021-10-29 14:11:47 +00: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))
|
|
|
|
}
|
|
|
|
return crdb.NewUpsertStatement(
|
|
|
|
&templateEvent,
|
2022-08-31 07:52:43 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(MessageTextInstanceIDCol, nil),
|
|
|
|
handler.NewCol(MessageTextAggregateIDCol, nil),
|
|
|
|
handler.NewCol(MessageTextTypeCol, nil),
|
|
|
|
handler.NewCol(MessageTextLanguageCol, nil),
|
|
|
|
},
|
|
|
|
cols,
|
|
|
|
), nil
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *messageTextProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var templateEvent policy.CustomTextRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextRemovedEvent:
|
|
|
|
templateEvent = e.CustomTextRemovedEvent
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.CustomTextRemovedEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
templateEvent = e.CustomTextRemovedEvent
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-fm0ge", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextRemovedEventType, instance.CustomTextRemovedEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
if !isMessageTemplate(templateEvent.Template) {
|
2021-12-21 14:18:05 +00:00
|
|
|
return crdb.NewNoOpStatement(event), nil
|
2021-10-29 14:11:47 +00: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, ""))
|
|
|
|
}
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
&templateEvent,
|
|
|
|
cols,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MessageTextAggregateIDCol, templateEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(MessageTextTypeCol, templateEvent.Template),
|
|
|
|
handler.NewCond(MessageTextLanguageCol, templateEvent.Language.String()),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *messageTextProjection) reduceTemplateRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-21 14:18:05 +00:00
|
|
|
var templateEvent policy.CustomTextTemplateRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextTemplateRemovedEvent:
|
|
|
|
templateEvent = e.CustomTextTemplateRemovedEvent
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.CustomTextTemplateRemovedEvent:
|
2021-12-21 14:18:05 +00:00
|
|
|
templateEvent = e.CustomTextTemplateRemovedEvent
|
|
|
|
default:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-2n9rs", "reduce.wrong.event.type %s", org.CustomTextTemplateRemovedEventType)
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
if !isMessageTemplate(templateEvent.Template) {
|
2021-12-21 14:18:05 +00:00
|
|
|
return crdb.NewNoOpStatement(event), nil
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
2021-12-21 14:18:05 +00:00
|
|
|
event,
|
2021-10-29 14:11:47 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(MessageTextAggregateIDCol, templateEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(MessageTextTypeCol, templateEvent.Template),
|
2021-12-17 08:31:39 +00:00
|
|
|
handler.NewCond(MessageTextLanguageCol, templateEvent.Language.String()),
|
2021-10-29 14:11:47 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMessageTemplate(template string) bool {
|
|
|
|
return template == domain.InitCodeMessageType ||
|
|
|
|
template == domain.PasswordResetMessageType ||
|
|
|
|
template == domain.VerifyEmailMessageType ||
|
|
|
|
template == domain.VerifyPhoneMessageType ||
|
|
|
|
template == domain.DomainClaimedMessageType ||
|
|
|
|
template == domain.PasswordlessRegistrationMessageType
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|