zitadel/internal/v2/repository/idp/config_read_model.go
adlerhurst 4bb9650f27 idp
2020-11-25 20:04:32 +01:00

65 lines
1.4 KiB
Go

package idp
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
)
type ConfigReadModel struct {
eventstore.ReadModel
Type ConfigType
State ConfigState
ConfigID string
Name string
StylingType StylingType
ProviderType ProviderType
OIDCConfig *oidc.ConfigReadModel
}
func (rm *ConfigReadModel) AppendEvents(events ...eventstore.EventReader) {
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch event.(type) {
case *oidc.ConfigAddedEvent:
rm.OIDCConfig = &oidc.ConfigReadModel{}
rm.OIDCConfig.AppendEvents(event)
case *oidc.ConfigChangedEvent:
rm.OIDCConfig.AppendEvents(event)
}
}
}
func (rm *ConfigReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
case *ConfigChangedEvent:
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
case *ConfigDeactivatedEvent:
rm.State = ConfigStateInactive
case *ConfigReactivatedEvent:
rm.State = ConfigStateActive
case *ConfigRemovedEvent:
rm.State = ConfigStateRemoved
case *oidc.ConfigAddedEvent:
rm.Type = ConfigTypeOIDC
}
}
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
}
return rm.ReadModel.Reduce()
}