2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-05 05:58:09 +00:00
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type InstanceMemberWriteModel struct {
|
2021-01-04 13:52:13 +00:00
|
|
|
MemberWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceMemberWriteModel(ctx context.Context, userID string) *InstanceMemberWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceMemberWriteModel{
|
2021-01-04 13:52:13 +00:00
|
|
|
MemberWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
UserID: userID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceMemberWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 13:52:13 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.MemberAddedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberAddedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.MemberChangedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberChangedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.MemberRemovedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberRemovedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.MemberCascadeRemovedEvent:
|
2021-06-07 05:20:47 +00:00
|
|
|
if e.UserID != wm.MemberWriteModel.UserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.MemberWriteModel.AppendEvents(&e.MemberCascadeRemovedEvent)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceMemberWriteModel) Reduce() error {
|
2021-01-04 13:52:13 +00:00
|
|
|
return wm.MemberWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceMemberWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-07-06 11:55:57 +00:00
|
|
|
AggregateIDs(wm.MemberWriteModel.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.MemberAddedEventType,
|
|
|
|
instance.MemberChangedEventType,
|
|
|
|
instance.MemberRemovedEventType,
|
|
|
|
instance.MemberCascadeRemovedEventType).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|