2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
SetUpStarted domain.Step
|
|
|
|
SetUpDone domain.Step
|
|
|
|
|
|
|
|
GlobalOrgID string
|
|
|
|
ProjectID string
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
func NewIAMWriteModel() *IAMWriteModel {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &IAMWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
2021-01-04 14:52:13 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.ProjectSetEvent:
|
|
|
|
wm.ProjectID = e.ProjectID
|
|
|
|
case *iam.GlobalOrgSetEvent:
|
|
|
|
wm.GlobalOrgID = e.OrgID
|
|
|
|
case *iam.SetupStepEvent:
|
|
|
|
if e.Done {
|
|
|
|
wm.SetUpDone = e.Step
|
|
|
|
} else {
|
|
|
|
wm.SetUpStarted = e.Step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMWriteModel) 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(iam.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(
|
|
|
|
iam.ProjectSetEventType,
|
|
|
|
iam.GlobalOrgSetEventType,
|
|
|
|
iam.SetupStartedEventType,
|
2021-07-06 13:55:57 +02:00
|
|
|
iam.SetupDoneEventType).
|
|
|
|
Builder()
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func IAMAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, iam.AggregateType, iam.AggregateVersion)
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|