2020-05-11 10:16:29 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
2020-05-11 10:16:29 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
projectRoleTable = "management.project_roles"
|
|
|
|
)
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
type ProjectRole struct {
|
|
|
|
handler
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
2020-12-18 15:47:45 +00:00
|
|
|
subscription *eventstore.Subscription
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newProjectRole(
|
|
|
|
handler handler,
|
|
|
|
projectEvents *proj_event.ProjectEventstore,
|
|
|
|
) *ProjectRole {
|
|
|
|
h := &ProjectRole{
|
|
|
|
handler: handler,
|
|
|
|
projectEvents: projectEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
h.subscribe()
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ProjectRole) subscribe() {
|
|
|
|
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range m.subscription.Events {
|
|
|
|
query.ReduceEvent(m, event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
|
|
|
|
func (p *ProjectRole) ViewModel() string {
|
|
|
|
return projectRoleTable
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *ProjectRole) AggregateTypes() []models.AggregateType {
|
|
|
|
return []models.AggregateType{es_model.ProjectAggregate}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectRole) CurrentSequence(event *models.Event) (uint64, error) {
|
|
|
|
sequence, err := p.view.GetLatestProjectRoleSequence(string(event.AggregateType))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func (p *ProjectRole) EventQuery() (*models.SearchQuery, error) {
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, err := p.view.GetLatestProjectRoleSequence("")
|
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-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-18 15:47:45 +00:00
|
|
|
return p.view.DeleteProjectRole(event.AggregateID, event.ResourceOwner, role.Key, event)
|
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-18 15:47:45 +00:00
|
|
|
return p.view.ProcessedProjectRoleSequence(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return p.view.PutProjectRole(role, event)
|
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)
|
|
|
|
}
|