zitadel/internal/command/sms_config_model.go
Fabi 7d235e3eed
feat: Default configs sms provider (#3187)
* feat: sms config

* feat: twilio as sms provider

* feat:sms projection

* feat: sms queries

* feat: sms queries test

* feat: sms configs

* feat: sms configs sql file

* fix merge

* fix: rename from to sendername

* fix: proto comments

* fix: token as crypto

* fix: tests

* fix: sms config sender name to sender number

* fix: sms config sender name to sender number

* Update email.go

* Update channel.go

* Update V1.111__settings.sql

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-02-21 12:22:20 +00:00

120 lines
2.9 KiB
Go

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