2020-11-11 16:51:44 +00:00
|
|
|
package member
|
|
|
|
|
|
|
|
import "github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
|
2020-11-18 20:22:15 +00:00
|
|
|
//ReadModel represenets the default member view.
|
|
|
|
// It's computed from events.
|
2020-11-11 16:51:44 +00:00
|
|
|
type ReadModel struct {
|
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
UserID string
|
|
|
|
Roles []string
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:40:56 +00:00
|
|
|
//NewReadModel is the default constructor of ReadModel
|
|
|
|
func NewReadModel(userID string) *ReadModel {
|
2020-11-11 16:51:44 +00:00
|
|
|
return &ReadModel{
|
2020-11-26 08:19:14 +00:00
|
|
|
UserID: userID,
|
2020-11-11 16:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:22:15 +00:00
|
|
|
//Reduce extends eventstore.ReadModel
|
2020-11-11 16:51:44 +00:00
|
|
|
func (rm *ReadModel) Reduce() error {
|
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *AddedEvent:
|
|
|
|
rm.Roles = e.Roles
|
|
|
|
case *ChangedEvent:
|
|
|
|
rm.Roles = e.Roles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|