mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
38 lines
739 B
Go
38 lines
739 B
Go
|
package command
|
||
|
|
||
|
import (
|
||
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
||
|
"github.com/caos/zitadel/internal/v2/repository/member"
|
||
|
)
|
||
|
|
||
|
type MemberWriteModel struct {
|
||
|
eventstore.WriteModel
|
||
|
|
||
|
UserID string
|
||
|
Roles []string
|
||
|
IsActive bool
|
||
|
}
|
||
|
|
||
|
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
|
||
|
wm.IsActive = true
|
||
|
case *member.MemberChangedEvent:
|
||
|
wm.Roles = e.Roles
|
||
|
case *member.MemberRemovedEvent:
|
||
|
wm.Roles = nil
|
||
|
wm.IsActive = false
|
||
|
}
|
||
|
}
|
||
|
return wm.WriteModel.Reduce()
|
||
|
}
|