2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanPhoneWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Phone string
|
|
|
|
IsPhoneVerified bool
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
Code *crypto.CryptoValue
|
|
|
|
CodeCreationDate time.Time
|
|
|
|
CodeExpiry time.Duration
|
|
|
|
|
2021-03-19 10:12:56 +00:00
|
|
|
State domain.PhoneState
|
|
|
|
UserState domain.UserState
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewHumanPhoneWriteModel(userID, resourceOwner string) *HumanPhoneWriteModel {
|
2021-01-07 15:06:45 +00:00
|
|
|
return &HumanPhoneWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-07 15:06:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPhoneWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
|
|
|
if e.PhoneNumber != "" {
|
|
|
|
wm.Phone = e.PhoneNumber
|
2021-03-19 10:12:56 +00:00
|
|
|
wm.State = domain.PhoneStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
if e.PhoneNumber != "" {
|
|
|
|
wm.Phone = e.PhoneNumber
|
|
|
|
wm.State = domain.PhoneStateActive
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanPhoneChangedEvent:
|
|
|
|
wm.Phone = e.PhoneNumber
|
|
|
|
wm.IsPhoneVerified = false
|
|
|
|
wm.State = domain.PhoneStateActive
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.Code = nil
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanPhoneVerifiedEvent:
|
|
|
|
wm.IsPhoneVerified = true
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.Code = nil
|
|
|
|
case *user.HumanPhoneCodeAddedEvent:
|
|
|
|
wm.Code = e.Code
|
|
|
|
wm.CodeCreationDate = e.CreationDate()
|
|
|
|
wm.CodeExpiry = e.Expiry
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.HumanPhoneRemovedEvent:
|
|
|
|
wm.State = domain.PhoneStateRemoved
|
|
|
|
case *user.UserRemovedEvent:
|
2021-03-19 10:12:56 +00:00
|
|
|
wm.UserState = domain.UserStateDeleted
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPhoneWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(user.HumanAddedType,
|
|
|
|
user.HumanRegisteredType,
|
|
|
|
user.HumanPhoneChangedType,
|
|
|
|
user.HumanPhoneVerifiedType,
|
|
|
|
user.HumanPhoneCodeAddedType,
|
|
|
|
user.HumanPhoneRemovedType,
|
|
|
|
user.UserRemovedType)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPhoneWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-07 15:06:45 +00:00
|
|
|
phone string,
|
|
|
|
) (*user.HumanPhoneChangedEvent, bool) {
|
2021-03-19 10:12:56 +00:00
|
|
|
changedEvent := user.NewHumanPhoneChangedEvent(ctx, aggregate, phone)
|
|
|
|
return changedEvent, phone != wm.Phone
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|