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/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 16:11:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-30 17:01:17 +01:00
|
|
|
CustomTextTable = "projections.custom_texts2"
|
2021-10-29 16:11:47 +02:00
|
|
|
|
|
|
|
CustomTextAggregateIDCol = "aggregate_id"
|
2022-03-23 09:02:39 +01:00
|
|
|
CustomTextInstanceIDCol = "instance_id"
|
2021-10-29 16:11:47 +02:00
|
|
|
CustomTextCreationDateCol = "creation_date"
|
|
|
|
CustomTextChangeDateCol = "change_date"
|
|
|
|
CustomTextSequenceCol = "sequence"
|
|
|
|
CustomTextIsDefaultCol = "is_default"
|
|
|
|
CustomTextTemplateCol = "template"
|
|
|
|
CustomTextLanguageCol = "language"
|
|
|
|
CustomTextKeyCol = "key"
|
|
|
|
CustomTextTextCol = "text"
|
2022-11-30 17:01:17 +01:00
|
|
|
CustomTextOwnerRemovedCol = "owner_removed"
|
2021-10-29 16:11:47 +02:00
|
|
|
)
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
type customTextProjection struct {
|
2022-03-23 09:02:39 +01:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func newCustomTextProjection(ctx context.Context, config crdb.StatementHandlerConfig) *customTextProjection {
|
|
|
|
p := new(customTextProjection)
|
2021-10-29 16:11:47 +02:00
|
|
|
config.ProjectionName = CustomTextTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 09:02:39 +01: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),
|
2022-11-30 17:01:17 +01:00
|
|
|
crdb.NewColumn(CustomTextOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 09:02:39 +01:00
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(CustomTextInstanceIDCol, CustomTextAggregateIDCol, CustomTextTemplateCol, CustomTextKeyCol, CustomTextLanguageCol),
|
2022-11-30 17:01:17 +01:00
|
|
|
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{CustomTextOwnerRemovedCol})),
|
2022-03-23 09:02:39 +01:00
|
|
|
),
|
|
|
|
)
|
2021-10-29 16:11:47 +02:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *customTextProjection) reducers() []handler.AggregateReducer {
|
2021-10-29 16:11:47 +02: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-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,
|
2021-10-29 16:11:47 +02:00
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
2022-03-24 17:21:34 +01:00
|
|
|
Event: instance.CustomTextSetEventType,
|
2021-10-29 16:11:47 +02:00
|
|
|
Reduce: p.reduceSet,
|
|
|
|
},
|
|
|
|
{
|
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(CustomTextInstanceIDCol),
|
|
|
|
},
|
2021-10-29 16:11:47 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *customTextProjection) reduceSet(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 16:11:47 +02:00
|
|
|
var customTextEvent policy.CustomTextSetEvent
|
|
|
|
var isDefault bool
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextSetEvent:
|
|
|
|
customTextEvent = e.CustomTextSetEvent
|
|
|
|
isDefault = false
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.CustomTextSetEvent:
|
2021-10-29 16:11:47 +02:00
|
|
|
customTextEvent = e.CustomTextSetEvent
|
|
|
|
isDefault = true
|
|
|
|
default:
|
2022-03-24 17:21:34 +01:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-KKfw4", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextSetEventType, instance.CustomTextSetEventType})
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
2021-12-16 15:21:37 +01:00
|
|
|
return crdb.NewUpsertStatement(
|
2021-10-29 16:11:47 +02:00
|
|
|
&customTextEvent,
|
2022-08-31 09:52:43 +02: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 16:11:47 +02:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
|
2022-03-23 09:02:39 +01:00
|
|
|
handler.NewCol(CustomTextInstanceIDCol, customTextEvent.Aggregate().InstanceID),
|
2021-10-29 16:11:47 +02: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 07:51:00 +02:00
|
|
|
func (p *customTextProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 16:11:47 +02:00
|
|
|
var customTextEvent policy.CustomTextRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextRemovedEvent:
|
|
|
|
customTextEvent = e.CustomTextRemovedEvent
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.CustomTextRemovedEvent:
|
2021-10-29 16:11:47 +02:00
|
|
|
customTextEvent = e.CustomTextRemovedEvent
|
|
|
|
default:
|
2022-03-24 17:21:34 +01:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-n9wJg", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextRemovedEventType, instance.CustomTextRemovedEventType})
|
2021-10-29 16:11:47 +02: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 15:21:37 +01:00
|
|
|
handler.NewCond(CustomTextLanguageCol, customTextEvent.Language.String()),
|
2022-11-10 11:59:33 +01:00
|
|
|
handler.NewCond(CustomTextInstanceIDCol, customTextEvent.Aggregate().InstanceID),
|
2021-10-29 16:11:47 +02:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *customTextProjection) reduceTemplateRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-10-29 16:11:47 +02:00
|
|
|
var customTextEvent policy.CustomTextTemplateRemovedEvent
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.CustomTextTemplateRemovedEvent:
|
|
|
|
customTextEvent = e.CustomTextTemplateRemovedEvent
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.CustomTextTemplateRemovedEvent:
|
2021-10-29 16:11:47 +02:00
|
|
|
customTextEvent = e.CustomTextTemplateRemovedEvent
|
|
|
|
default:
|
2022-03-24 17:21:34 +01:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-29iPf", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextTemplateRemovedEventType, instance.CustomTextTemplateRemovedEventType})
|
2021-10-29 16:11:47 +02:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
&customTextEvent,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
|
|
|
|
handler.NewCond(CustomTextTemplateCol, customTextEvent.Template),
|
2021-12-17 09:31:39 +01:00
|
|
|
handler.NewCond(CustomTextLanguageCol, customTextEvent.Language.String()),
|
2022-11-10 11:59:33 +01:00
|
|
|
handler.NewCond(CustomTextInstanceIDCol, customTextEvent.Aggregate().InstanceID),
|
2021-10-29 16:11:47 +02:00
|
|
|
}), nil
|
|
|
|
}
|
2022-11-30 17:01:17 +01:00
|
|
|
|
|
|
|
func (p *customTextProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-V2T3z", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(CustomTextChangeDateCol, e.CreationDate()),
|
|
|
|
handler.NewCol(CustomTextSequenceCol, e.Sequence()),
|
|
|
|
handler.NewCol(CustomTextOwnerRemovedCol, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(CustomTextInstanceIDCol, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(CustomTextAggregateIDCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|