2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
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) 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).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(
|
|
|
|
org.OrgAddedEventType,
|
|
|
|
org.OrgChangedEventType,
|
|
|
|
org.OrgDomainPrimarySetEventType)
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func OrgAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, org.AggregateType, org.AggregateVersion)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|