mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
bd33b54ac5
* 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 * 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 error to caos err Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
208 lines
5.3 KiB
Go
208 lines
5.3 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/logging"
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
)
|
|
|
|
const (
|
|
projectVersion = "v1"
|
|
)
|
|
|
|
type Project struct {
|
|
es_models.ObjectRoot
|
|
Name string `json:"name,omitempty"`
|
|
State int32 `json:"-"`
|
|
Members []*ProjectMember `json:"-"`
|
|
}
|
|
|
|
type ProjectMember struct {
|
|
es_models.ObjectRoot
|
|
UserID string `json:"userId,omitempty"`
|
|
Roles []string `json:"roles,omitempty"`
|
|
}
|
|
|
|
func (p *Project) Changes(changed *Project) map[string]interface{} {
|
|
changes := make(map[string]interface{}, 1)
|
|
if changed.Name != "" && p.Name != changed.Name {
|
|
changes["name"] = changed.Name
|
|
}
|
|
return changes
|
|
}
|
|
|
|
func ProjectFromModel(project *model.Project) *Project {
|
|
members := ProjectMembersFromModel(project.Members)
|
|
return &Project{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
ID: project.ObjectRoot.ID,
|
|
Sequence: project.Sequence,
|
|
ChangeDate: project.ChangeDate,
|
|
CreationDate: project.CreationDate,
|
|
},
|
|
Name: project.Name,
|
|
State: model.ProjectStateToInt(project.State),
|
|
Members: members,
|
|
}
|
|
}
|
|
|
|
func ProjectToModel(project *Project) *model.Project {
|
|
members := ProjectMembersToModel(project.Members)
|
|
return &model.Project{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
ID: project.ID,
|
|
ChangeDate: project.ChangeDate,
|
|
CreationDate: project.CreationDate,
|
|
Sequence: project.Sequence,
|
|
},
|
|
Name: project.Name,
|
|
State: model.ProjectStateFromInt(project.State),
|
|
Members: members,
|
|
}
|
|
}
|
|
|
|
func ProjectMembersToModel(members []*ProjectMember) []*model.ProjectMember {
|
|
convertedMembers := make([]*model.ProjectMember, len(members))
|
|
for i, m := range members {
|
|
convertedMembers[i] = ProjectMemberToModel(m)
|
|
}
|
|
return convertedMembers
|
|
}
|
|
|
|
func ProjectMembersFromModel(members []*model.ProjectMember) []*ProjectMember {
|
|
convertedMembers := make([]*ProjectMember, len(members))
|
|
for i, m := range members {
|
|
convertedMembers[i] = ProjectMemberFromModel(m)
|
|
}
|
|
return convertedMembers
|
|
}
|
|
|
|
func ProjectMemberFromModel(member *model.ProjectMember) *ProjectMember {
|
|
return &ProjectMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
ID: member.ObjectRoot.ID,
|
|
Sequence: member.Sequence,
|
|
ChangeDate: member.ChangeDate,
|
|
CreationDate: member.CreationDate,
|
|
},
|
|
UserID: member.UserID,
|
|
Roles: member.Roles,
|
|
}
|
|
}
|
|
|
|
func ProjectMemberToModel(member *ProjectMember) *model.ProjectMember {
|
|
return &model.ProjectMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
ID: member.ID,
|
|
ChangeDate: member.ChangeDate,
|
|
CreationDate: member.CreationDate,
|
|
Sequence: member.Sequence,
|
|
},
|
|
UserID: member.UserID,
|
|
Roles: member.Roles,
|
|
}
|
|
}
|
|
|
|
func ProjectFromEvents(project *Project, events ...*es_models.Event) (*Project, error) {
|
|
if project == nil {
|
|
project = &Project{}
|
|
}
|
|
|
|
return project, project.AppendEvents(events...)
|
|
}
|
|
|
|
func (p *Project) AppendEvents(events ...*es_models.Event) error {
|
|
for _, event := range events {
|
|
if err := p.AppendEvent(event); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Project) AppendEvent(event *es_models.Event) error {
|
|
p.ObjectRoot.AppendEvent(event)
|
|
|
|
switch event.Type {
|
|
case model.ProjectAdded, model.ProjectChanged:
|
|
if err := json.Unmarshal(event.Data, p); err != nil {
|
|
logging.Log("EVEN-idl93").WithError(err).Error("could not unmarshal event data")
|
|
return err
|
|
}
|
|
p.State = model.ProjectStateToInt(model.Active)
|
|
return nil
|
|
case model.ProjectDeactivated:
|
|
return p.appendDeactivatedEvent()
|
|
case model.ProjectReactivated:
|
|
return p.appendReactivatedEvent()
|
|
case model.ProjectMemberAdded:
|
|
return p.appendAddMemberEvent(event)
|
|
case model.ProjectMemberChanged:
|
|
return p.appendChangeMemberEvent(event)
|
|
case model.ProjectMemberRemoved:
|
|
return p.appendRemoveMemberEvent(event)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Project) appendDeactivatedEvent() error {
|
|
p.State = model.ProjectStateToInt(model.Inactive)
|
|
return nil
|
|
}
|
|
|
|
func (p *Project) appendReactivatedEvent() error {
|
|
p.State = model.ProjectStateToInt(model.Active)
|
|
return nil
|
|
}
|
|
|
|
func (p *Project) appendAddMemberEvent(event *es_models.Event) error {
|
|
member, err := getMemberData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
member.ObjectRoot.CreationDate = event.CreationDate
|
|
p.Members = append(p.Members, member)
|
|
return nil
|
|
}
|
|
|
|
func (p *Project) appendChangeMemberEvent(event *es_models.Event) error {
|
|
member, err := getMemberData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i, m := range p.Members {
|
|
if m.UserID == member.UserID {
|
|
p.Members[i] = member
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Project) appendRemoveMemberEvent(event *es_models.Event) error {
|
|
member, err := getMemberData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i, m := range p.Members {
|
|
if m.UserID == member.UserID {
|
|
p.Members[i] = p.Members[len(p.Members)-1]
|
|
p.Members[len(p.Members)-1] = nil
|
|
p.Members = p.Members[:len(p.Members)-1]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getMemberData(event *es_models.Event) (*ProjectMember, error) {
|
|
member := &ProjectMember{}
|
|
member.ObjectRoot.AppendEvent(event)
|
|
if err := json.Unmarshal(event.Data, member); err != nil {
|
|
logging.Log("EVEN-e4dkp").WithError(err).Error("could not unmarshal event data")
|
|
return nil, errors.ThrowInternal(err, "EVENT-83js6", "could not unmarshal event data")
|
|
}
|
|
return member, nil
|
|
}
|