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
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
ID string
|
|
|
|
Description string
|
|
|
|
Twilio *TwilioConfig
|
|
|
|
HTTP *HTTPConfig
|
|
|
|
State domain.SMSConfigState
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TwilioConfig struct {
|
|
|
|
SID string
|
|
|
|
Token *crypto.CryptoValue
|
|
|
|
SenderNumber string
|
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
type HTTPConfig struct {
|
|
|
|
Endpoint 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,
|
2023-10-19 10:19:10 +00:00
|
|
|
InstanceID: 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,
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
wm.Description = e.Description
|
2022-02-21 12:22:20 +00:00
|
|
|
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
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if e.Description != nil {
|
|
|
|
wm.Description = *e.Description
|
|
|
|
}
|
2022-02-21 12:22:20 +00:00
|
|
|
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
|
2024-09-06 13:11:36 +00:00
|
|
|
case *instance.SMSConfigHTTPAddedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.HTTP = &HTTPConfig{
|
|
|
|
Endpoint: e.Endpoint,
|
|
|
|
}
|
|
|
|
wm.Description = e.Description
|
|
|
|
wm.State = domain.SMSConfigStateInactive
|
|
|
|
case *instance.SMSConfigHTTPChangedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e.Description != nil {
|
|
|
|
wm.Description = *e.Description
|
|
|
|
}
|
|
|
|
if e.Endpoint != nil {
|
|
|
|
wm.HTTP.Endpoint = *e.Endpoint
|
|
|
|
}
|
|
|
|
case *instance.SMSConfigTwilioActivatedEvent:
|
|
|
|
if wm.ID != e.ID {
|
2024-09-13 13:22:25 +00:00
|
|
|
wm.State = domain.SMSConfigStateInactive
|
2024-09-06 13:11:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.SMSConfigStateActive
|
|
|
|
case *instance.SMSConfigTwilioDeactivatedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.State = domain.SMSConfigStateInactive
|
|
|
|
case *instance.SMSConfigTwilioRemovedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Twilio = nil
|
|
|
|
wm.HTTP = nil
|
|
|
|
wm.State = domain.SMSConfigStateRemoved
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMSConfigActivatedEvent:
|
2022-02-21 12:22:20 +00:00
|
|
|
if wm.ID != e.ID {
|
2024-09-13 13:22:25 +00:00
|
|
|
wm.State = domain.SMSConfigStateInactive
|
2022-02-21 12:22:20 +00:00
|
|
|
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
|
2024-09-06 13:11:36 +00:00
|
|
|
wm.HTTP = nil
|
2022-02-21 12:22:20 +00:00
|
|
|
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,
|
2024-09-06 13:11:36 +00:00
|
|
|
instance.SMSConfigHTTPAddedEventType,
|
|
|
|
instance.SMSConfigHTTPChangedEventType,
|
|
|
|
instance.SMSConfigTwilioActivatedEventType,
|
|
|
|
instance.SMSConfigTwilioDeactivatedEventType,
|
|
|
|
instance.SMSConfigTwilioRemovedEventType,
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.SMSConfigActivatedEventType,
|
|
|
|
instance.SMSConfigDeactivatedEventType,
|
|
|
|
instance.SMSConfigRemovedEventType).
|
2022-02-21 12:22:20 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
func (wm *IAMSMSConfigWriteModel) NewTwilioChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id string, description, sid, senderNumber *string) (*instance.SMSConfigTwilioChangedEvent, bool, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes := make([]instance.SMSConfigTwilioChanges, 0)
|
2022-02-21 12:22:20 +00:00
|
|
|
var err error
|
|
|
|
|
2024-09-06 13:11:36 +00:00
|
|
|
if wm.Twilio == nil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if description != nil && wm.Description != *description {
|
|
|
|
changes = append(changes, instance.ChangeSMSConfigTwilioDescription(*description))
|
|
|
|
}
|
|
|
|
if sid != nil && wm.Twilio.SID != *sid {
|
|
|
|
changes = append(changes, instance.ChangeSMSConfigTwilioSID(*sid))
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
if senderNumber != nil && wm.Twilio.SenderNumber != *senderNumber {
|
|
|
|
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
|
|
|
|
}
|
2024-09-06 13:11:36 +00:00
|
|
|
|
|
|
|
func (wm *IAMSMSConfigWriteModel) NewHTTPChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id string, description, endpoint *string) (*instance.SMSConfigHTTPChangedEvent, bool, error) {
|
|
|
|
changes := make([]instance.SMSConfigHTTPChanges, 0)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if wm.HTTP == nil {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if description != nil && wm.Description != *description {
|
|
|
|
changes = append(changes, instance.ChangeSMSConfigHTTPDescription(*description))
|
|
|
|
}
|
|
|
|
if endpoint != nil && wm.HTTP.Endpoint != *endpoint {
|
|
|
|
changes = append(changes, instance.ChangeSMSConfigHTTPEndpoint(*endpoint))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
changeEvent, err := instance.NewSMSConfigHTTPChangedEvent(ctx, aggregate, id, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|