2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-08 10:33:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/iam"
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/repository/org"
|
|
|
|
)
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
type OrgWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
Name string
|
|
|
|
State domain.OrgState
|
|
|
|
PrimaryDomain string
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgWriteModel(orgID string) *OrgWriteModel {
|
|
|
|
return &OrgWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
2021-01-08 10:33:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
wm.WriteModel.AppendEvents(events...)
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.OrgAddedEvent,
|
|
|
|
*iam.LabelPolicyChangedEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-01-15 08:32:59 +00:00
|
|
|
case *org.DomainPrimarySetEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.OrgAddedEvent:
|
|
|
|
wm.Name = e.Name
|
|
|
|
wm.State = domain.OrgStateActive
|
|
|
|
case *org.OrgChangedEvent:
|
|
|
|
wm.Name = e.Name
|
2021-01-15 08:32:59 +00:00
|
|
|
case *org.DomainPrimarySetEvent:
|
|
|
|
wm.PrimaryDomain = e.Domain
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OrgAggregateFromWriteModel(wm *eventstore.WriteModel) *org.Aggregate {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &org.Aggregate{
|
|
|
|
Aggregate: *eventstore.AggregateFromWriteModel(wm, org.AggregateType, org.AggregateVersion),
|
|
|
|
}
|
|
|
|
}
|