2020-05-11 10:16:29 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 ProjectRole struct {
|
|
|
|
handler
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
projectRoleTable = "management.project_roles"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *ProjectRole) ViewModel() string {
|
|
|
|
return projectRoleTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectRole) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := p.view.GetLatestProjectRoleSequence()
|
|
|
|
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-06-23 12:47:47 +00:00
|
|
|
func (p *ProjectRole) Reduce(event *models.Event) (err error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
role := new(view_model.ProjectRoleView)
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.ProjectRoleAdded:
|
2020-08-13 06:28:18 +00:00
|
|
|
err = role.AppendEvent(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ProjectRoleChanged:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = role.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
role, err = p.view.ProjectRoleByIDs(event.AggregateID, event.ResourceOwner, role.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-13 07:43:47 +00:00
|
|
|
err = role.AppendEvent(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
case es_model.ProjectRoleRemoved:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = role.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return p.view.DeleteProjectRole(event.AggregateID, event.ResourceOwner, role.Key, event.Sequence, event.CreationDate)
|
2020-08-05 16:32:25 +00:00
|
|
|
case es_model.ProjectRemoved:
|
|
|
|
return p.view.DeleteProjectRolesByProjectID(event.AggregateID)
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-12-02 07:50:59 +00:00
|
|
|
return p.view.ProcessedProjectRoleSequence(event.Sequence, event.CreationDate)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return p.view.PutProjectRole(role, event.CreationDate)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectRole) OnError(event *models.Event, err error) error {
|
|
|
|
logging.LogWithFields("SPOOL-lso9w", "id", event.AggregateID).WithError(err).Warn("something went wrong in project role handler")
|
2020-05-12 04:30:53 +00:00
|
|
|
return spooler.HandleError(event, err, p.view.GetLatestProjectRoleFailedEvent, p.view.ProcessedProjectRoleFailedEvent, p.view.ProcessedProjectRoleSequence, p.errorCountUntilSkip)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
|
|
|
|
func (p *ProjectRole) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(p.view.UpdateProjectRoleSpoolerRunTimestamp)
|
|
|
|
}
|