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

68 lines
1.4 KiB
Go
Raw Normal View History

2020-11-17 12:44:37 +00:00
package idp
2020-11-18 20:22:15 +00:00
import (
2020-11-25 19:04:32 +00:00
"encoding/json"
2020-11-18 20:22:15 +00:00
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
2020-11-25 19:04:32 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-11-18 20:22:15 +00:00
)
2020-11-17 12:44:37 +00:00
2020-11-18 20:22:15 +00:00
type ConfigChangedEvent struct {
2020-11-17 12:44:37 +00:00
eventstore.BaseEvent `json:"-"`
2020-11-25 19:04:32 +00:00
ConfigID string `json:"idpConfigId"`
Name string `json:"name,omitempty"`
2020-11-18 20:22:15 +00:00
StylingType StylingType `json:"stylingType,omitempty"`
2020-11-17 12:44:37 +00:00
}
2020-11-18 20:22:15 +00:00
func NewConfigChangedEvent(
2020-11-17 12:44:37 +00:00
base *eventstore.BaseEvent,
2020-11-25 19:04:32 +00:00
current *ConfigWriteModel,
name string,
stylingType StylingType,
2020-11-18 20:22:15 +00:00
) (*ConfigChangedEvent, error) {
2020-11-17 12:44:37 +00:00
2020-11-18 20:22:15 +00:00
change := &ConfigChangedEvent{
2020-11-17 12:44:37 +00:00
BaseEvent: *base,
2020-11-25 19:04:32 +00:00
ConfigID: current.ConfigID,
2020-11-18 20:22:15 +00:00
}
2020-11-25 19:04:32 +00:00
hasChanged := false
2020-11-18 20:22:15 +00:00
2020-11-25 19:04:32 +00:00
if current.Name != name {
change.Name = name
hasChanged = true
2020-11-18 20:22:15 +00:00
}
2020-11-25 19:04:32 +00:00
if stylingType != current.StylingType {
change.StylingType = stylingType
hasChanged = true
2020-11-18 20:22:15 +00:00
}
2020-11-25 19:04:32 +00:00
if !hasChanged {
2020-11-18 20:22:15 +00:00
return nil, errors.ThrowPreconditionFailed(nil, "IDP-UBJbB", "Errors.NoChanges")
}
return change, nil
2020-11-17 12:44:37 +00:00
}
2020-11-18 20:22:15 +00:00
func (e *ConfigChangedEvent) CheckPrevious() bool {
2020-11-17 12:44:37 +00:00
return true
}
2020-11-18 20:22:15 +00:00
func (e *ConfigChangedEvent) Data() interface{} {
2020-11-17 12:44:37 +00:00
return e
}
2020-11-25 19:04:32 +00:00
2020-11-26 12:14:07 +00:00
func ConfigChangedEventMapper(event *repository.Event) (*ConfigChangedEvent, error) {
2020-11-25 19:04:32 +00:00
e := &ConfigChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "OIDC-plaBZ", "unable to unmarshal event")
}
return e, nil
}