zitadel/internal/v2/repository/member/write_model.go
adlerhurst 0780c23f76 member
2020-11-30 07:56:38 +01:00

44 lines
954 B
Go

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
UserID string
Roles []string
IsActive bool
}
func NewWriteModel(userID string) *WriteModel {
return &WriteModel{
UserID: userID,
}
}
//Reduce extends eventstore.ReadModel
func (wm *WriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *AddedEvent:
wm.UserID = e.UserID
wm.Roles = e.Roles
wm.IsActive = true
case *ChangedEvent:
wm.Roles = e.Roles
case *RemovedEvent:
wm.Roles = nil
wm.IsActive = false
}
}
return wm.WriteModel.Reduce()
}
// func (wm *WriteModel) Query() *eventstore.SearchQueryFactory {
// return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, wm.aggregateType).
// AggregateIDs(wm.aggregateID)
// }