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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
org.MemberRemovedEventType)
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|