2021-01-12 12:59:51 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2021-01-12 12:59:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectMemberWriteModel struct {
|
|
|
|
MemberWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectMemberWriteModel(projectID, userID, resourceOwner string) *ProjectMemberWriteModel {
|
|
|
|
return &ProjectMemberWriteModel{
|
|
|
|
MemberWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: projectID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
UserID: userID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *ProjectMemberWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-12 12:59:51 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *project.MemberAddedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberAddedEvent)
|
|
|
|
case *project.MemberChangedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberChangedEvent)
|
|
|
|
case *project.MemberRemovedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberRemovedEvent)
|
2021-06-07 07:20:47 +02:00
|
|
|
case *project.MemberCascadeRemovedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberCascadeRemovedEvent)
|
2021-01-12 12:59:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectMemberWriteModel) Reduce() error {
|
|
|
|
return wm.MemberWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ProjectMemberWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(project.AggregateType).
|
|
|
|
AggregateIDs(wm.MemberWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(project.MemberAddedType,
|
|
|
|
project.MemberChangedType,
|
2021-06-07 07:20:47 +02:00
|
|
|
project.MemberRemovedType,
|
2021-07-06 13:55:57 +02:00
|
|
|
project.MemberCascadeRemovedType).
|
|
|
|
Builder()
|
2021-01-12 12:59:51 +01:00
|
|
|
}
|