2021-01-28 06:35:26 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2021-01-28 06:35:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectGrantMemberWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
GrantID string
|
|
|
|
UserID string
|
|
|
|
Roles []string
|
|
|
|
|
|
|
|
State domain.MemberState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGrantMemberWriteModel(projectID, userID, grantID string) *ProjectGrantMemberWriteModel {
|
|
|
|
return &ProjectGrantMemberWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
},
|
|
|
|
UserID: userID,
|
|
|
|
GrantID: grantID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *ProjectGrantMemberWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-28 06:35:26 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.GrantMemberAddedEvent:
|
|
|
|
if e.UserID != wm.UserID || e.GrantID != wm.GrantID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.GrantMemberChangedEvent:
|
|
|
|
if e.UserID != wm.UserID || e.GrantID != wm.GrantID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *project.GrantMemberRemovedEvent:
|
|
|
|
if e.UserID != wm.UserID || e.GrantID != wm.GrantID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-06-07 07:20:47 +02:00
|
|
|
case *project.GrantMemberCascadeRemovedEvent:
|
|
|
|
if e.UserID != wm.UserID || e.GrantID != wm.GrantID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-01-28 06:35:26 +01:00
|
|
|
case *project.GrantRemovedEvent:
|
|
|
|
if e.GrantID != wm.GrantID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-02-18 14:48:27 +01:00
|
|
|
case *project.ProjectRemovedEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectGrantMemberWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.GrantMemberAddedEvent:
|
|
|
|
wm.Roles = e.Roles
|
|
|
|
wm.State = domain.MemberStateActive
|
|
|
|
case *project.GrantMemberChangedEvent:
|
|
|
|
wm.Roles = e.Roles
|
|
|
|
case *project.GrantMemberRemovedEvent:
|
|
|
|
wm.State = domain.MemberStateRemoved
|
2021-06-07 07:20:47 +02:00
|
|
|
case *project.GrantMemberCascadeRemovedEvent:
|
|
|
|
wm.State = domain.MemberStateRemoved
|
2021-01-28 06:35:26 +01:00
|
|
|
case *project.GrantRemovedEvent, *project.ProjectRemovedEvent:
|
|
|
|
wm.State = domain.MemberStateRemoved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectGrantMemberWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
2021-02-18 14:48:27 +01:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
|
|
|
project.GrantMemberAddedType,
|
|
|
|
project.GrantMemberChangedType,
|
|
|
|
project.GrantMemberRemovedType,
|
2021-06-07 07:20:47 +02:00
|
|
|
project.GrantMemberCascadeRemovedType,
|
2021-02-18 14:48:27 +01:00
|
|
|
project.GrantRemovedType,
|
2021-07-06 13:55:57 +02:00
|
|
|
project.ProjectRemovedType).
|
|
|
|
Builder()
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|