2020-11-20 17:03:17 +01:00
|
|
|
package member
|
|
|
|
|
|
|
|
import "github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
|
|
|
|
//WriteModel is used to create events
|
|
|
|
// It has no computed fields and represents the data
|
|
|
|
// which can be changed
|
|
|
|
type WriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2020-11-23 11:36:58 +01:00
|
|
|
UserID string
|
|
|
|
Roles []string
|
|
|
|
IsRemoved bool
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:40:56 +01:00
|
|
|
func NewWriteModel(userID string) *WriteModel {
|
2020-11-23 11:36:58 +01:00
|
|
|
return &WriteModel{
|
2020-11-30 06:40:56 +01:00
|
|
|
UserID: userID,
|
2020-11-23 11:36:58 +01:00
|
|
|
}
|
2020-11-20 17:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Reduce extends eventstore.ReadModel
|
2020-11-23 11:36:58 +01:00
|
|
|
func (wm *WriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
2020-11-20 17:03:17 +01:00
|
|
|
switch e := event.(type) {
|
|
|
|
case *AddedEvent:
|
2020-11-23 11:36:58 +01:00
|
|
|
wm.UserID = e.UserID
|
|
|
|
wm.Roles = e.Roles
|
2020-11-20 17:03:17 +01:00
|
|
|
case *ChangedEvent:
|
2020-11-23 11:36:58 +01:00
|
|
|
wm.Roles = e.Roles
|
|
|
|
case *RemovedEvent:
|
|
|
|
wm.Roles = nil
|
|
|
|
wm.IsRemoved = true
|
2020-11-20 17:03:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 11:36:58 +01:00
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:40:56 +01:00
|
|
|
// func (wm *WriteModel) Query() *eventstore.SearchQueryFactory {
|
|
|
|
// return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, wm.aggregateType).
|
|
|
|
// AggregateIDs(wm.aggregateID)
|
|
|
|
// }
|