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