2022-03-24 17:21:34 +01: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"
|
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"
|
2022-03-24 17:21:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-28 10:05:09 +02:00
|
|
|
InstanceProjectionTable = "projections.instances"
|
2022-03-24 17:21:34 +01:00
|
|
|
|
|
|
|
InstanceColumnID = "id"
|
2022-04-21 12:37:39 +02:00
|
|
|
InstanceColumnName = "name"
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceColumnChangeDate = "change_date"
|
2022-04-21 12:37:39 +02:00
|
|
|
InstanceColumnCreationDate = "creation_date"
|
2022-06-03 14:30:39 +02:00
|
|
|
InstanceColumnDefaultOrgID = "default_org_id"
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceColumnProjectID = "iam_project_id"
|
2022-03-29 11:53:19 +02:00
|
|
|
InstanceColumnConsoleID = "console_client_id"
|
2022-04-14 14:19:18 +02:00
|
|
|
InstanceColumnConsoleAppID = "console_app_id"
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceColumnSequence = "sequence"
|
|
|
|
InstanceColumnDefaultLanguage = "default_language"
|
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type instanceProjection struct{}
|
|
|
|
|
|
|
|
func newInstanceProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(instanceProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*instanceProjection) Name() string {
|
|
|
|
return InstanceProjectionTable
|
2022-03-24 17:21:34 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (*instanceProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(InstanceColumnID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(InstanceColumnName, handler.ColumnTypeText, handler.Default("")),
|
|
|
|
handler.NewColumn(InstanceColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(InstanceColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(InstanceColumnDefaultOrgID, handler.ColumnTypeText, handler.Default("")),
|
|
|
|
handler.NewColumn(InstanceColumnProjectID, handler.ColumnTypeText, handler.Default("")),
|
|
|
|
handler.NewColumn(InstanceColumnConsoleID, handler.ColumnTypeText, handler.Default("")),
|
|
|
|
handler.NewColumn(InstanceColumnConsoleAppID, handler.ColumnTypeText, handler.Default("")),
|
|
|
|
handler.NewColumn(InstanceColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(InstanceColumnDefaultLanguage, handler.ColumnTypeText, handler.Default("")),
|
2022-03-24 17:21:34 +01:00
|
|
|
},
|
2023-10-19 12:19:10 +02:00
|
|
|
handler.NewPrimaryKey(InstanceColumnID),
|
2022-03-24 17:21:34 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (p *instanceProjection) Reducers() []handler.AggregateReducer {
|
2022-03-24 17:21:34 +01:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 12:19:10 +02:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-04-21 12:37:39 +02:00
|
|
|
{
|
|
|
|
Event: instance.InstanceAddedEventType,
|
|
|
|
Reduce: p.reduceInstanceAdded,
|
|
|
|
},
|
2022-09-27 07:58:50 +01:00
|
|
|
{
|
|
|
|
Event: instance.InstanceChangedEventType,
|
|
|
|
Reduce: p.reduceInstanceChanged,
|
|
|
|
},
|
2022-10-20 13:36:52 +01:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(InstanceColumnID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
{
|
2022-06-03 14:30:39 +02:00
|
|
|
Event: instance.DefaultOrgSetEventType,
|
|
|
|
Reduce: p.reduceDefaultOrgSet,
|
2022-03-24 17:21:34 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.ProjectSetEventType,
|
|
|
|
Reduce: p.reduceIAMProjectSet,
|
|
|
|
},
|
2022-03-29 11:53:19 +02:00
|
|
|
{
|
|
|
|
Event: instance.ConsoleSetEventType,
|
|
|
|
Reduce: p.reduceConsoleSet,
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
{
|
|
|
|
Event: instance.DefaultLanguageSetEventType,
|
|
|
|
Reduce: p.reduceDefaultLanguageSet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceInstanceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-04-21 12:37:39 +02:00
|
|
|
e, ok := event.(*instance.InstanceAddedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-29nlS", "reduce.wrong.event.type %s", instance.InstanceAddedEventType)
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewCreateStatement(
|
2022-04-21 12:37:39 +02:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(InstanceColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnName, e.Name),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:36:52 +01:00
|
|
|
func reduceInstanceRemovedHelper(instanceIDCol string) func(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
return func(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*instance.InstanceRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-22nlS", "reduce.wrong.event.type %s", instance.InstanceRemovedEventType)
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewDeleteStatement(
|
2022-10-20 13:36:52 +01:00
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(instanceIDCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:58:50 +01:00
|
|
|
func (p *instanceProjection) reduceInstanceChanged(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*instance.InstanceChangedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-so2am1", "reduce.wrong.event.type %s", instance.InstanceChangedEventType)
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2022-09-27 07:58:50 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnName, e.Name),
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceDefaultOrgSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-06-03 14:30:39 +02:00
|
|
|
e, ok := event.(*instance.DefaultOrgSetEvent)
|
2022-03-24 17:21:34 +01:00
|
|
|
if !ok {
|
2022-06-03 14:30:39 +02:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-2n9f2", "reduce.wrong.event.type %s", instance.DefaultOrgSetEventType)
|
2022-03-24 17:21:34 +01:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2022-03-24 17:21:34 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
2022-06-03 14:30:39 +02:00
|
|
|
handler.NewCol(InstanceColumnDefaultOrgID, e.OrgID),
|
2022-03-24 17:21:34 +01:00
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e, ok := event.(*instance.ProjectSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", instance.ProjectSetEventType)
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2022-03-24 17:21:34 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnProjectID, e.ProjectID),
|
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceConsoleSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-29 11:53:19 +02:00
|
|
|
e, ok := event.(*instance.ConsoleSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dgf11", "reduce.wrong.event.type %s", instance.ConsoleSetEventType)
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2022-03-29 11:53:19 +02:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnConsoleID, e.ClientID),
|
2022-04-14 14:19:18 +02:00
|
|
|
handler.NewCol(InstanceColumnConsoleAppID, e.AppID),
|
2022-03-29 11:53:19 +02:00
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-29 11:53:19 +02:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:51:00 +02:00
|
|
|
func (p *instanceProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e, ok := event.(*instance.DefaultLanguageSetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", instance.DefaultLanguageSetEventType)
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewUpdateStatement(
|
2022-03-24 17:21:34 +01:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(InstanceColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(InstanceColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(InstanceColumnDefaultLanguage, e.Language.String()),
|
|
|
|
},
|
2022-04-21 12:37:39 +02:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID),
|
|
|
|
},
|
2022-03-24 17:21:34 +01:00
|
|
|
), nil
|
|
|
|
}
|