fixup! added first event

This commit is contained in:
Iraq Jaber
2025-07-23 11:47:23 +01:00
parent 46958de19f
commit 666243cf27
2 changed files with 459 additions and 161 deletions

View File

@@ -4,8 +4,10 @@ import (
"context"
"github.com/zitadel/zitadel/backend/v3/domain"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/repository/idpconfig"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/org"
"github.com/zitadel/zitadel/internal/zerrors"
@@ -36,26 +38,26 @@ func (p *idpRelationalProjection) Reducers() []handler.AggregateReducer {
Event: instance.IDPConfigAddedEventType,
Reduce: p.reduceIDPRelationalAdded,
},
// {
// Event: instance.IDPConfigChangedEventType,
// Reduce: p.reduceIDPChanged,
// },
// {
// Event: instance.IDPConfigDeactivatedEventType,
// Reduce: p.reduceIDPDeactivated,
// },
// {
// Event: instance.IDPConfigReactivatedEventType,
// Reduce: p.reduceIDPReactivated,
// },
// {
// Event: instance.IDPConfigRemovedEventType,
// Reduce: p.reduceIDPRemoved,
// },
// {
// Event: instance.IDPOIDCConfigAddedEventType,
// Reduce: p.reduceOIDCConfigAdded,
// },
{
Event: instance.IDPConfigChangedEventType,
Reduce: p.reduceIDPRelationalChanged,
},
{
Event: instance.IDPConfigDeactivatedEventType,
Reduce: p.reduceIDRelationalPDeactivated,
},
{
Event: instance.IDPConfigReactivatedEventType,
Reduce: p.reduceIDPRelationalReactivated,
},
{
Event: instance.IDPConfigRemovedEventType,
Reduce: p.reduceIDPRelationalRemoved,
},
{
Event: instance.IDPOIDCConfigAddedEventType,
Reduce: p.reduceOIDCRelationalConfigAdded,
},
// {
// Event: instance.IDPOIDCConfigChangedEventType,
// Reduce: p.reduceOIDCConfigChanged,
@@ -80,7 +82,7 @@ func (p *idpRelationalProjection) Reducers() []handler.AggregateReducer {
func (p *idpRelationalProjection) reduceIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.IDPConfigAddedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-uYR5R", "reduce.wrong.event.type %s", org.OrgAddedEventType)
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-fcUdQ", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigAddedEventType, instance.IDPConfigAddedEventType})
}
return handler.NewCreateStatement(
@@ -94,162 +96,149 @@ func (p *idpRelationalProjection) reduceIDPRelationalAdded(event eventstore.Even
handler.NewCol(IDPStylingTypeCol, e.StylingType),
handler.NewCol(IDPRelationalAllowAutoCreationCol, e.AutoRegister),
handler.NewCol(IDPTypeCol, domain.IDPTypeOIDC.String()),
handler.NewCol(CreatedAt, e.CreationDate()),
handler.NewCol(UpdatedAt, e.CreationDate()),
handler.NewCol(CreatedAt, e.CreationDate()),
},
), nil
}
// func (p *idpRelationalProjection) reduceIDPChanged(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.IDPConfigChangedEvent
// switch e := event.(type) {
// case *org.IDPConfigChangedEvent:
// idpEvent = e.IDPConfigChangedEvent
// case *instance.IDPConfigChangedEvent:
// idpEvent = e.IDPConfigChangedEvent
// default:
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-NVvJD", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigChangedEventType, instance.IDPConfigChangedEventType})
// }
func (p *idpRelationalProjection) reduceIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.IDPConfigChangedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-NVvJD", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigChangedEventType, instance.IDPConfigChangedEventType})
}
// cols := make([]handler.Column, 0, 5)
// if idpEvent.Name != nil {
// cols = append(cols, handler.NewCol(IDPNameCol, *idpEvent.Name))
// }
// if idpEvent.StylingType != nil {
// cols = append(cols, handler.NewCol(IDPStylingTypeCol, *idpEvent.StylingType))
// }
// if idpEvent.AutoRegister != nil {
// cols = append(cols, handler.NewCol(IDPAutoRegisterCol, *idpEvent.AutoRegister))
// }
// if len(cols) == 0 {
// return handler.NewNoOpStatement(&idpEvent), nil
// }
cols := make([]handler.Column, 0, 5)
if e.Name != nil {
cols = append(cols, handler.NewCol(IDPNameCol, e.Name))
}
if e.StylingType != nil {
cols = append(cols, handler.NewCol(IDPStylingTypeCol, e.StylingType))
}
if e.AutoRegister != nil {
cols = append(cols, handler.NewCol(IDPRelationalAllowAutoCreationCol, e.AutoRegister))
}
if len(cols) == 0 {
return handler.NewNoOpStatement(e), nil
}
// cols = append(cols,
// handler.NewCol(IDPChangeDateCol, idpEvent.CreationDate()),
// handler.NewCol(IDPSequenceCol, idpEvent.Sequence()),
// )
cols = append(cols,
handler.NewCol(UpdatedAt, e.CreationDate()),
)
// return handler.NewUpdateStatement(
// &idpEvent,
// cols,
// []handler.Condition{
// handler.NewCond(IDPIDCol, idpEvent.ConfigID),
// handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID),
// },
// ), nil
// }
return handler.NewUpdateStatement(
e,
cols,
[]handler.Condition{
handler.NewCond(IDPIDCol, e.ConfigID),
handler.NewCond(IDPInstanceIDCol, e.Aggregate().InstanceID),
},
), nil
}
// func (p *idpRelationalProjection) reduceIDPDeactivated(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.IDPConfigDeactivatedEvent
// switch e := event.(type) {
// case *org.IDPConfigDeactivatedEvent:
// idpEvent = e.IDPConfigDeactivatedEvent
// case *instance.IDPConfigDeactivatedEvent:
// idpEvent = e.IDPConfigDeactivatedEvent
// default:
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-94O5l", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigDeactivatedEventType, instance.IDPConfigDeactivatedEventType})
// }
func (p *idpRelationalProjection) reduceIDRelationalPDeactivated(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.IDPConfigDeactivatedEvent
// switch e := event.(type) {
// case *org.IDPConfigDeactivatedEvent:
// idpEvent = e.IDPConfigDeactivatedEvent
// case *instance.IDPConfigDeactivatedEvent:
// idpEvent = e.IDPConfigDeactivatedEvent
// default:
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-94O5l", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigDeactivatedEventType, instance.IDPConfigDeactivatedEventType})
// }
// return handler.NewUpdateStatement(
// &idpEvent,
// []handler.Column{
// handler.NewCol(IDPStateCol, domain.IDPConfigStateInactive),
// handler.NewCol(IDPChangeDateCol, idpEvent.CreationDate()),
// handler.NewCol(IDPSequenceCol, idpEvent.Sequence()),
// },
// []handler.Condition{
// handler.NewCond(IDPIDCol, idpEvent.ConfigID),
// handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID),
// },
// ), nil
// }
e, ok := event.(*instance.IDPConfigDeactivatedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-94O5l", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigDeactivatedEventType, instance.IDPConfigDeactivatedEventType})
}
// func (p *idpRelationalProjection) reduceIDPReactivated(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.IDPConfigReactivatedEvent
// switch e := event.(type) {
// case *org.IDPConfigReactivatedEvent:
// idpEvent = e.IDPConfigReactivatedEvent
// case *instance.IDPConfigReactivatedEvent:
// idpEvent = e.IDPConfigReactivatedEvent
// default:
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-I8QyS", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigReactivatedEventType, instance.IDPConfigReactivatedEventType})
// }
return handler.NewUpdateStatement(
e,
[]handler.Column{
handler.NewCol(IDPStateCol, domain.IDPStateInactive.String()),
handler.NewCol(UpdatedAt, e.CreationDate()),
},
[]handler.Condition{
handler.NewCond(IDPIDCol, e.ConfigID),
handler.NewCond(IDPInstanceIDCol, e.Aggregate().InstanceID),
},
), nil
}
// return handler.NewUpdateStatement(
// &idpEvent,
// []handler.Column{
// handler.NewCol(IDPStateCol, domain.IDPConfigStateActive),
// handler.NewCol(IDPChangeDateCol, idpEvent.CreationDate()),
// handler.NewCol(IDPSequenceCol, idpEvent.Sequence()),
// },
// []handler.Condition{
// handler.NewCond(IDPIDCol, idpEvent.ConfigID),
// handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID),
// },
// ), nil
// }
func (p *idpRelationalProjection) reduceIDPRelationalReactivated(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.IDPConfigReactivatedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-I8QyS", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigReactivatedEventType, instance.IDPConfigReactivatedEventType})
}
// func (p *idpRelationalProjection) reduceIDPRemoved(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.IDPConfigRemovedEvent
// switch e := event.(type) {
// case *org.IDPConfigRemovedEvent:
// idpEvent = e.IDPConfigRemovedEvent
// case *instance.IDPConfigRemovedEvent:
// idpEvent = e.IDPConfigRemovedEvent
// default:
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-B4zy8", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigRemovedEventType, instance.IDPConfigRemovedEventType})
// }
return handler.NewUpdateStatement(
e,
[]handler.Column{
handler.NewCol(IDPStateCol, domain.IDPStateActive.String()),
handler.NewCol(UpdatedAt, e.CreationDate()),
},
[]handler.Condition{
handler.NewCond(IDPIDCol, e.ConfigID),
handler.NewCond(IDPInstanceIDCol, e.Aggregate().InstanceID),
},
), nil
}
// return handler.NewDeleteStatement(
// &idpEvent,
// []handler.Condition{
// handler.NewCond(IDPIDCol, idpEvent.ConfigID),
// handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID),
// },
// ), nil
// }
func (p *idpRelationalProjection) reduceIDPRelationalRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.IDPConfigRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-B4zy8", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigRemovedEventType, instance.IDPConfigRemovedEventType})
}
// func (p *idpRelationalProjection) reduceOIDCConfigAdded(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.OIDCConfigAddedEvent
// switch e := event.(type) {
// case *org.IDPOIDCConfigAddedEvent:
// idpEvent = e.OIDCConfigAddedEvent
// case *instance.IDPOIDCConfigAddedEvent:
// idpEvent = e.OIDCConfigAddedEvent
// default:
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-2FuAA", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPOIDCConfigAddedEventType, instance.IDPOIDCConfigAddedEventType})
// }
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(IDPIDCol, e.ConfigID),
handler.NewCond(IDPInstanceIDCol, e.Aggregate().InstanceID),
},
), nil
}
// return handler.NewMultiStatement(&idpEvent,
// handler.AddUpdateStatement(
// []handler.Column{
// handler.NewCol(IDPChangeDateCol, idpEvent.CreationDate()),
// handler.NewCol(IDPSequenceCol, idpEvent.Sequence()),
// handler.NewCol(IDPTypeCol, domain.IDPConfigTypeOIDC),
// },
// []handler.Condition{
// handler.NewCond(IDPIDCol, idpEvent.IDPConfigID),
// handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID),
// },
// ),
// handler.AddCreateStatement(
// []handler.Column{
// handler.NewCol(OIDCConfigIDPIDCol, idpEvent.IDPConfigID),
// handler.NewCol(OIDCConfigInstanceIDCol, idpEvent.Aggregate().InstanceID),
// handler.NewCol(OIDCConfigClientIDCol, idpEvent.ClientID),
// handler.NewCol(OIDCConfigClientSecretCol, idpEvent.ClientSecret),
// handler.NewCol(OIDCConfigIssuerCol, idpEvent.Issuer),
// handler.NewCol(OIDCConfigScopesCol, database.TextArray[string](idpEvent.Scopes)),
// handler.NewCol(OIDCConfigDisplayNameMappingCol, idpEvent.IDPDisplayNameMapping),
// handler.NewCol(OIDCConfigUsernameMappingCol, idpEvent.UserNameMapping),
// handler.NewCol(OIDCConfigAuthorizationEndpointCol, idpEvent.AuthorizationEndpoint),
// handler.NewCol(OIDCConfigTokenEndpointCol, idpEvent.TokenEndpoint),
// },
// handler.WithTableSuffix(IDPOIDCSuffix),
// ),
// ), nil
// }
func (p *idpRelationalProjection) reduceOIDCRelationalConfigAdded(event eventstore.Event) (*handler.Statement, error) {
var idpEvent idpconfig.OIDCConfigAddedEvent
switch e := event.(type) {
case *org.IDPOIDCConfigAddedEvent:
idpEvent = e.OIDCConfigAddedEvent
case *instance.IDPOIDCConfigAddedEvent:
idpEvent = e.OIDCConfigAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-2FuAA", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPOIDCConfigAddedEventType, instance.IDPOIDCConfigAddedEventType})
}
return handler.NewMultiStatement(&idpEvent,
handler.AddUpdateStatement(
[]handler.Column{
handler.NewCol(IDPChangeDateCol, idpEvent.CreationDate()),
handler.NewCol(IDPSequenceCol, idpEvent.Sequence()),
handler.NewCol(IDPTypeCol, domain.IDPConfigTypeOIDC),
},
[]handler.Condition{
handler.NewCond(IDPIDCol, idpEvent.IDPConfigID),
handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID),
},
),
handler.AddCreateStatement(
[]handler.Column{
handler.NewCol(OIDCConfigIDPIDCol, idpEvent.IDPConfigID),
handler.NewCol(OIDCConfigInstanceIDCol, idpEvent.Aggregate().InstanceID),
handler.NewCol(OIDCConfigClientIDCol, idpEvent.ClientID),
handler.NewCol(OIDCConfigClientSecretCol, idpEvent.ClientSecret),
handler.NewCol(OIDCConfigIssuerCol, idpEvent.Issuer),
handler.NewCol(OIDCConfigScopesCol, database.TextArray[string](idpEvent.Scopes)),
handler.NewCol(OIDCConfigDisplayNameMappingCol, idpEvent.IDPDisplayNameMapping),
handler.NewCol(OIDCConfigUsernameMappingCol, idpEvent.UserNameMapping),
handler.NewCol(OIDCConfigAuthorizationEndpointCol, idpEvent.AuthorizationEndpoint),
handler.NewCol(OIDCConfigTokenEndpointCol, idpEvent.TokenEndpoint),
},
handler.WithTableSuffix(IDPOIDCSuffix),
),
), nil
}
// func (p *idpRelationalProjection) reduceOIDCConfigChanged(event eventstore.Event) (*handler.Statement, error) {
// var idpEvent idpconfig.OIDCConfigChangedEvent