2022-02-21 12:22:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
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/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-21 12:22:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMSMSConfigWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
ID string
|
|
|
|
Twilio *TwilioConfig
|
|
|
|
State domain.SMSConfigState
|
|
|
|
}
|
|
|
|
|
|
|
|
type TwilioConfig struct {
|
|
|
|
SID string
|
|
|
|
Token *crypto.CryptoValue
|
|
|
|
SenderNumber string
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewIAMSMSConfigWriteModel(instanceID, id string) *IAMSMSConfigWriteModel {
|
2022-02-21 12:22:20 +00:00
|
|
|
return &IAMSMSConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: instanceID,
|
|
|
|
ResourceOwner: instanceID,
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMSMSConfigWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigTwilioAddedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Twilio = &TwilioConfig{
|
|
|
|
SID: e.SID,
|
|
|
|
Token: e.Token,
|
|
|
|
SenderNumber: e.SenderNumber,
|
|
|
|
}
|
|
|
|
wm.State = domain.SMSConfigStateInactive
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigTwilioChangedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e.SID != nil {
|
|
|
|
wm.Twilio.SID = *e.SID
|
|
|
|
}
|
|
|
|
if e.SenderNumber != nil {
|
|
|
|
wm.Twilio.SenderNumber = *e.SenderNumber
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigTwilioTokenChangedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Twilio.Token = e.Token
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigActivatedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.SMSConfigStateActive
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigDeactivatedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.SMSConfigStateInactive
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigRemovedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Twilio = nil
|
|
|
|
wm.State = domain.SMSConfigStateRemoved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
func (wm *IAMSMSConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2022-02-21 12:22:20 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.SMSConfigTwilioAddedEventType,
|
|
|
|
instance.SMSConfigTwilioChangedEventType,
|
|
|
|
instance.SMSConfigTwilioTokenChangedEventType,
|
|
|
|
instance.SMSConfigActivatedEventType,
|
|
|
|
instance.SMSConfigDeactivatedEventType,
|
|
|
|
instance.SMSConfigRemovedEventType).
|
2022-02-21 12:22:20 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *IAMSMSConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id, sid, senderNumber string) (*instance.SMSConfigTwilioChangedEvent, bool, error) {
|
|
|
|
changes := make([]instance.SMSConfigTwilioChanges, 0)
|
2022-02-21 12:22:20 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if wm.Twilio.SID != sid {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMSConfigTwilioSID(sid))
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
if wm.Twilio.SenderNumber != senderNumber {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMSConfigTwilioSenderNumber(senderNumber))
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changeEvent, err := instance.NewSMSConfigTwilioChangedEvent(ctx, aggregate, id, changes)
|
2022-02-21 12:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|