2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDPConfigWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
State domain.IDPConfigState
|
|
|
|
|
2021-09-10 07:49:49 +00:00
|
|
|
ConfigID string
|
|
|
|
Name string
|
|
|
|
AutoRegister bool
|
|
|
|
StylingType domain.IDPConfigStylingType
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (rm *IDPConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 13:52:13 +00:00
|
|
|
rm.WriteModel.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) Reduce() error {
|
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *idpconfig.IDPConfigAddedEvent:
|
|
|
|
rm.reduceConfigAddedEvent(e)
|
|
|
|
case *idpconfig.IDPConfigChangedEvent:
|
|
|
|
rm.reduceConfigChangedEvent(e)
|
|
|
|
case *idpconfig.IDPConfigDeactivatedEvent:
|
|
|
|
rm.reduceConfigStateChanged(e.ConfigID, domain.IDPConfigStateInactive)
|
|
|
|
case *idpconfig.IDPConfigReactivatedEvent:
|
|
|
|
rm.reduceConfigStateChanged(e.ConfigID, domain.IDPConfigStateActive)
|
|
|
|
case *idpconfig.IDPConfigRemovedEvent:
|
|
|
|
rm.reduceConfigStateChanged(e.ConfigID, domain.IDPConfigStateRemoved)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) reduceConfigAddedEvent(e *idpconfig.IDPConfigAddedEvent) {
|
|
|
|
rm.ConfigID = e.ConfigID
|
|
|
|
rm.Name = e.Name
|
|
|
|
rm.StylingType = e.StylingType
|
2021-09-10 07:49:49 +00:00
|
|
|
rm.AutoRegister = e.AutoRegister
|
2021-01-04 13:52:13 +00:00
|
|
|
rm.State = domain.IDPConfigStateActive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) reduceConfigChangedEvent(e *idpconfig.IDPConfigChangedEvent) {
|
2021-01-20 10:06:52 +00:00
|
|
|
if e.Name != nil {
|
|
|
|
rm.Name = *e.Name
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
if e.StylingType != nil && e.StylingType.Valid() {
|
|
|
|
rm.StylingType = *e.StylingType
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-09-10 07:49:49 +00:00
|
|
|
if e.AutoRegister != nil {
|
|
|
|
rm.AutoRegister = *e.AutoRegister
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) reduceConfigStateChanged(configID string, state domain.IDPConfigState) {
|
|
|
|
rm.State = state
|
|
|
|
}
|