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