2021-01-12 11:59:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/project"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ApplicationWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
AppID string
|
|
|
|
State domain.AppState
|
|
|
|
Name string
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func NewApplicationWriteModelWithAppIDC(projectID, appID, resourceOwner string) *ApplicationWriteModel {
|
2021-01-12 11:59:51 +00:00
|
|
|
return &ApplicationWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
2021-01-28 05:35:26 +00:00
|
|
|
AppID: appID,
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func NewApplicationWriteModel(projectID, resourceOwner string) *ApplicationWriteModel {
|
|
|
|
return &ApplicationWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
func (wm *ApplicationWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.ApplicationAddedEvent:
|
2021-01-28 05:35:26 +00:00
|
|
|
if e.AppID != wm.AppID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.ApplicationChangedEvent:
|
|
|
|
if e.AppID != wm.AppID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.ApplicationDeactivatedEvent:
|
|
|
|
if e.AppID != wm.AppID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.ApplicationReactivatedEvent:
|
|
|
|
if e.AppID != wm.AppID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.ApplicationRemovedEvent:
|
|
|
|
if e.AppID != wm.AppID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.ProjectRemovedEvent:
|
2021-01-12 11:59:51 +00:00
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ApplicationWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.ApplicationAddedEvent:
|
|
|
|
wm.Name = e.Name
|
|
|
|
wm.State = domain.AppStateActive
|
2021-01-28 05:35:26 +00:00
|
|
|
case *project.ApplicationChangedEvent:
|
|
|
|
wm.Name = e.Name
|
|
|
|
case *project.ApplicationDeactivatedEvent:
|
|
|
|
if wm.State == domain.AppStateRemoved {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.AppStateInactive
|
|
|
|
case *project.ApplicationReactivatedEvent:
|
|
|
|
if wm.State == domain.AppStateRemoved {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.AppStateActive
|
|
|
|
case *project.ApplicationRemovedEvent:
|
|
|
|
wm.State = domain.AppStateRemoved
|
|
|
|
case *project.ProjectRemovedEvent:
|
|
|
|
wm.State = domain.AppStateRemoved
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
return wm.WriteModel.Reduce()
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ApplicationWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, project.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
2021-01-28 05:35:26 +00:00
|
|
|
//EventTypes(
|
|
|
|
// project.ApplicationAddedType,
|
|
|
|
// project.ApplicationChangedType,
|
|
|
|
// project.ApplicationDeactivatedType,
|
|
|
|
// project.ApplicationReactivatedType,
|
|
|
|
// project.ApplicationRemovedType,
|
|
|
|
// project.ProjectRemovedType,
|
|
|
|
//)
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|