2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/member"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemberWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2021-01-07 16:06:45 +01:00
|
|
|
UserID string
|
|
|
|
Roles []string
|
|
|
|
|
|
|
|
State domain.MemberState
|
2021-01-04 14:52:13 +01: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 16:06:45 +01:00
|
|
|
wm.State = domain.MemberStateActive
|
2021-01-04 14:52:13 +01:00
|
|
|
case *member.MemberChangedEvent:
|
|
|
|
wm.Roles = e.Roles
|
|
|
|
case *member.MemberRemovedEvent:
|
|
|
|
wm.Roles = nil
|
2021-01-07 16:06:45 +01:00
|
|
|
wm.State = domain.MemberStateRemoved
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|