2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
import (
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-01-08 10:33:45 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type OrgMemberWriteModel struct {
|
|
|
|
MemberWriteModel
|
|
|
|
}
|
2021-01-08 10:33:45 +00:00
|
|
|
|
|
|
|
func NewOrgMemberWriteModel(orgID, userID string) *OrgMemberWriteModel {
|
|
|
|
return &OrgMemberWriteModel{
|
|
|
|
MemberWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
2021-01-08 10:33:45 +00:00
|
|
|
},
|
|
|
|
UserID: userID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgMemberWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.MemberAddedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberAddedEvent)
|
|
|
|
case *org.MemberChangedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberChangedEvent)
|
|
|
|
case *org.MemberRemovedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberRemovedEvent)
|
2021-06-07 05:20:47 +00:00
|
|
|
case *org.MemberCascadeRemovedEvent:
|
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberCascadeRemovedEvent)
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgMemberWriteModel) Reduce() error {
|
|
|
|
return wm.MemberWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgMemberWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.MemberWriteModel.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(
|
|
|
|
org.MemberAddedEventType,
|
|
|
|
org.MemberChangedEventType,
|
2021-06-07 05:20:47 +00:00
|
|
|
org.MemberRemovedEventType,
|
|
|
|
org.MemberCascadeRemovedEventType)
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|