2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanPhoneWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
Phone domain.PhoneNumber
|
2021-01-07 15:06:45 +00:00
|
|
|
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-03-22 13:40:25 +00:00
|
|
|
case *user.HumanInitialCodeAddedEvent:
|
|
|
|
wm.UserState = domain.UserStateInitial
|
|
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
|
|
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
|
2022-05-02 09:06:30 +00:00
|
|
|
wm.IsPhoneVerified = false
|
|
|
|
wm.Phone = ""
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.UserRemovedEvent:
|
2021-03-19 10:12:56 +00:00
|
|
|
wm.UserState = domain.UserStateDeleted
|
2022-05-02 09:06:30 +00:00
|
|
|
wm.IsPhoneVerified = false
|
|
|
|
wm.Phone = ""
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPhoneWriteModel) 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-02-18 13:48:27 +00:00
|
|
|
EventTypes(user.HumanAddedType,
|
|
|
|
user.HumanRegisteredType,
|
2021-03-22 16:15:24 +00:00
|
|
|
user.HumanInitialCodeAddedType,
|
|
|
|
user.HumanInitializedCheckSucceededType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanPhoneChangedType,
|
|
|
|
user.HumanPhoneVerifiedType,
|
|
|
|
user.HumanPhoneCodeAddedType,
|
|
|
|
user.HumanPhoneRemovedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserRemovedType,
|
|
|
|
user.UserV1AddedType,
|
|
|
|
user.UserV1RegisteredType,
|
|
|
|
user.UserV1InitialCodeAddedType,
|
|
|
|
user.UserV1InitializedCheckSucceededType,
|
|
|
|
user.UserV1PhoneCodeAddedType,
|
|
|
|
user.UserV1PhoneChangedType,
|
|
|
|
user.UserV1PhoneVerifiedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
user.UserV1PhoneRemovedType).
|
|
|
|
Builder()
|
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,
|
2023-03-14 19:20:38 +00:00
|
|
|
phone domain.PhoneNumber,
|
2021-01-07 15:06:45 +00:00
|
|
|
) (*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
|
|
|
}
|