zitadel/internal/v2/repository/member/write_model.go

62 lines
1.3 KiB
Go
Raw Normal View History

2020-11-20 16:03:17 +00: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 10:36:58 +00:00
UserID string
Roles []string
IsRemoved bool
userID string
aggregateType eventstore.AggregateType
aggregateID string
}
func PrepareWriteModel(
userID string,
aggregateType eventstore.AggregateType,
aggregateID string,
) *WriteModel {
return &WriteModel{
WriteModel: *eventstore.NewWriteModel(),
userID: userID,
aggregateType: aggregateType,
aggregateID: aggregateID,
}
2020-11-20 16:03:17 +00:00
}
//Reduce extends eventstore.ReadModel
2020-11-23 10:36:58 +00:00
func (wm *WriteModel) Reduce() error {
for _, event := range wm.Events {
2020-11-20 16:03:17 +00:00
switch e := event.(type) {
case *AddedEvent:
2020-11-23 10:36:58 +00:00
if e.UserID != wm.userID {
continue
}
wm.UserID = e.UserID
wm.Roles = e.Roles
2020-11-20 16:03:17 +00:00
case *ChangedEvent:
2020-11-23 10:36:58 +00:00
if e.UserID != wm.userID {
continue
}
wm.UserID = e.UserID
wm.Roles = e.Roles
case *RemovedEvent:
wm.Roles = nil
wm.IsRemoved = true
2020-11-20 16:03:17 +00:00
}
}
2020-11-23 10:36:58 +00:00
return wm.WriteModel.Reduce()
}
func (wm *WriteModel) Query() *eventstore.SearchQueryFactory {
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, wm.aggregateType).
AggregateIDs(wm.aggregateID)
2020-11-20 16:03:17 +00:00
}