2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/member"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemberWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
UserID string
|
|
|
|
Roles []string
|
|
|
|
|
|
|
|
State domain.MemberState
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemberWriteModel(userID string) *MemberWriteModel {
|
|
|
|
return &MemberWriteModel{
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MemberWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *member.MemberAddedEvent:
|
|
|
|
wm.UserID = e.UserID
|
|
|
|
wm.Roles = e.Roles
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.State = domain.MemberStateActive
|
2021-01-04 13:52:13 +00:00
|
|
|
case *member.MemberChangedEvent:
|
|
|
|
wm.Roles = e.Roles
|
|
|
|
case *member.MemberRemovedEvent:
|
|
|
|
wm.Roles = nil
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.State = domain.MemberStateRemoved
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|