2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
2021-01-08 11:33:45 +01:00
|
|
|
type OrgWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-01-15 09:32:59 +01:00
|
|
|
Name string
|
|
|
|
State domain.OrgState
|
|
|
|
PrimaryDomain string
|
2021-01-08 11:33:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgWriteModel(orgID string) *OrgWriteModel {
|
|
|
|
return &OrgWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
2021-01-08 11:33:45 +01: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
|
2021-03-15 12:51:15 +01:00
|
|
|
case *org.OrgDeactivatedEvent:
|
|
|
|
wm.State = domain.OrgStateInactive
|
2021-04-15 18:15:49 +02:00
|
|
|
case *org.OrgReactivatedEvent:
|
|
|
|
wm.State = domain.OrgStateActive
|
2022-07-07 15:13:17 +02:00
|
|
|
case *org.OrgRemovedEvent:
|
|
|
|
wm.State = domain.OrgStateRemoved
|
2021-01-08 11:33:45 +01:00
|
|
|
case *org.OrgChangedEvent:
|
|
|
|
wm.Name = e.Name
|
2021-01-15 09:32:59 +01:00
|
|
|
case *org.DomainPrimarySetEvent:
|
|
|
|
wm.PrimaryDomain = e.Domain
|
2021-01-08 11:33:45 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 17:01:17 +01:00
|
|
|
return wm.WriteModel.Reduce()
|
2021-01-08 11:33:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(
|
|
|
|
org.OrgAddedEventType,
|
|
|
|
org.OrgChangedEventType,
|
2022-07-07 15:13:17 +02:00
|
|
|
org.OrgDeactivatedEventType,
|
|
|
|
org.OrgReactivatedEventType,
|
|
|
|
org.OrgRemovedEventType,
|
2021-07-06 13:55:57 +02:00
|
|
|
org.OrgDomainPrimarySetEventType).
|
|
|
|
Builder()
|
2021-01-08 11:33:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func OrgAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, org.AggregateType, org.AggregateVersion)
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|