2021-01-12 11:59:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-01-28 05:35:26 +00:00
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2021-01-12 11:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-08-24 06:34:10 +00:00
|
|
|
Name string
|
|
|
|
ProjectRoleAssertion bool
|
|
|
|
ProjectRoleCheck bool
|
|
|
|
HasProjectCheck bool
|
|
|
|
PrivateLabelingSetting domain.PrivateLabelingSetting
|
|
|
|
State domain.ProjectState
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectWriteModel(projectID string, resourceOwner string) *ProjectWriteModel {
|
|
|
|
return &ProjectWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.ProjectAddedEvent:
|
|
|
|
wm.Name = e.Name
|
2021-01-28 05:35:26 +00:00
|
|
|
wm.ProjectRoleAssertion = e.ProjectRoleAssertion
|
|
|
|
wm.ProjectRoleCheck = e.ProjectRoleCheck
|
2021-08-18 08:49:04 +00:00
|
|
|
wm.HasProjectCheck = e.HasProjectCheck
|
2021-08-24 06:34:10 +00:00
|
|
|
wm.PrivateLabelingSetting = e.PrivateLabelingSetting
|
2021-01-12 11:59:51 +00:00
|
|
|
wm.State = domain.ProjectStateActive
|
2021-01-28 05:35:26 +00:00
|
|
|
case *project.ProjectChangeEvent:
|
|
|
|
if e.Name != nil {
|
|
|
|
wm.Name = *e.Name
|
|
|
|
}
|
|
|
|
if e.ProjectRoleAssertion != nil {
|
|
|
|
wm.ProjectRoleAssertion = *e.ProjectRoleAssertion
|
|
|
|
}
|
|
|
|
if e.ProjectRoleCheck != nil {
|
|
|
|
wm.ProjectRoleCheck = *e.ProjectRoleCheck
|
|
|
|
}
|
2021-08-18 08:49:04 +00:00
|
|
|
if e.HasProjectCheck != nil {
|
|
|
|
wm.HasProjectCheck = *e.HasProjectCheck
|
|
|
|
}
|
2021-08-24 06:34:10 +00:00
|
|
|
if e.PrivateLabelingSetting != nil {
|
|
|
|
wm.PrivateLabelingSetting = *e.PrivateLabelingSetting
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
case *project.ProjectDeactivatedEvent:
|
|
|
|
if wm.State == domain.ProjectStateRemoved {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.ProjectStateInactive
|
|
|
|
case *project.ProjectReactivatedEvent:
|
|
|
|
if wm.State == domain.ProjectStateRemoved {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.ProjectStateActive
|
|
|
|
case *project.ProjectRemovedEvent:
|
|
|
|
wm.State = domain.ProjectStateRemoved
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
return wm.WriteModel.Reduce()
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(project.ProjectAddedType,
|
|
|
|
project.ProjectChangedType,
|
|
|
|
project.ProjectDeactivatedType,
|
|
|
|
project.ProjectReactivatedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
project.ProjectRemovedType).
|
|
|
|
Builder()
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
func (wm *ProjectWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
name string,
|
|
|
|
projectRoleAssertion,
|
2021-08-18 08:49:04 +00:00
|
|
|
projectRoleCheck,
|
|
|
|
hasProjectCheck bool,
|
2021-08-24 06:34:10 +00:00
|
|
|
privateLabelingSetting domain.PrivateLabelingSetting,
|
2021-01-28 05:35:26 +00:00
|
|
|
) (*project.ProjectChangeEvent, bool, error) {
|
|
|
|
changes := make([]project.ProjectChanges, 0)
|
|
|
|
var err error
|
|
|
|
|
2021-02-15 12:31:24 +00:00
|
|
|
oldName := ""
|
2021-01-28 05:35:26 +00:00
|
|
|
if wm.Name != name {
|
2021-02-15 12:31:24 +00:00
|
|
|
oldName = wm.Name
|
2021-01-28 05:35:26 +00:00
|
|
|
changes = append(changes, project.ChangeName(name))
|
|
|
|
}
|
|
|
|
if wm.ProjectRoleAssertion != projectRoleAssertion {
|
|
|
|
changes = append(changes, project.ChangeProjectRoleAssertion(projectRoleAssertion))
|
|
|
|
}
|
|
|
|
if wm.ProjectRoleCheck != projectRoleCheck {
|
|
|
|
changes = append(changes, project.ChangeProjectRoleCheck(projectRoleCheck))
|
|
|
|
}
|
2021-08-18 08:49:04 +00:00
|
|
|
if wm.HasProjectCheck != hasProjectCheck {
|
|
|
|
changes = append(changes, project.ChangeHasProjectCheck(hasProjectCheck))
|
|
|
|
}
|
2021-08-24 06:34:10 +00:00
|
|
|
if wm.PrivateLabelingSetting != privateLabelingSetting {
|
|
|
|
changes = append(changes, project.ChangePrivateLabelingSetting(privateLabelingSetting))
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
changeEvent, err := project.NewProjectChangeEvent(ctx, aggregate, oldName, changes)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func ProjectAggregateFromWriteModel(wm *eventstore.WriteModel) *eventstore.Aggregate {
|
|
|
|
return eventstore.AggregateFromWriteModel(wm, project.AggregateType, project.AggregateVersion)
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|