/
service.go
executable file
·161 lines (132 loc) · 4.71 KB
/
service.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
157
158
159
160
161
package service
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/emadghaffari/virgool/notification/conf"
"github.com/emadghaffari/virgool/notification/database/redis"
"github.com/emadghaffari/virgool/notification/model/notif"
"github.com/emadghaffari/virgool/notification/pkg/grpc/pb"
)
// NotificationService describes the service.
type NotificationService interface {
// Add your methods here
SMS(ctx context.Context, to, body string) (message, status string, err error)
SMST(ctx context.Context, to string, params map[string]string, template string) (message, status string, err error)
Email(ctx context.Context, to, body string, data interface{}) (message, status string, err error)
Verify(ctx context.Context, phone, code string) (message, status string, data interface{}, err error)
}
type basicNotificationService struct{}
// send sms with body
func (b *basicNotificationService) SMS(ctx context.Context, to string, body string) (message string, status string, err error) {
var tracer opentracing.Tracer
var span opentracing.Span
if parent := opentracing.SpanFromContext(ctx); parent != nil {
pctx := parent.Context()
if tracer = opentracing.GlobalTracer(); tracer != nil {
span = tracer.StartSpan("SendWithBody", opentracing.ChildOf(pctx))
defer span.Finish()
}
}
objs := make(map[string]interface{}, 2)
objs["phone"] = to
objs["body"] = body
ntf := notif.Notification{
Data: objs,
Type: "SMS",
}
if notifire, err := notif.GetNotifier(notif.SMS); err == nil {
err := notifire.SendWithBody(ctx, ntf, notif.Line(conf.GlobalConfigs.Notif.SMS.Send.LineNumber[0]))
if err != nil {
return "ERROR IN SEND SMS", "FAILED", err
}
}
return "SUCCESS", "OK", err
}
func (b *basicNotificationService) SMST(ctx context.Context, to string, params map[string]string, template string) (message string, status string, err error) {
var tracer opentracing.Tracer
var span opentracing.Span
if parent := opentracing.SpanFromContext(ctx); parent != nil {
pctx := parent.Context()
if tracer = opentracing.GlobalTracer(); tracer != nil {
span = tracer.StartSpan("SendWithTemplate", opentracing.ChildOf(pctx))
defer span.Finish()
}
}
dt := make(map[string]interface{}, 1)
dt["phone"] = to
ntf := notif.Notification{
Data: dt,
Type: "SMS",
}
ps := make([]notif.Params, len(params))
cc := 0
for k, v := range params {
ps[cc] = notif.Params{Parameter: k, ParameterValue: v}
cc++
}
if notifire, err := notif.GetNotifier(notif.SMS); err == nil {
err := notifire.SendWithTemplate(ctx, ntf, ps, template)
if err != nil {
return "ERROR IN SEND SMS", "FAILED", err
}
}
return message, status, err
}
func (b *basicNotificationService) Email(ctx context.Context, to string, body string, data interface{}) (message string, status string, err error) {
var tracer opentracing.Tracer
var span opentracing.Span
if parent := opentracing.SpanFromContext(ctx); parent != nil {
pctx := parent.Context()
if tracer = opentracing.GlobalTracer(); tracer != nil {
span = tracer.StartSpan("Email", opentracing.ChildOf(pctx))
defer span.Finish()
}
}
objs := make(map[string]interface{}, len(data.([]*pb.Any))+2)
objs["to"] = to
objs["body"] = body
for _, v := range data.([]*pb.Any) {
objs[v.Key] = v.Value
}
ntf := notif.Notification{
Data: objs,
Type: "SMS",
}
if notifire, err := notif.GetNotifier(notif.EMAIL); err == nil {
err := notifire.SendWithBody(ctx, ntf, notif.Line(conf.GlobalConfigs.Notif.SMS.Send.LineNumber[0]))
if err != nil {
return "ERROR IN SEND SMS", "FAILED", err
}
}
return "SUCCESS", "OK", err
}
func (b *basicNotificationService) Verify(ctx context.Context, phone string, code string) (message string, status string, data interface{}, err error) {
var tracer opentracing.Tracer
var span opentracing.Span
if parent := opentracing.SpanFromContext(ctx); parent != nil {
pctx := parent.Context()
if tracer = opentracing.GlobalTracer(); tracer != nil {
span = tracer.StartSpan("verify", opentracing.ChildOf(pctx))
defer span.Finish()
}
}
if err := redis.Database.Get(context.Background(), phone+"_"+code, &data); err != nil {
return "FAILD", "ERROR", data, err
}
redis.Database.Del(context.Background(), phone+"_"+code)
message = "OK"
status = "OK"
return message, status, data, err
}
// NewBasicNotificationService returns a naive, stateless implementation of NotificationService.
func NewBasicNotificationService() NotificationService {
return &basicNotificationService{}
}
// New returns a NotificationService with all of the expected middleware wired in.
func New(middleware []Middleware) NotificationService {
var svc NotificationService = NewBasicNotificationService()
for _, m := range middleware {
svc = m(svc)
}
return svc
}