zitadel/internal/v2/repository/idp/config_write_model.go

75 lines
1.8 KiB
Go
Raw Normal View History

2020-11-25 19:04:32 +00:00
package idp
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
)
type ConfigWriteModel struct {
eventstore.WriteModel
2020-11-27 10:30:56 +00:00
State ConfigState
2020-11-25 19:04:32 +00:00
ConfigID string
Name string
StylingType StylingType
2020-11-27 10:30:56 +00:00
OIDCConfig *oidc.ConfigWriteModel
2020-11-25 19:04:32 +00:00
}
func (rm *ConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
rm.WriteModel.AppendEvents(events...)
for _, event := range events {
switch event.(type) {
case *oidc.ConfigAddedEvent:
2020-11-27 10:30:56 +00:00
rm.OIDCConfig = new(oidc.ConfigWriteModel)
2020-11-25 19:04:32 +00:00
rm.OIDCConfig.AppendEvents(event)
case *oidc.ConfigChangedEvent:
rm.OIDCConfig.AppendEvents(event)
}
}
}
func (rm *ConfigWriteModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigAddedEvent(e)
2020-11-25 19:04:32 +00:00
case *ConfigChangedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigChangedEvent(e)
case *ConfigDeactivatedEvent:
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateInactive)
case *ConfigReactivatedEvent:
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateActive)
case *ConfigRemovedEvent:
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateRemoved)
2020-11-25 19:04:32 +00:00
}
}
2020-11-27 10:30:56 +00:00
if rm.OIDCConfig != nil {
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
}
2020-11-25 19:04:32 +00:00
}
return rm.WriteModel.Reduce()
}
2020-11-27 10:30:56 +00:00
func (rm *ConfigWriteModel) reduceConfigAddedEvent(e *ConfigAddedEvent) {
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
}
func (rm *ConfigWriteModel) reduceConfigChangedEvent(e *ConfigChangedEvent) {
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
}
func (rm *ConfigWriteModel) reduceConfigStateChanged(configID string, state ConfigState) {
rm.State = state
}