mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
802bb56ea1
* feat: eventstore repository * fix: remove gorm * version * feat: pkg * feat: add some files for project * feat: eventstore without eventstore-lib * rename files * gnueg * fix: key json * fix: add object * fix: change imports * fix: internal models * fix: some imports * fix: global model * fix: add some functions on repo * feat(eventstore): sdk * fix(eventstore): search query * fix(eventstore): rename app to eventstore * delete empty test * remove unused func * merge master * fix(eventstore): tests * fix(models): delete unused struct * fix: some funcitons * feat(eventstore): implemented push events * fix: move project eventstore to project package * fix: change project eventstore funcs * feat(eventstore): overwrite context data * fix: change project eventstore * fix: add project repo to mgmt server * feat(types): SQL-config * fix: commented code * feat(eventstore): options to overwrite editor * feat: auth interceptor and cockroach migrations * fix: migrations * fix: fix filter * fix: not found on getbyid * fix: add sequence * fix: add some tests * fix(eventstore): nullable sequence * fix: add some tests * merge * fix: add some tests * fix(migrations): correct statements for sequence * fix: add some tests * fix: add some tests * fix: changes from mr * Update internal/eventstore/models/field.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix(eventstore): code quality * fix: add types to aggregate/Event-types * fix(eventstore): rename modifier* to editor* * fix(eventstore): delete editor_org * fix(migrations): remove editor_org field, rename modifier_* to editor_* * fix: generate files * fix(eventstore): tests * fix(eventstore): rename modifier to editor * fix(migrations): add cluster migration, fix(migrations): fix typo of host in clean clsuter * fix(eventstore): move health * fix(eventstore): AggregateTypeFilter aggregateType as param * code quality * feat: add member funcs * feat: add member model * feat: add member events * feat: add member repo model * fix: project member funcs * fix: add tests * fix: add tests * feat: implement member requests * fix: merge master * fix: read existing in project repo * fix: fix tests * feat: add internal cache * feat: add cache mock * fix: return values of cache mock * fix: add cache config * fix: use eventstore sdk * Update internal/project/model/project_member.go Co-Authored-By: Silvan <silvan.reusser@gmail.com> * fix: use get project func * fix: return err not nil * fix: change err types * Update internal/cache/bigcache/cache.go Co-Authored-By: livio-a <livio.a@gmail.com> * Update internal/cache/config/config.go Co-Authored-By: livio-a <livio.a@gmail.com> * Update internal/cache/config/config.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix: config * fix: config * resolve conversations * fix: mr changes * fix: fix decode of bigcache * feat: test caches * fix: remove unnecessary code Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
208 lines
7.0 KiB
Go
208 lines
7.0 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
"github.com/caos/zitadel/internal/cache/config"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
)
|
|
|
|
type ProjectEventstore struct {
|
|
es_int.Eventstore
|
|
projectCache *ProjectCache
|
|
}
|
|
|
|
type ProjectConfig struct {
|
|
es_int.Eventstore
|
|
Cache *config.CacheConfig
|
|
}
|
|
|
|
func StartProject(conf ProjectConfig) (*ProjectEventstore, error) {
|
|
projectCache, err := StartCache(conf.Cache)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ProjectEventstore{
|
|
Eventstore: conf.Eventstore,
|
|
projectCache: projectCache,
|
|
}, nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) ProjectByID(ctx context.Context, id string) (*proj_model.Project, error) {
|
|
project := es.projectCache.getProject(id)
|
|
|
|
query, err := ProjectByIDQuery(project.ID, project.Sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, project.AppendEvents, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
es.projectCache.cacheProject(project)
|
|
return ProjectToModel(project), nil
|
|
}
|
|
|
|
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)
|
|
|
|
createAggregate := ProjectCreateAggregate(es.AggregateCreator(), repoProject)
|
|
err := es_sdk.Push(ctx, es.PushAggregates, repoProject.AppendEvents, createAggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
es.projectCache.cacheProject(repoProject)
|
|
return ProjectToModel(repoProject), nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) UpdateProject(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")
|
|
}
|
|
existingProject, err := es.ProjectByID(ctx, project.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repoExisting := ProjectFromModel(existingProject)
|
|
repoNew := ProjectFromModel(project)
|
|
|
|
updateAggregate := ProjectUpdateAggregate(es.AggregateCreator(), repoExisting, repoNew)
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, updateAggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
es.projectCache.cacheProject(repoExisting)
|
|
return ProjectToModel(repoExisting), nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) DeactivateProject(ctx context.Context, id string) (*proj_model.Project, error) {
|
|
existing, err := es.ProjectByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !existing.IsActive() {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be active")
|
|
}
|
|
|
|
repoExisting := ProjectFromModel(existing)
|
|
aggregate := ProjectDeactivateAggregate(es.AggregateCreator(), repoExisting)
|
|
es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, aggregate)
|
|
|
|
es.projectCache.cacheProject(repoExisting)
|
|
return ProjectToModel(repoExisting), nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) ReactivateProject(ctx context.Context, id string) (*proj_model.Project, error) {
|
|
existing, err := es.ProjectByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existing.IsActive() {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be inactive")
|
|
}
|
|
|
|
repoExisting := ProjectFromModel(existing)
|
|
aggregate := ProjectReactivateAggregate(es.AggregateCreator(), repoExisting)
|
|
es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, aggregate)
|
|
|
|
es.projectCache.cacheProject(repoExisting)
|
|
return ProjectToModel(repoExisting), nil
|
|
}
|
|
|
|
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, member.ID)
|
|
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, member.ID)
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
es.projectCache.cacheProject(repoProject)
|
|
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, member.ID)
|
|
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)
|
|
es.projectCache.cacheProject(repoProject)
|
|
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, member.ID)
|
|
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)
|
|
es.projectCache.cacheProject(repoProject)
|
|
return err
|
|
}
|