2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/user"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HumanProfileWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
FirstName string
|
|
|
|
LastName string
|
|
|
|
NickName string
|
|
|
|
DisplayName string
|
|
|
|
PreferredLanguage language.Tag
|
|
|
|
Gender domain.Gender
|
|
|
|
|
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanProfileWriteModel(userID string) *HumanProfileWriteModel {
|
|
|
|
return &HumanProfileWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: userID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanProfileWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanProfileChangedEvent:
|
|
|
|
wm.AppendEvents(e)
|
|
|
|
case *user.HumanAddedEvent, *user.HumanRegisteredEvent:
|
|
|
|
wm.AppendEvents(e)
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanProfileWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
|
|
|
wm.FirstName = e.FirstName
|
|
|
|
wm.LastName = e.LastName
|
|
|
|
wm.NickName = e.NickName
|
|
|
|
wm.DisplayName = e.DisplayName
|
|
|
|
wm.PreferredLanguage = e.PreferredLanguage
|
|
|
|
wm.Gender = e.Gender
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
wm.FirstName = e.FirstName
|
|
|
|
wm.LastName = e.LastName
|
|
|
|
wm.NickName = e.NickName
|
|
|
|
wm.DisplayName = e.DisplayName
|
|
|
|
wm.PreferredLanguage = e.PreferredLanguage
|
|
|
|
wm.Gender = e.Gender
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanProfileChangedEvent:
|
2021-01-06 10:12:56 +00:00
|
|
|
if e.FirstName != "" {
|
|
|
|
wm.FirstName = e.FirstName
|
|
|
|
}
|
|
|
|
if e.LastName != "" {
|
|
|
|
wm.LastName = e.LastName
|
|
|
|
}
|
|
|
|
if e.NickName != nil {
|
|
|
|
wm.NickName = *e.NickName
|
|
|
|
}
|
|
|
|
if e.DisplayName != nil {
|
|
|
|
wm.DisplayName = *e.DisplayName
|
|
|
|
}
|
|
|
|
if e.PreferredLanguage != nil {
|
|
|
|
wm.PreferredLanguage = *e.PreferredLanguage
|
|
|
|
}
|
|
|
|
if e.Gender != nil {
|
|
|
|
wm.Gender = *e.Gender
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanProfileWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanProfileWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
nickName,
|
|
|
|
displayName string,
|
|
|
|
preferredLanguage language.Tag,
|
|
|
|
gender domain.Gender,
|
|
|
|
) (*user.HumanProfileChangedEvent, bool) {
|
|
|
|
hasChanged := false
|
|
|
|
changedEvent := user.NewHumanProfileChangedEvent(ctx)
|
|
|
|
if wm.FirstName != firstName {
|
|
|
|
hasChanged = true
|
|
|
|
changedEvent.FirstName = firstName
|
|
|
|
}
|
|
|
|
if wm.LastName != lastName {
|
|
|
|
hasChanged = true
|
|
|
|
changedEvent.LastName = lastName
|
|
|
|
}
|
|
|
|
if wm.NickName != nickName {
|
|
|
|
hasChanged = true
|
2021-01-06 10:12:56 +00:00
|
|
|
changedEvent.NickName = &nickName
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.DisplayName != displayName {
|
|
|
|
hasChanged = true
|
2021-01-06 10:12:56 +00:00
|
|
|
changedEvent.DisplayName = &displayName
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.PreferredLanguage != preferredLanguage {
|
|
|
|
hasChanged = true
|
2021-01-06 10:12:56 +00:00
|
|
|
changedEvent.PreferredLanguage = &preferredLanguage
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if gender.Valid() && wm.Gender != gender {
|
|
|
|
hasChanged = true
|
2021-01-06 10:12:56 +00:00
|
|
|
changedEvent.Gender = &gender
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return changedEvent, hasChanged
|
|
|
|
}
|