2021-01-18 10:24:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-18 10:24:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgLabelPolicyWriteModel struct {
|
|
|
|
LabelPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgLabelPolicyWriteModel(orgID string) *OrgLabelPolicyWriteModel {
|
|
|
|
return &OrgLabelPolicyWriteModel{
|
|
|
|
LabelPolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgLabelPolicyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.LabelPolicyAddedEvent:
|
|
|
|
wm.LabelPolicyWriteModel.AppendEvents(&e.LabelPolicyAddedEvent)
|
|
|
|
case *org.LabelPolicyChangedEvent:
|
|
|
|
wm.LabelPolicyWriteModel.AppendEvents(&e.LabelPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgLabelPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.LabelPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgLabelPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
|
|
|
|
AggregateIDs(wm.LabelPolicyWriteModel.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(
|
|
|
|
org.LabelPolicyAddedEventType,
|
|
|
|
org.LabelPolicyChangedEventType)
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgLabelPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-18 10:24:15 +00:00
|
|
|
primaryColor,
|
|
|
|
secondaryColor string,
|
|
|
|
) (*org.LabelPolicyChangedEvent, bool) {
|
|
|
|
changes := make([]policy.LabelPolicyChanges, 0)
|
|
|
|
if wm.PrimaryColor != primaryColor {
|
|
|
|
changes = append(changes, policy.ChangePrimaryColor(primaryColor))
|
|
|
|
}
|
|
|
|
if wm.SecondaryColor != secondaryColor {
|
|
|
|
changes = append(changes, policy.ChangeSecondaryColor(secondaryColor))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
changedEvent, err := org.NewLabelPolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|