2020-04-07 11:23:04 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-20 13:16:33 +00:00
|
|
|
"github.com/caos/zitadel/internal/cache/config"
|
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
|
2020-04-20 13:16:33 +00:00
|
|
|
projectCache *ProjectCache
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectConfig struct {
|
|
|
|
es_int.Eventstore
|
2020-04-20 13:16:33 +00:00
|
|
|
Cache *config.CacheConfig
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StartProject(conf ProjectConfig) (*ProjectEventstore, error) {
|
2020-04-20 13:16:33 +00:00
|
|
|
projectCache, err := StartCache(conf.Cache)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ProjectEventstore{
|
|
|
|
Eventstore: conf.Eventstore,
|
|
|
|
projectCache: projectCache,
|
|
|
|
}, nil
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:16:33 +00:00
|
|
|
func (es *ProjectEventstore) ProjectByID(ctx context.Context, id string) (*proj_model.Project, error) {
|
|
|
|
project := es.projectCache.getProject(id)
|
|
|
|
|
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-20 13:16:33 +00:00
|
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, project.AppendEvents, query)
|
2020-04-07 11:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-20 13:16:33 +00:00
|
|
|
es.projectCache.cacheProject(project)
|
|
|
|
return ProjectToModel(project), 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
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:16:33 +00:00
|
|
|
es.projectCache.cacheProject(repoProject)
|
2020-04-07 11:23:04 +00:00
|
|
|
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-20 13:16:33 +00:00
|
|
|
existingProject, err := es.ProjectByID(ctx, project.ID)
|
2020-04-15 15:11:42 +00:00
|
|
|
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-20 13:16:33 +00:00
|
|
|
es.projectCache.cacheProject(repoExisting)
|
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) {
|
2020-04-20 13:16:33 +00:00
|
|
|
existing, err := es.ProjectByID(ctx, id)
|
2020-04-15 15:11:42 +00:00
|
|
|
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-20 13:16:33 +00:00
|
|
|
|
|
|
|
es.projectCache.cacheProject(repoExisting)
|
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) {
|
2020-04-20 13:16:33 +00:00
|
|
|
existing, err := es.ProjectByID(ctx, id)
|
2020-04-15 15:11:42 +00:00
|
|
|
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-20 13:16:33 +00:00
|
|
|
|
|
|
|
es.projectCache.cacheProject(repoExisting)
|
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")
|
|
|
|
}
|
2020-04-20 13:16:33 +00:00
|
|
|
project, err := es.ProjectByID(ctx, member.ID)
|
2020-04-15 15:11:42 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-04-20 13:16:33 +00:00
|
|
|
existing, err := es.ProjectByID(ctx, member.ID)
|
2020-04-15 15:11:42 +00:00
|
|
|
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)
|
2020-04-20 13:16:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
es.projectCache.cacheProject(repoProject)
|
2020-04-15 15:11:42 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-04-20 13:16:33 +00:00
|
|
|
existing, err := es.ProjectByID(ctx, member.ID)
|
2020-04-15 15:11:42 +00:00
|
|
|
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)
|
2020-04-20 13:16:33 +00:00
|
|
|
es.projectCache.cacheProject(repoProject)
|
2020-04-15 15:11:42 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-04-20 13:16:33 +00:00
|
|
|
existing, err := es.ProjectByID(ctx, member.ID)
|
2020-04-15 15:11:42 +00:00
|
|
|
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)
|
2020-04-20 13:16:33 +00:00
|
|
|
es.projectCache.cacheProject(repoProject)
|
2020-04-15 15:11:42 +00:00
|
|
|
return err
|
|
|
|
}
|