|
| 1 | +package services |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "firebase.google.com/go/auth" |
| 11 | + "github.com/NdoleStudio/httpsms/pkg/entities" |
| 12 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 13 | + "github.com/davecgh/go-spew/spew" |
| 14 | + "github.com/palantir/stacktrace" |
| 15 | + "github.com/sendgrid/sendgrid-go" |
| 16 | +) |
| 17 | + |
| 18 | +// MarketingService is handles marketing requests |
| 19 | +type MarketingService struct { |
| 20 | + logger telemetry.Logger |
| 21 | + tracer telemetry.Tracer |
| 22 | + authClient *auth.Client |
| 23 | + sendgridAPIKey string |
| 24 | + sendgridListID string |
| 25 | +} |
| 26 | + |
| 27 | +type sendgridContact struct { |
| 28 | + FirstName string `json:"first_name"` |
| 29 | + LastName string `json:"last_name"` |
| 30 | + Email string `json:"email"` |
| 31 | +} |
| 32 | + |
| 33 | +type sendgridContactRequest struct { |
| 34 | + ListIDs []string `json:"list_ids"` |
| 35 | + Contacts []sendgridContact `json:"contacts"` |
| 36 | +} |
| 37 | + |
| 38 | +// NewMarketingService creates a new instance of the MarketingService |
| 39 | +func NewMarketingService( |
| 40 | + logger telemetry.Logger, |
| 41 | + tracer telemetry.Tracer, |
| 42 | + authClient *auth.Client, |
| 43 | + sendgridAPIKey string, |
| 44 | + sendgridListID string, |
| 45 | +) *MarketingService { |
| 46 | + return &MarketingService{ |
| 47 | + logger: logger.WithService(fmt.Sprintf("%T", &MarketingService{})), |
| 48 | + tracer: tracer, |
| 49 | + authClient: authClient, |
| 50 | + sendgridAPIKey: sendgridAPIKey, |
| 51 | + sendgridListID: sendgridListID, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// AddToList adds a new user on the onboarding automation. |
| 56 | +func (service *MarketingService) AddToList(ctx context.Context, user *entities.User) { |
| 57 | + ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) |
| 58 | + defer span.End() |
| 59 | + |
| 60 | + userRecord, err := service.authClient.GetUser(ctx, string(user.ID)) |
| 61 | + if err != nil { |
| 62 | + msg := fmt.Sprintf("cannot get auth user with id [%s]", user.ID) |
| 63 | + ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + id, err := service.addContact(sendgridContactRequest{ |
| 68 | + ListIDs: []string{service.sendgridListID}, |
| 69 | + Contacts: []sendgridContact{service.toSendgridContact(userRecord)}, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + msg := fmt.Sprintf("cannot add user with id [%s] to list [%s]", user.ID, service.sendgridListID) |
| 73 | + ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + ctxLogger.Info(fmt.Sprintf("user [%s] added to list [%s] with job [%s]", user.ID, service.sendgridListID, id)) |
| 78 | +} |
| 79 | + |
| 80 | +func (service *MarketingService) toSendgridContact(user *auth.UserRecord) sendgridContact { |
| 81 | + name := strings.TrimSpace(user.DisplayName) |
| 82 | + if name == "" { |
| 83 | + return sendgridContact{ |
| 84 | + FirstName: "", |
| 85 | + LastName: "", |
| 86 | + Email: user.Email, |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + parts := strings.Split(name, " ") |
| 91 | + if len(parts) == 1 { |
| 92 | + return sendgridContact{ |
| 93 | + FirstName: name, |
| 94 | + LastName: "", |
| 95 | + Email: user.Email, |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return sendgridContact{ |
| 100 | + FirstName: strings.Join(parts[0:len(parts)-1], " "), |
| 101 | + LastName: parts[len(parts)-1], |
| 102 | + Email: user.Email, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (service *MarketingService) addContact(contactRequest sendgridContactRequest) (string, error) { |
| 107 | + request := sendgrid.GetRequest(service.sendgridAPIKey, "/v3/marketing/contacts", "https://api.sendgrid.com") |
| 108 | + request.Method = "PUT" |
| 109 | + |
| 110 | + body, err := json.Marshal(contactRequest) |
| 111 | + if err != nil { |
| 112 | + log.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot marshal [%s]", spew.Sdump(contactRequest)))) |
| 113 | + } |
| 114 | + |
| 115 | + request.Body = body |
| 116 | + response, err := sendgrid.API(request) |
| 117 | + if err != nil { |
| 118 | + return "", stacktrace.Propagate(err, fmt.Sprintf("cannot add contact to sendgrid list [%s]", spew.Sdump(contactRequest))) |
| 119 | + } |
| 120 | + return response.Body, nil |
| 121 | +} |
0 commit comments