mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 02:54:20 +00:00
5349d96ce4
* sub queries * fix: tests * add builder to tests * new search query * rename searchquerybuilder to builder * remove comment from code * test with multiple queries * add filters test * fix(contibute): listing * add validate module * fix: search queries * remove unused event type in query * ignore query if error in marshal * go mod tidy * update privacy policy query * update queries Co-authored-by: Livio Amstutz <livio.a@gmail.com>
137 lines
3.4 KiB
Go
137 lines
3.4 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
"github.com/caos/zitadel/internal/repository/user"
|
|
)
|
|
|
|
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, resourceOwner string) *HumanProfileWriteModel {
|
|
return &HumanProfileWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: userID,
|
|
ResourceOwner: resourceOwner,
|
|
},
|
|
}
|
|
}
|
|
|
|
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:
|
|
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
|
|
}
|
|
case *user.UserRemovedEvent:
|
|
wm.UserState = domain.UserStateDeleted
|
|
}
|
|
}
|
|
return wm.WriteModel.Reduce()
|
|
}
|
|
|
|
func (wm *HumanProfileWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
ResourceOwner(wm.ResourceOwner).
|
|
AddQuery().
|
|
AggregateTypes(user.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
EventTypes(user.HumanAddedType,
|
|
user.HumanRegisteredType,
|
|
user.HumanProfileChangedType,
|
|
user.UserRemovedType,
|
|
user.UserV1AddedType,
|
|
user.UserV1RegisteredType,
|
|
user.UserV1ProfileChangedType).
|
|
Builder()
|
|
}
|
|
|
|
func (wm *HumanProfileWriteModel) NewChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
firstName,
|
|
lastName,
|
|
nickName,
|
|
displayName string,
|
|
preferredLanguage language.Tag,
|
|
gender domain.Gender,
|
|
) (*user.HumanProfileChangedEvent, bool, error) {
|
|
changes := make([]user.ProfileChanges, 0)
|
|
var err error
|
|
|
|
if wm.FirstName != firstName {
|
|
changes = append(changes, user.ChangeFirstName(firstName))
|
|
}
|
|
if wm.LastName != lastName {
|
|
changes = append(changes, user.ChangeLastName(lastName))
|
|
}
|
|
if wm.NickName != nickName {
|
|
changes = append(changes, user.ChangeNickName(nickName))
|
|
}
|
|
if wm.DisplayName != displayName {
|
|
changes = append(changes, user.ChangeDisplayName(displayName))
|
|
}
|
|
if wm.PreferredLanguage != preferredLanguage {
|
|
changes = append(changes, user.ChangePreferredLanguage(preferredLanguage))
|
|
}
|
|
if wm.Gender != gender {
|
|
changes = append(changes, user.ChangeGender(gender))
|
|
}
|
|
if len(changes) == 0 {
|
|
return nil, false, nil
|
|
}
|
|
changeEvent, err := user.NewHumanProfileChangedEvent(ctx, aggregate, changes)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return changeEvent, true, nil
|
|
}
|