zitadel/internal/command/user_v3_username_model.go

55 lines
1.3 KiB
Go
Raw Normal View History

package command
import (
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/user/authenticator"
)
type UsernameV3WriteModel struct {
eventstore.WriteModel
UserID string
Username string
IsOrgSpecific bool
}
2024-09-24 20:42:20 +02:00
func NewUsernameV3WriteModel(resourceOwner, userID, id string) *UsernameV3WriteModel {
return &UsernameV3WriteModel{
WriteModel: eventstore.WriteModel{
2024-09-24 20:42:20 +02:00
AggregateID: id,
ResourceOwner: resourceOwner,
},
2024-09-24 20:42:20 +02:00
UserID: userID,
}
}
func (wm *UsernameV3WriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *authenticator.UsernameCreatedEvent:
2024-09-24 20:42:20 +02:00
if e.UserID != wm.UserID {
continue
}
wm.UserID = e.UserID
wm.Username = e.Username
wm.IsOrgSpecific = e.IsOrgSpecific
case *authenticator.UsernameDeletedEvent:
wm.UserID = ""
wm.Username = ""
wm.IsOrgSpecific = false
}
}
return wm.WriteModel.Reduce()
}
func (wm *UsernameV3WriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
ResourceOwner(wm.ResourceOwner).
AddQuery().
2024-09-24 20:42:20 +02:00
AggregateTypes(authenticator.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(
authenticator.UsernameCreatedType,
authenticator.UsernameDeletedType,
).Builder()
}