-
Notifications
You must be signed in to change notification settings - Fork 13
/
sender.go
156 lines (125 loc) · 3.98 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sms
import (
"fmt"
"github.com/coretrix/hitrix/example/entity"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/latolukasz/orm"
)
type ISender interface {
SendOTPSMS(*OTP) error
SendOTPCallout(*OTP) error
SendMessage(*Message) error
}
type Sender struct {
OrmService *orm.Engine
Clock clock.Clock
GatewayFactory map[string]Gateway
Logger LogEntity
}
func (s *Sender) SendOTPSMS(otp *OTP) error {
primaryProvider := otp.Provider.Primary
secondaryProvider := otp.Provider.Secondary
primaryGateway, ok := s.GatewayFactory[primaryProvider]
if !ok {
return fmt.Errorf("primary provider not supported")
}
otp.OTP = fmt.Sprintf(otp.Template, otp.OTP)
smsTrackerEntity := s.Logger
smsTrackerEntity.SetTo(otp.Number)
smsTrackerEntity.SetType(entity.SMSTrackerTypeSMS)
smsTrackerEntity.SetText(otp.OTP)
smsTrackerEntity.SetFromPrimaryGateway(primaryProvider)
smsTrackerEntity.SetSentAt(s.Clock.Now())
trySecondaryProvider := false
status, err := primaryGateway.SendOTPSMS(otp)
if err != nil {
trySecondaryProvider = true
smsTrackerEntity.SetPrimaryGatewayError(err.Error())
}
if trySecondaryProvider {
secondaryGateway, ok := s.GatewayFactory[secondaryProvider]
if !ok {
return fmt.Errorf("secondary provider not supported")
}
smsTrackerEntity.SetFromSecondaryGateway(secondaryProvider)
status, err = secondaryGateway.SendOTPSMS(otp)
if err != nil {
smsTrackerEntity.SetSecondaryGatewayError(err.Error())
}
}
smsTrackerEntity.SetStatus(status)
logger := NewSmsLog(s.OrmService, smsTrackerEntity)
logger.Do()
return nil
}
func (s *Sender) SendOTPCallout(otp *OTP) error {
primaryProvider := otp.Provider.Primary
secondaryProvider := otp.Provider.Secondary
primaryGateway, ok := s.GatewayFactory[primaryProvider]
if !ok {
return fmt.Errorf("primary provider not supported")
}
otp.OTP = fmt.Sprintf(otp.Template, otp.OTP)
smsTrackerEntity := s.Logger
smsTrackerEntity.SetTo(otp.Number)
smsTrackerEntity.SetType(entity.SMSTrackerTypeCallout)
smsTrackerEntity.SetText(otp.OTP)
smsTrackerEntity.SetFromPrimaryGateway(primaryProvider)
smsTrackerEntity.SetSentAt(s.Clock.Now())
trySecondaryProvider := false
status, err := primaryGateway.SendOTPCallout(otp)
if err != nil {
trySecondaryProvider = true
smsTrackerEntity.SetPrimaryGatewayError(err.Error())
}
if trySecondaryProvider {
secondaryGateway, ok := s.GatewayFactory[secondaryProvider]
if !ok {
return fmt.Errorf("secondary provider not supported")
}
smsTrackerEntity.SetFromSecondaryGateway(secondaryProvider)
status, err = secondaryGateway.SendOTPCallout(otp)
if err != nil {
smsTrackerEntity.SetSecondaryGatewayError(err.Error())
}
}
smsTrackerEntity.SetStatus(status)
logger := NewSmsLog(s.OrmService, smsTrackerEntity)
logger.Do()
return nil
}
func (s *Sender) SendMessage(message *Message) error {
primaryProvider := message.Provider.Primary
secondaryProvider := message.Provider.Secondary
primaryGateway, ok := s.GatewayFactory[primaryProvider]
if !ok {
return fmt.Errorf("primary provider not supported")
}
smsTrackerEntity := s.Logger
smsTrackerEntity.SetTo(message.Number)
smsTrackerEntity.SetType(entity.SMSTrackerTypeCallout)
smsTrackerEntity.SetText(message.Text)
smsTrackerEntity.SetFromPrimaryGateway(primaryProvider)
smsTrackerEntity.SetSentAt(s.Clock.Now())
trySecondaryProvider := false
status, err := primaryGateway.SendSMSMessage(message)
if err != nil {
trySecondaryProvider = true
smsTrackerEntity.SetPrimaryGatewayError(err.Error())
}
if trySecondaryProvider {
secondaryGateway, ok := s.GatewayFactory[secondaryProvider]
if !ok {
return fmt.Errorf("secondary provider not supported")
}
smsTrackerEntity.SetFromSecondaryGateway(secondaryProvider)
status, err = secondaryGateway.SendSMSMessage(message)
if err != nil {
smsTrackerEntity.SetSecondaryGatewayError(err.Error())
}
}
smsTrackerEntity.SetStatus(status)
logger := NewSmsLog(s.OrmService, smsTrackerEntity)
logger.Do()
return nil
}