2020-04-07 11:23:04 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-15 15:11:42 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
2020-04-07 16:36:37 +00:00
|
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
2020-04-07 11:23:04 +00:00
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectEventstore struct {
|
|
|
|
es_int.Eventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectConfig struct {
|
|
|
|
es_int.Eventstore
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartProject(conf ProjectConfig) (*ProjectEventstore, error) {
|
|
|
|
return &ProjectEventstore{Eventstore: conf.Eventstore}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) ProjectByID(ctx context.Context, project *proj_model.Project) (*proj_model.Project, error) {
|
2020-04-07 16:36:37 +00:00
|
|
|
query, err := ProjectByIDQuery(project.ID, project.Sequence)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
|
|
|
p := ProjectFromModel(project)
|
|
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, p.AppendEvents, query)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
return ProjectToModel(p), nil
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) CreateProject(ctx context.Context, project *proj_model.Project) (*proj_model.Project, error) {
|
|
|
|
if !project.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
|
|
|
}
|
|
|
|
project.State = proj_model.Active
|
|
|
|
repoProject := ProjectFromModel(project)
|
2020-04-07 16:36:37 +00:00
|
|
|
|
|
|
|
createAggregate := ProjectCreateAggregate(es.AggregateCreator(), repoProject)
|
|
|
|
err := es_sdk.Push(ctx, es.PushAggregates, repoProject.AppendEvents, createAggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ProjectToModel(repoProject), nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:11:42 +00:00
|
|
|
func (es *ProjectEventstore) UpdateProject(ctx context.Context, project *proj_model.Project) (*proj_model.Project, error) {
|
2020-04-07 16:36:37 +00:00
|
|
|
if !project.IsValid() {
|
2020-04-07 11:23:04 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
|
|
|
}
|
2020-04-15 15:11:42 +00:00
|
|
|
existingProject, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: project.ID, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
repoExisting := ProjectFromModel(existingProject)
|
|
|
|
repoNew := ProjectFromModel(project)
|
|
|
|
|
|
|
|
updateAggregate := ProjectUpdateAggregate(es.AggregateCreator(), repoExisting, repoNew)
|
2020-04-15 15:11:42 +00:00
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, updateAggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
return ProjectToModel(repoExisting), nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:11:42 +00:00
|
|
|
func (es *ProjectEventstore) DeactivateProject(ctx context.Context, id string) (*proj_model.Project, error) {
|
|
|
|
existing, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: id, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
if !existing.IsActive() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be active")
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
repoExisting := ProjectFromModel(existing)
|
2020-04-07 16:36:37 +00:00
|
|
|
aggregate := ProjectDeactivateAggregate(es.AggregateCreator(), repoExisting)
|
|
|
|
es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, aggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
return ProjectToModel(repoExisting), nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:11:42 +00:00
|
|
|
func (es *ProjectEventstore) ReactivateProject(ctx context.Context, id string) (*proj_model.Project, error) {
|
|
|
|
existing, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: id, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
if existing.IsActive() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be inactive")
|
|
|
|
}
|
2020-04-07 16:36:37 +00:00
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
repoExisting := ProjectFromModel(existing)
|
2020-04-07 16:36:37 +00:00
|
|
|
aggregate := ProjectReactivateAggregate(es.AggregateCreator(), repoExisting)
|
|
|
|
es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, aggregate)
|
2020-04-07 11:23:04 +00:00
|
|
|
return ProjectToModel(repoExisting), nil
|
|
|
|
}
|
2020-04-15 15:11:42 +00:00
|
|
|
|
|
|
|
func (es *ProjectEventstore) ProjectMemberByIDs(ctx context.Context, member *proj_model.ProjectMember) (*proj_model.ProjectMember, error) {
|
|
|
|
if member.UserID == "" {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-ld93d", "userID missing")
|
|
|
|
}
|
|
|
|
project, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: member.ID, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, m := range project.Members {
|
|
|
|
if m.UserID == member.UserID {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Could not find member in list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) AddProjectMember(ctx context.Context, member *proj_model.ProjectMember) (*proj_model.ProjectMember, error) {
|
|
|
|
if !member.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "UserID and Roles are required")
|
|
|
|
}
|
|
|
|
existing, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: member.ID, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.ContainsMember(member) {
|
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "EVENT-idke6", "User is already member of this Project")
|
|
|
|
}
|
|
|
|
repoProject := ProjectFromModel(existing)
|
|
|
|
repoMember := ProjectMemberFromModel(member)
|
|
|
|
|
|
|
|
addAggregate := ProjectMemberAddedAggregate(es.Eventstore.AggregateCreator(), repoProject, repoMember)
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoProject.AppendEvents, addAggregate)
|
|
|
|
for _, m := range repoProject.Members {
|
|
|
|
if m.UserID == member.UserID {
|
|
|
|
return ProjectMemberToModel(m), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Could not find member in list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) ChangeProjectMember(ctx context.Context, member *proj_model.ProjectMember) (*proj_model.ProjectMember, error) {
|
|
|
|
if !member.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "UserID and Roles are required")
|
|
|
|
}
|
|
|
|
existing, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: member.ID, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !existing.ContainsMember(member) {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-oe39f", "User is not member of this project")
|
|
|
|
}
|
|
|
|
repoProject := ProjectFromModel(existing)
|
|
|
|
repoMember := ProjectMemberFromModel(member)
|
|
|
|
|
|
|
|
projectAggregate := ProjectMemberChangedAggregate(es.Eventstore.AggregateCreator(), repoProject, repoMember)
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoProject.AppendEvents, projectAggregate)
|
|
|
|
|
|
|
|
for _, m := range repoProject.Members {
|
|
|
|
if m.UserID == member.UserID {
|
|
|
|
return ProjectMemberToModel(m), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, caos_errs.ThrowInternal(nil, "EVENT-3udjs", "Could not find member in list")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ProjectEventstore) RemoveProjectMember(ctx context.Context, member *proj_model.ProjectMember) error {
|
|
|
|
if member.UserID == "" {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "EVENT-d43fs", "UserID and Roles are required")
|
|
|
|
}
|
|
|
|
existing, err := es.ProjectByID(ctx, &proj_model.Project{ObjectRoot: models.ObjectRoot{ID: member.ID, Sequence: 0}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !existing.ContainsMember(member) {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "EVENT-swf34", "User is not member of this project")
|
|
|
|
}
|
|
|
|
repoProject := ProjectFromModel(existing)
|
|
|
|
repoMember := ProjectMemberFromModel(member)
|
|
|
|
|
|
|
|
projectAggregate := ProjectMemberRemovedAggregate(es.Eventstore.AggregateCreator(), repoProject, repoMember)
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoProject.AppendEvents, projectAggregate)
|
|
|
|
return err
|
|
|
|
}
|