2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-05 05:58:09 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type InstanceWriteModel struct {
|
2021-01-04 13:52:13 +00:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
Name string
|
|
|
|
State domain.InstanceState
|
|
|
|
GeneratedDomain string
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
GlobalOrgID string
|
|
|
|
ProjectID string
|
|
|
|
DefaultLanguage language.Tag
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceWriteModel(instanceID string) *InstanceWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceWriteModel{
|
2021-01-04 13:52:13 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: instanceID,
|
|
|
|
ResourceOwner: instanceID,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceWriteModel) Reduce() error {
|
2021-01-04 13:52:13 +00:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2022-04-05 05:58:09 +00:00
|
|
|
case *instance.InstanceAddedEvent:
|
|
|
|
wm.Name = e.Name
|
|
|
|
wm.State = domain.InstanceStateActive
|
|
|
|
case *instance.InstanceChangedEvent:
|
|
|
|
wm.Name = e.Name
|
|
|
|
case *instance.InstanceRemovedEvent:
|
|
|
|
wm.State = domain.InstanceStateRemoved
|
|
|
|
case *instance.DomainAddedEvent:
|
|
|
|
if !e.Generated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.GeneratedDomain = e.Domain
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.ProjectSetEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
wm.ProjectID = e.ProjectID
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.GlobalOrgSetEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
wm.GlobalOrgID = e.OrgID
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.DefaultLanguageSetEvent:
|
2022-02-16 15:49:17 +00:00
|
|
|
wm.DefaultLanguage = e.Language
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-07-06 11:55:57 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
2022-04-05 05:58:09 +00:00
|
|
|
instance.InstanceAddedEventType,
|
|
|
|
instance.InstanceChangedEventType,
|
|
|
|
instance.InstanceRemovedEventType,
|
|
|
|
instance.InstanceDomainAddedEventType,
|
|
|
|
instance.InstanceDomainRemovedEventType,
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.ProjectSetEventType,
|
|
|
|
instance.GlobalOrgSetEventType,
|
2022-05-04 15:09:49 +00:00
|
|
|
instance.DefaultLanguageSetEventType).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func InstanceAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, instance.AggregateType, instance.AggregateVersion)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|