2021-08-09 08:01:20 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/metadata"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-08-09 08:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserMetadataWriteModel struct {
|
|
|
|
MetadataWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMetadataWriteModel(userID, resourceOwner, key string) *UserMetadataWriteModel {
|
|
|
|
return &UserMetadataWriteModel{
|
|
|
|
MetadataWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *UserMetadataWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-08-09 08:01:20 +02:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MetadataSetEvent:
|
|
|
|
wm.MetadataWriteModel.AppendEvents(&e.SetEvent)
|
|
|
|
case *user.MetadataRemovedEvent:
|
|
|
|
wm.MetadataWriteModel.AppendEvents(&e.RemovedEvent)
|
2021-08-12 16:10:01 +02:00
|
|
|
case *user.MetadataRemovedAllEvent:
|
|
|
|
wm.MetadataWriteModel.AppendEvents(&e.RemovedAllEvent)
|
2021-08-09 08:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserMetadataWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateIDs(wm.MetadataWriteModel.AggregateID).
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
EventTypes(
|
|
|
|
user.MetadataSetType,
|
2021-08-12 16:10:01 +02:00
|
|
|
user.MetadataRemovedType,
|
|
|
|
user.MetadataRemovedAllType).
|
2021-08-09 08:01:20 +02:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserMetadataListWriteModel struct {
|
|
|
|
MetadataListWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMetadataListWriteModel(userID, resourceOwner string) *UserMetadataListWriteModel {
|
|
|
|
return &UserMetadataListWriteModel{
|
|
|
|
MetadataListWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
metadataList: make(map[string][]byte),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *UserMetadataListWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-08-09 08:01:20 +02:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MetadataSetEvent:
|
|
|
|
wm.MetadataListWriteModel.AppendEvents(&e.SetEvent)
|
|
|
|
case *user.MetadataRemovedEvent:
|
|
|
|
wm.MetadataListWriteModel.AppendEvents(&e.RemovedEvent)
|
2021-08-12 16:10:01 +02:00
|
|
|
case *user.MetadataRemovedAllEvent:
|
|
|
|
wm.MetadataListWriteModel.AppendEvents(&e.RemovedAllEvent)
|
2021-08-09 08:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserMetadataListWriteModel) Reduce() error {
|
|
|
|
return wm.MetadataListWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserMetadataListWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateIDs(wm.MetadataListWriteModel.AggregateID).
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
EventTypes(
|
|
|
|
user.MetadataSetType,
|
2021-08-12 16:10:01 +02:00
|
|
|
user.MetadataRemovedType,
|
|
|
|
user.MetadataRemovedAllType).
|
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserMetadataByOrgListWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
2021-09-14 17:15:42 +02:00
|
|
|
UserMetadata map[string]map[string][]byte
|
2021-08-12 16:10:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMetadataByOrgListWriteModel(resourceOwner string) *UserMetadataByOrgListWriteModel {
|
|
|
|
return &UserMetadataByOrgListWriteModel{
|
2021-09-14 17:15:42 +02:00
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
UserMetadata: make(map[string]map[string][]byte),
|
2021-08-12 16:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *UserMetadataByOrgListWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-08-12 16:10:01 +02:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MetadataSetEvent:
|
|
|
|
wm.WriteModel.AppendEvents(&e.SetEvent)
|
|
|
|
case *user.MetadataRemovedEvent:
|
|
|
|
wm.WriteModel.AppendEvents(&e.RemovedEvent)
|
|
|
|
case *user.MetadataRemovedAllEvent:
|
|
|
|
wm.WriteModel.AppendEvents(&e.RemovedAllEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserMetadataByOrgListWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *metadata.SetEvent:
|
|
|
|
if val, ok := wm.UserMetadata[e.Aggregate().ID]; ok {
|
|
|
|
val[e.Key] = e.Value
|
|
|
|
} else {
|
|
|
|
wm.UserMetadata[e.Aggregate().ID] = map[string][]byte{
|
|
|
|
e.Key: e.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *metadata.RemovedEvent:
|
|
|
|
if val, ok := wm.UserMetadata[e.Aggregate().ID]; ok {
|
|
|
|
delete(val, e.Key)
|
|
|
|
}
|
|
|
|
case *metadata.RemovedAllEvent:
|
2022-11-30 17:01:17 +01:00
|
|
|
delete(wm.UserMetadata, e.Aggregate().ID)
|
2021-08-12 16:10:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *UserMetadataByOrgListWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
EventTypes(
|
|
|
|
user.MetadataSetType,
|
|
|
|
user.MetadataRemovedType,
|
|
|
|
user.MetadataRemovedAllType).
|
2021-08-09 08:01:20 +02:00
|
|
|
Builder()
|
|
|
|
}
|