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"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2022-02-16 15:49:17 +00:00
|
|
|
"golang.org/x/text/language"
|
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
|
|
|
|
|
|
|
|
SetUpStarted domain.Step
|
|
|
|
SetUpDone domain.Step
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
GlobalOrgID string
|
|
|
|
ProjectID string
|
|
|
|
DefaultLanguage language.Tag
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func NewInstanceWriteModel() *InstanceWriteModel {
|
|
|
|
return &InstanceWriteModel{
|
2021-01-04 13:52:13 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
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-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
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SetupStepEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if e.Done {
|
|
|
|
wm.SetUpDone = e.Step
|
|
|
|
} else {
|
|
|
|
wm.SetUpStarted = e.Step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-03-24 16:21:34 +00:00
|
|
|
instance.ProjectSetEventType,
|
|
|
|
instance.GlobalOrgSetEventType,
|
|
|
|
instance.DefaultLanguageSetEventType,
|
|
|
|
instance.SetupStartedEventType,
|
|
|
|
instance.SetupDoneEventType).
|
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
|
|
|
}
|