2021-01-07 16:06:45 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 14:48:27 +01:00
|
|
|
"time"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-04-03 13:43:43 +03:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-07 16:06:45 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanPhoneWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-03-14 20:20:38 +01:00
|
|
|
Phone domain.PhoneNumber
|
2021-01-07 16:06:45 +01:00
|
|
|
IsPhoneVerified bool
|
|
|
|
|
2021-01-15 09:32:59 +01:00
|
|
|
Code *crypto.CryptoValue
|
|
|
|
CodeCreationDate time.Time
|
|
|
|
CodeExpiry time.Duration
|
2024-09-26 09:14:33 +02:00
|
|
|
GeneratorID string
|
|
|
|
VerificationID string
|
2021-01-15 09:32:59 +01:00
|
|
|
|
2021-03-19 11:12:56 +01:00
|
|
|
State domain.PhoneState
|
|
|
|
UserState domain.UserState
|
2021-01-07 16:06:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
func NewHumanPhoneWriteModel(userID, resourceOwner string) *HumanPhoneWriteModel {
|
2021-01-07 16:06:45 +01:00
|
|
|
return &HumanPhoneWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-07 16:06:45 +01: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 11:12:56 +01:00
|
|
|
wm.State = domain.PhoneStateActive
|
2021-01-07 16:06:45 +01:00
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 16:06:45 +01:00
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
if e.PhoneNumber != "" {
|
|
|
|
wm.Phone = e.PhoneNumber
|
|
|
|
wm.State = domain.PhoneStateActive
|
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-03-22 14:40:25 +01:00
|
|
|
case *user.HumanInitialCodeAddedEvent:
|
|
|
|
wm.UserState = domain.UserStateInitial
|
|
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 16:06:45 +01:00
|
|
|
case *user.HumanPhoneChangedEvent:
|
|
|
|
wm.Phone = e.PhoneNumber
|
|
|
|
wm.IsPhoneVerified = false
|
|
|
|
wm.State = domain.PhoneStateActive
|
2021-01-15 09:32:59 +01:00
|
|
|
wm.Code = nil
|
2021-01-07 16:06:45 +01:00
|
|
|
case *user.HumanPhoneVerifiedEvent:
|
|
|
|
wm.IsPhoneVerified = true
|
2021-01-15 09:32:59 +01:00
|
|
|
wm.Code = nil
|
|
|
|
case *user.HumanPhoneCodeAddedEvent:
|
|
|
|
wm.Code = e.Code
|
|
|
|
wm.CodeCreationDate = e.CreationDate()
|
|
|
|
wm.CodeExpiry = e.Expiry
|
2024-09-26 09:14:33 +02:00
|
|
|
wm.GeneratorID = e.GeneratorID
|
|
|
|
case *user.HumanPhoneCodeSentEvent:
|
|
|
|
wm.GeneratorID = e.GeneratorInfo.GetID()
|
|
|
|
wm.VerificationID = e.GeneratorInfo.GetVerificationID()
|
2021-01-07 16:06:45 +01:00
|
|
|
case *user.HumanPhoneRemovedEvent:
|
|
|
|
wm.State = domain.PhoneStateRemoved
|
2022-05-02 11:06:30 +02:00
|
|
|
wm.IsPhoneVerified = false
|
|
|
|
wm.Phone = ""
|
2021-01-07 16:06:45 +01:00
|
|
|
case *user.UserRemovedEvent:
|
2021-03-19 11:12:56 +01:00
|
|
|
wm.UserState = domain.UserStateDeleted
|
2022-05-02 11:06:30 +02:00
|
|
|
wm.IsPhoneVerified = false
|
|
|
|
wm.Phone = ""
|
2021-01-07 16:06:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPhoneWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(user.HumanAddedType,
|
|
|
|
user.HumanRegisteredType,
|
2021-03-22 17:15:24 +01:00
|
|
|
user.HumanInitialCodeAddedType,
|
|
|
|
user.HumanInitializedCheckSucceededType,
|
2021-02-18 14:48:27 +01:00
|
|
|
user.HumanPhoneChangedType,
|
|
|
|
user.HumanPhoneVerifiedType,
|
|
|
|
user.HumanPhoneCodeAddedType,
|
2024-09-26 09:14:33 +02:00
|
|
|
user.HumanPhoneCodeSentType,
|
2021-02-18 14:48:27 +01:00
|
|
|
user.HumanPhoneRemovedType,
|
2021-04-06 09:57:58 +02:00
|
|
|
user.UserRemovedType,
|
|
|
|
user.UserV1AddedType,
|
|
|
|
user.UserV1RegisteredType,
|
|
|
|
user.UserV1InitialCodeAddedType,
|
|
|
|
user.UserV1InitializedCheckSucceededType,
|
|
|
|
user.UserV1PhoneCodeAddedType,
|
|
|
|
user.UserV1PhoneChangedType,
|
|
|
|
user.UserV1PhoneVerifiedType,
|
2021-07-06 13:55:57 +02:00
|
|
|
user.UserV1PhoneRemovedType).
|
|
|
|
Builder()
|
2021-01-07 16:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPhoneWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2023-03-14 20:20:38 +01:00
|
|
|
phone domain.PhoneNumber,
|
2021-01-07 16:06:45 +01:00
|
|
|
) (*user.HumanPhoneChangedEvent, bool) {
|
2021-03-19 11:12:56 +01:00
|
|
|
changedEvent := user.NewHumanPhoneChangedEvent(ctx, aggregate, phone)
|
|
|
|
return changedEvent, phone != wm.Phone
|
2021-01-07 16:06:45 +01:00
|
|
|
}
|