2021-01-12 11:59:51 +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"
|
|
|
|
"github.com/caos/zitadel/internal/repository/project"
|
2021-01-12 11:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *ApplicationWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-12 11:59:51 +00:00
|
|
|
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 {
|
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().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
project.ApplicationAddedType,
|
|
|
|
project.ApplicationChangedType,
|
|
|
|
project.ApplicationDeactivatedType,
|
|
|
|
project.ApplicationReactivatedType,
|
|
|
|
project.ApplicationRemovedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
project.ProjectRemovedType).
|
|
|
|
Builder()
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|