2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanAddressWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Country string
|
|
|
|
Locality string
|
|
|
|
PostalCode string
|
|
|
|
Region string
|
|
|
|
StreetAddress string
|
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
State domain.AddressState
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewHumanAddressWriteModel(userID, resourceOwner string) *HumanAddressWriteModel {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanAddressWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanAddressWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
|
|
|
wm.Country = e.Country
|
|
|
|
wm.Locality = e.Locality
|
|
|
|
wm.PostalCode = e.PostalCode
|
|
|
|
wm.Region = e.Region
|
|
|
|
wm.StreetAddress = e.StreetAddress
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.State = domain.AddressStateActive
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
wm.Country = e.Country
|
|
|
|
wm.Locality = e.Locality
|
|
|
|
wm.PostalCode = e.PostalCode
|
|
|
|
wm.Region = e.Region
|
|
|
|
wm.StreetAddress = e.StreetAddress
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.State = domain.AddressStateActive
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.HumanAddressChangedEvent:
|
2021-01-06 10:12:56 +00:00
|
|
|
if e.Country != nil {
|
|
|
|
wm.Country = *e.Country
|
|
|
|
}
|
|
|
|
if e.Locality != nil {
|
|
|
|
wm.Locality = *e.Locality
|
|
|
|
}
|
|
|
|
if e.PostalCode != nil {
|
|
|
|
wm.PostalCode = *e.PostalCode
|
|
|
|
}
|
|
|
|
if e.Region != nil {
|
|
|
|
wm.Region = *e.Region
|
|
|
|
}
|
|
|
|
if e.StreetAddress != nil {
|
|
|
|
wm.StreetAddress = *e.StreetAddress
|
|
|
|
}
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.UserRemovedEvent:
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.State = domain.AddressStateRemoved
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanAddressWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-04-06 07:57:58 +00:00
|
|
|
EventTypes(user.UserV1AddedType,
|
|
|
|
user.UserV1RegisteredType,
|
|
|
|
user.UserV1AddressChangedType,
|
|
|
|
user.HumanAddedType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanRegisteredType,
|
|
|
|
user.HumanAddressChangedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
user.UserRemovedType).
|
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanAddressWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 13:52:13 +00:00
|
|
|
country,
|
|
|
|
locality,
|
|
|
|
postalCode,
|
|
|
|
region,
|
|
|
|
streetAddress string,
|
2021-03-19 10:12:56 +00:00
|
|
|
) (*user.HumanAddressChangedEvent, bool, error) {
|
|
|
|
changes := make([]user.AddressChanges, 0)
|
|
|
|
var err error
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.Country != country {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangeCountry(country))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.Locality != locality {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangeLocality(locality))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.PostalCode != postalCode {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangePostalCode(postalCode))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.Region != region {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangeRegion(region))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.StreetAddress != streetAddress {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangeStreetAddress(streetAddress))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
changeEvent, err := user.NewAddressChangedEvent(ctx, aggregate, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
return changeEvent, true, nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|