feat: add http as sms provider (#8540)

# Which Problems Are Solved

Send SMS messages as a HTTP call to a relay, for own logic on handling
different SMS providers.

# How the Problems Are Solved

Add HTTP as SMS provider type and handling of webhook messages in the
notification handlers.

# Additional Changes

Clean up old Twilio events, which were supposed to handle the general
SMS providers with deactivate, activate and remove.

# Additional Context

Partially closes #8270

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
Stefan Benz
2024-09-06 15:11:36 +02:00
committed by GitHub
parent d2e0ac07f1
commit 5bdf1a4547
26 changed files with 2536 additions and 593 deletions

View File

@@ -12,9 +12,11 @@ import (
type IAMSMSConfigWriteModel struct {
eventstore.WriteModel
ID string
Twilio *TwilioConfig
State domain.SMSConfigState
ID string
Description string
Twilio *TwilioConfig
HTTP *HTTPConfig
State domain.SMSConfigState
}
type TwilioConfig struct {
@@ -23,6 +25,10 @@ type TwilioConfig struct {
SenderNumber string
}
type HTTPConfig struct {
Endpoint string
}
func NewIAMSMSConfigWriteModel(instanceID, id string) *IAMSMSConfigWriteModel {
return &IAMSMSConfigWriteModel{
WriteModel: eventstore.WriteModel{
@@ -46,11 +52,15 @@ func (wm *IAMSMSConfigWriteModel) Reduce() error {
Token: e.Token,
SenderNumber: e.SenderNumber,
}
wm.Description = e.Description
wm.State = domain.SMSConfigStateInactive
case *instance.SMSConfigTwilioChangedEvent:
if wm.ID != e.ID {
continue
}
if e.Description != nil {
wm.Description = *e.Description
}
if e.SID != nil {
wm.Twilio.SID = *e.SID
}
@@ -62,6 +72,42 @@ func (wm *IAMSMSConfigWriteModel) Reduce() error {
continue
}
wm.Twilio.Token = e.Token
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 {
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
case *instance.SMSConfigActivatedEvent:
if wm.ID != e.ID {
continue
@@ -77,6 +123,7 @@ func (wm *IAMSMSConfigWriteModel) Reduce() error {
continue
}
wm.Twilio = nil
wm.HTTP = nil
wm.State = domain.SMSConfigStateRemoved
}
}
@@ -92,21 +139,33 @@ func (wm *IAMSMSConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
instance.SMSConfigTwilioAddedEventType,
instance.SMSConfigTwilioChangedEventType,
instance.SMSConfigTwilioTokenChangedEventType,
instance.SMSConfigHTTPAddedEventType,
instance.SMSConfigHTTPChangedEventType,
instance.SMSConfigTwilioActivatedEventType,
instance.SMSConfigTwilioDeactivatedEventType,
instance.SMSConfigTwilioRemovedEventType,
instance.SMSConfigActivatedEventType,
instance.SMSConfigDeactivatedEventType,
instance.SMSConfigRemovedEventType).
Builder()
}
func (wm *IAMSMSConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id, sid, senderNumber string) (*instance.SMSConfigTwilioChangedEvent, bool, error) {
func (wm *IAMSMSConfigWriteModel) NewTwilioChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id string, description, sid, senderNumber *string) (*instance.SMSConfigTwilioChangedEvent, bool, error) {
changes := make([]instance.SMSConfigTwilioChanges, 0)
var err error
if wm.Twilio.SID != sid {
changes = append(changes, instance.ChangeSMSConfigTwilioSID(sid))
if wm.Twilio == nil {
return nil, false, nil
}
if wm.Twilio.SenderNumber != senderNumber {
changes = append(changes, instance.ChangeSMSConfigTwilioSenderNumber(senderNumber))
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))
}
if senderNumber != nil && wm.Twilio.SenderNumber != *senderNumber {
changes = append(changes, instance.ChangeSMSConfigTwilioSenderNumber(*senderNumber))
}
if len(changes) == 0 {
@@ -118,3 +177,28 @@ func (wm *IAMSMSConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate
}
return changeEvent, true, nil
}
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
}