2021-01-04 13:52:13 +00:00
|
|
|
package user
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-01-03 08:19:07 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-10 15:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-04 13:52:13 +00:00
|
|
|
addressEventPrefix = humanEventPrefix + "address."
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanAddressChangedType = addressEventPrefix + "changed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanAddressChangedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
Country *string `json:"country,omitempty"`
|
|
|
|
Locality *string `json:"locality,omitempty"`
|
|
|
|
PostalCode *string `json:"postalCode,omitempty"`
|
|
|
|
Region *string `json:"region,omitempty"`
|
|
|
|
StreetAddress *string `json:"streetAddress,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *HumanAddressChangedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *HumanAddressChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 10:12:56 +00:00
|
|
|
func NewAddressChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
changes []AddressChanges,
|
|
|
|
) (*HumanAddressChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "USER-3n8fs", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &HumanAddressChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanAddressChangedType,
|
|
|
|
),
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressChanges func(event *HumanAddressChangedEvent)
|
|
|
|
|
|
|
|
func ChangeCountry(country string) func(event *HumanAddressChangedEvent) {
|
|
|
|
return func(e *HumanAddressChangedEvent) {
|
|
|
|
e.Country = &country
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeLocality(locality string) func(event *HumanAddressChangedEvent) {
|
|
|
|
return func(e *HumanAddressChangedEvent) {
|
|
|
|
e.Locality = &locality
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangePostalCode(code string) func(event *HumanAddressChangedEvent) {
|
|
|
|
return func(e *HumanAddressChangedEvent) {
|
|
|
|
e.PostalCode = &code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeRegion(region string) func(event *HumanAddressChangedEvent) {
|
|
|
|
return func(e *HumanAddressChangedEvent) {
|
|
|
|
e.Region = ®ion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeStreetAddress(street string) func(event *HumanAddressChangedEvent) {
|
|
|
|
return func(e *HumanAddressChangedEvent) {
|
|
|
|
e.StreetAddress = &street
|
|
|
|
}
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func HumanAddressChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
addressChanged := &HumanAddressChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, addressChanged)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-5M0pd", "unable to unmarshal human address changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return addressChanged, nil
|
|
|
|
}
|