mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
c07ed83c41
* 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: start implementing project members * feat: remove member funcs * feat: remove member model * feat: remove member events * feat: remove member repo model * fix: better error func testing * Update docs/local.md Co-Authored-By: Silvan <silvan.reusser@gmail.com> * Update docs/local.md Co-Authored-By: Silvan <silvan.reusser@gmail.com> * fix: mr requests * fix: md file Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
111 lines
3.5 KiB
Go
111 lines
3.5 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
|
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) {
|
|
filter, err := ProjectByIDQuery(project.ID, project.Sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
events, err := es.Eventstore.FilterEvents(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(events) == 0 {
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-8due3", "Could not find project events")
|
|
}
|
|
foundProject, err := ProjectFromEvents(nil, events...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ProjectToModel(foundProject), 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)
|
|
projectAggregate, err := ProjectCreateAggregate(ctx, es.Eventstore.AggregateCreator(), repoProject)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = es.PushAggregates(ctx, projectAggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
repoProject.AppendEvents(projectAggregate.Events...)
|
|
return ProjectToModel(repoProject), nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) UpdateProject(ctx context.Context, existing *proj_model.Project, new *proj_model.Project) (*proj_model.Project, error) {
|
|
if !new.IsValid() {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Name is required")
|
|
}
|
|
repoExisting := ProjectFromModel(existing)
|
|
repoNew := ProjectFromModel(new)
|
|
projectAggregate, err := ProjectUpdateAggregate(ctx, es.AggregateCreator(), repoExisting, repoNew)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = es.PushAggregates(ctx, projectAggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repoExisting.AppendEvents(projectAggregate.Events...)
|
|
return ProjectToModel(repoExisting), nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) DeactivateProject(ctx context.Context, existing *proj_model.Project) (*proj_model.Project, error) {
|
|
if !existing.IsActive() {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be active")
|
|
}
|
|
repoExisting := ProjectFromModel(existing)
|
|
projectAggregate, err := ProjectDeactivateAggregate(ctx, es.AggregateCreator(), repoExisting)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = es.PushAggregates(ctx, projectAggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repoExisting.AppendEvents(projectAggregate.Events...)
|
|
return ProjectToModel(repoExisting), nil
|
|
}
|
|
|
|
func (es *ProjectEventstore) ReactivateProject(ctx context.Context, existing *proj_model.Project) (*proj_model.Project, error) {
|
|
if existing.IsActive() {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-die45", "project must be inactive")
|
|
}
|
|
repoExisting := ProjectFromModel(existing)
|
|
projectAggregate, err := ProjectReactivateAggregate(ctx, es.AggregateCreator(), repoExisting)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = es.PushAggregates(ctx, projectAggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
repoExisting.AppendEvents(projectAggregate.Events...)
|
|
return ProjectToModel(repoExisting), nil
|
|
}
|