2020-05-11 10:16:29 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-10-27 14:53:36 +00:00
|
|
|
"context"
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/logging"
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
view_model "github.com/caos/zitadel/internal/project/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
handler
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
applicationTable = "management.applications"
|
|
|
|
)
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) ViewModel() string {
|
2020-05-11 10:16:29 +00:00
|
|
|
return applicationTable
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := a.view.GetLatestApplicationSequence()
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
return eventsourcing.ProjectQuery(sequence.CurrentSequence), nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) Reduce(event *models.Event) (err error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
app := new(view_model.ApplicationView)
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.ApplicationAdded:
|
2020-10-27 14:53:36 +00:00
|
|
|
project, err := a.projectEvents.ProjectByID(context.Background(), event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
app.ProjectRoleCheck = project.ProjectRoleCheck
|
|
|
|
app.ProjectRoleAssertion = project.ProjectRoleAssertion
|
|
|
|
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.AppendEvent(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ApplicationChanged,
|
|
|
|
es_model.OIDCConfigAdded,
|
|
|
|
es_model.OIDCConfigChanged,
|
|
|
|
es_model.ApplicationDeactivated,
|
|
|
|
es_model.ApplicationReactivated:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 14:53:36 +00:00
|
|
|
app, err = a.view.ApplicationByID(event.AggregateID, app.ID)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.AppendEvent(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ApplicationRemoved:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = app.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.DeleteApplication(app.ID, event.Sequence)
|
2020-10-16 05:49:38 +00:00
|
|
|
case es_model.ProjectChanged:
|
2020-10-27 14:53:36 +00:00
|
|
|
apps, err := a.view.ApplicationsByProjectID(event.AggregateID)
|
2020-10-16 05:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(apps) == 0 {
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.ProcessedApplicationSequence(event.Sequence)
|
2020-10-16 05:49:38 +00:00
|
|
|
}
|
|
|
|
for _, app := range apps {
|
|
|
|
if err := app.AppendEvent(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.PutApplications(apps, event.Sequence)
|
2020-08-05 16:32:25 +00:00
|
|
|
case es_model.ProjectRemoved:
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.DeleteApplicationsByProjectID(event.AggregateID)
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.ProcessedApplicationSequence(event.Sequence)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 14:53:36 +00:00
|
|
|
return a.view.PutApplication(app)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (a *Application) OnError(event *models.Event, spoolerError error) error {
|
2020-05-12 04:30:53 +00:00
|
|
|
logging.LogWithFields("SPOOL-ls9ew", "id", event.AggregateID).WithError(spoolerError).Warn("something went wrong in project app handler")
|
2020-10-27 14:53:36 +00:00
|
|
|
return spooler.HandleError(event, spoolerError, a.view.GetLatestApplicationFailedEvent, a.view.ProcessedApplicationFailedEvent, a.view.ProcessedApplicationSequence, a.errorCountUntilSkip)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|