166 lines
3.6 KiB
Go
Raw Normal View History

2020-11-06 17:25:07 +01:00
package policy
import (
"context"
"encoding/json"
2020-11-06 17:25:07 +01:00
"github.com/caos/zitadel/internal/errors"
2020-11-06 17:25:07 +01:00
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-11-06 17:25:07 +01:00
)
const (
2020-11-06 22:09:19 +01:00
LabelPolicyAddedEventType = "policy.label.added"
LabelPolicyChangedEventType = "policy.label.changed"
LabelPolicyRemovedEventType = "policy.label.removed"
2020-11-06 17:25:07 +01:00
)
2020-11-06 22:09:19 +01:00
type LabelPolicyAggregate struct {
eventstore.Aggregate
PrimaryColor string
SecondaryColor string
}
2020-11-11 17:51:44 +01:00
type LabelPolicyReadModel struct {
eventstore.ReadModel
PrimaryColor string
SecondaryColor string
}
func (rm *LabelPolicyReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *LabelPolicyAddedEvent:
rm.PrimaryColor = e.PrimaryColor
rm.SecondaryColor = e.SecondaryColor
case *LabelPolicyChangedEvent:
rm.PrimaryColor = e.PrimaryColor
rm.SecondaryColor = e.SecondaryColor
}
}
return rm.ReadModel.Reduce()
}
2020-11-06 17:25:07 +01:00
type LabelPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
2020-11-27 11:30:56 +01:00
PrimaryColor string `json:"primaryColor,omitempty"`
SecondaryColor string `json:"secondaryColor,omitempty"`
2020-11-06 17:25:07 +01:00
}
func (e *LabelPolicyAddedEvent) CheckPrevious() bool {
return true
}
func (e *LabelPolicyAddedEvent) Data() interface{} {
return e
}
func NewLabelPolicyAddedEvent(
ctx context.Context,
primaryColor,
secondaryColor string,
) *LabelPolicyAddedEvent {
return &LabelPolicyAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
LabelPolicyAddedEventType,
),
PrimaryColor: primaryColor,
SecondaryColor: secondaryColor,
}
}
2020-11-06 22:09:19 +01:00
func LabelPolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &LabelPolicyAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "POLIC-puqv4", "unable to unmarshal label policy")
}
return e, nil
}
2020-11-06 22:09:19 +01:00
type LabelPolicyChangedEvent struct {
eventstore.BaseEvent `json:"-"`
2020-11-11 17:51:44 +01:00
PrimaryColor string `json:"primaryColor,omitempty"`
SecondaryColor string `json:"secondaryColor,omitempty"`
2020-11-06 22:09:19 +01:00
}
func (e *LabelPolicyChangedEvent) CheckPrevious() bool {
return true
}
func (e *LabelPolicyChangedEvent) Data() interface{} {
2020-11-11 17:51:44 +01:00
return e
2020-11-06 22:09:19 +01:00
}
func NewLabelPolicyChangedEvent(
ctx context.Context,
current,
changed *LabelPolicyAggregate,
) *LabelPolicyChangedEvent {
2020-11-11 17:51:44 +01:00
e := &LabelPolicyChangedEvent{
2020-11-06 22:09:19 +01:00
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
LabelPolicyChangedEventType,
),
}
2020-11-11 17:51:44 +01:00
if current.PrimaryColor != changed.PrimaryColor {
e.PrimaryColor = changed.PrimaryColor
}
if current.SecondaryColor != changed.SecondaryColor {
e.SecondaryColor = changed.SecondaryColor
}
return e
2020-11-06 22:09:19 +01:00
}
func LabelPolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &LabelPolicyChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "POLIC-qhfFb", "unable to unmarshal label policy")
}
return e, nil
}
2020-11-06 22:09:19 +01:00
type LabelPolicyRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *LabelPolicyRemovedEvent) CheckPrevious() bool {
return true
}
func (e *LabelPolicyRemovedEvent) Data() interface{} {
return nil
}
2020-11-11 17:51:44 +01:00
func NewLabelPolicyRemovedEvent(ctx context.Context) *LabelPolicyRemovedEvent {
2020-11-06 22:09:19 +01:00
return &LabelPolicyRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
2020-11-11 17:51:44 +01:00
LabelPolicyRemovedEventType,
2020-11-06 22:09:19 +01:00
),
}
}
func LabelPolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &LabelPolicyRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}