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/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
|
|
|
CustomTextTable = "projections.custom_texts"
|
2021-10-29 14:11:47 +00:00
|
|
|
|
|
|
|
CustomTextAggregateIDCol = "aggregate_id"
|
2022-03-23 08:02:39 +00:00
|
|
|
CustomTextInstanceIDCol = "instance_id"
|
2021-10-29 14:11:47 +00:00
|
|
|
CustomTextCreationDateCol = "creation_date"
|
|
|
|
CustomTextChangeDateCol = "change_date"
|
|
|
|
CustomTextSequenceCol = "sequence"
|
|
|
|
CustomTextIsDefaultCol = "is_default"
|
|
|
|
CustomTextTemplateCol = "template"
|
|
|
|
CustomTextLanguageCol = "language"
|
|
|
|
CustomTextKeyCol = "key"
|
|
|
|
CustomTextTextCol = "text"
|
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type customTextProjection struct {
|
2022-03-23 08:02:39 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newCustomTextProjection(ctx context.Context, config crdb.StatementHandlerConfig) *customTextProjection {
|
|
|
|
p := new(customTextProjection)
|
2021-10-29 14:11:47 +00:00
|
|
|
config.ProjectionName = CustomTextTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(CustomTextAggregateIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(CustomTextInstanceIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(CustomTextCreationDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(CustomTextChangeDateCol, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(CustomTextSequenceCol, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(CustomTextIsDefaultCol, crdb.ColumnTypeBool),
|
|
|
|
crdb.NewColumn(CustomTextTemplateCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(CustomTextLanguageCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(CustomTextKeyCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(CustomTextTextCol, crdb.ColumnTypeText),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(CustomTextInstanceIDCol, CustomTextAggregateIDCol, CustomTextTemplateCol, CustomTextKeyCol, CustomTextLanguageCol),
|
|
|
|
),
|
|
|
|
)
|
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 *customTextProjection) 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.reduceSet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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.reduceSet,
|
|
|
|
},
|
|
|
|
{
|
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(CustomTextInstanceIDCol),
|
|
|
|
},
|
2021-10-29 14:11:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *customTextProjection) reduceSet(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var customTextEvent policy.CustomTextSetEvent
|
|
|
|
var isDefault bool
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextSetEvent:
|
|
|
|
customTextEvent = e.CustomTextSetEvent
|
|
|
|
isDefault = false
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.CustomTextSetEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
customTextEvent = e.CustomTextSetEvent
|
|
|
|
isDefault = true
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-KKfw4", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextSetEventType, instance.CustomTextSetEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
2021-12-16 14:21:37 +00:00
|
|
|
return crdb.NewUpsertStatement(
|
2021-10-29 14:11:47 +00:00
|
|
|
&customTextEvent,
|
2022-08-31 07:52:43 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(CustomTextInstanceIDCol, nil),
|
|
|
|
handler.NewCol(CustomTextAggregateIDCol, nil),
|
|
|
|
handler.NewCol(CustomTextTemplateCol, nil),
|
|
|
|
handler.NewCol(CustomTextKeyCol, nil),
|
|
|
|
handler.NewCol(CustomTextLanguageCol, nil),
|
|
|
|
},
|
2021-10-29 14:11:47 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(CustomTextInstanceIDCol, customTextEvent.Aggregate().InstanceID),
|
2021-10-29 14:11:47 +00:00
|
|
|
handler.NewCol(CustomTextCreationDateCol, customTextEvent.CreationDate()),
|
|
|
|
handler.NewCol(CustomTextChangeDateCol, customTextEvent.CreationDate()),
|
|
|
|
handler.NewCol(CustomTextSequenceCol, customTextEvent.Sequence()),
|
|
|
|
handler.NewCol(CustomTextIsDefaultCol, isDefault),
|
|
|
|
handler.NewCol(CustomTextTemplateCol, customTextEvent.Template),
|
|
|
|
handler.NewCol(CustomTextLanguageCol, customTextEvent.Language.String()),
|
|
|
|
handler.NewCol(CustomTextKeyCol, customTextEvent.Key),
|
|
|
|
handler.NewCol(CustomTextTextCol, customTextEvent.Text),
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *customTextProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var customTextEvent policy.CustomTextRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextRemovedEvent:
|
|
|
|
customTextEvent = e.CustomTextRemovedEvent
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.CustomTextRemovedEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
customTextEvent = e.CustomTextRemovedEvent
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-n9wJg", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextRemovedEventType, instance.CustomTextRemovedEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
&customTextEvent,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(CustomTextTemplateCol, customTextEvent.Template),
|
|
|
|
handler.NewCond(CustomTextKeyCol, customTextEvent.Key),
|
2021-12-16 14:21:37 +00:00
|
|
|
handler.NewCond(CustomTextLanguageCol, customTextEvent.Language.String()),
|
2021-10-29 14:11:47 +00:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *customTextProjection) reduceTemplateRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 14:11:47 +00:00
|
|
|
var customTextEvent policy.CustomTextTemplateRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextTemplateRemovedEvent:
|
|
|
|
customTextEvent = e.CustomTextTemplateRemovedEvent
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.CustomTextTemplateRemovedEvent:
|
2021-10-29 14:11:47 +00:00
|
|
|
customTextEvent = e.CustomTextTemplateRemovedEvent
|
|
|
|
default:
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-29iPf", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextTemplateRemovedEventType, instance.CustomTextTemplateRemovedEventType})
|
2021-10-29 14:11:47 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
&customTextEvent,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(CustomTextTemplateCol, customTextEvent.Template),
|
2021-12-17 08:31:39 +00:00
|
|
|
handler.NewCond(CustomTextLanguageCol, customTextEvent.Language.String()),
|
2021-10-29 14:11:47 +00:00
|
|
|
}), nil
|
|
|
|
}
|