2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDPConfigWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
State domain.IDPConfigState
|
|
|
|
|
2021-09-10 09:49:49 +02:00
|
|
|
ConfigID string
|
|
|
|
Name string
|
|
|
|
AutoRegister bool
|
|
|
|
StylingType domain.IDPConfigStylingType
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (rm *IDPConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 14:52:13 +01: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 09:49:49 +02:00
|
|
|
rm.AutoRegister = e.AutoRegister
|
2021-01-04 14:52:13 +01:00
|
|
|
rm.State = domain.IDPConfigStateActive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) reduceConfigChangedEvent(e *idpconfig.IDPConfigChangedEvent) {
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.Name != nil {
|
|
|
|
rm.Name = *e.Name
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.StylingType != nil && e.StylingType.Valid() {
|
|
|
|
rm.StylingType = *e.StylingType
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-09-10 09:49:49 +02:00
|
|
|
if e.AutoRegister != nil {
|
|
|
|
rm.AutoRegister = *e.AutoRegister
|
|
|
|
}
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) reduceConfigStateChanged(configID string, state domain.IDPConfigState) {
|
|
|
|
rm.State = state
|
|
|
|
}
|
2022-10-17 21:19:15 +02:00
|
|
|
|
|
|
|
func (rm *IDPConfigWriteModel) Exists() bool {
|
|
|
|
return rm.State.Exists()
|
|
|
|
}
|