Skip to content

Commit

Permalink
Add code to remove contacts from sendgrid list
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Nov 29, 2023
1 parent 785ceca commit b6841ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
32 changes: 30 additions & 2 deletions api/cmd/experiments/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"fmt"
"log"
"os"
"strings"
"sync"
"time"

"github.com/palantir/stacktrace"

"github.com/NdoleStudio/httpsms/pkg/di"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/google/uuid"
Expand All @@ -27,11 +30,36 @@ func main() {
logger := container.Logger()

logger.Info("Starting experiments")
}

func chunkBy[T any](items []T, chunkSize int) (chunks [][]T) {
for chunkSize < len(items) {
items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize])
}
return append(chunks, items)
}

func deleteContacts(container *di.Container) {
sendgrid := container.MarketingService()
err = sendgrid.ClearList(context.Background())
logger := container.Logger()

b, err := os.ReadFile("28462979_cf6f5478-3e15-4666-95d7-59149df6f0fd.csv") // just pass the file name
if err != nil {
logger.Fatal(err)
logger.Fatal(stacktrace.Propagate(err, "cannot read file"))
}

lines := strings.Split(string(b), "\n")[1:]
var contacts []string
for _, line := range lines {
contacts = append(contacts, strings.ReplaceAll(strings.Split(line, ",")[17], "\"", ""))
}

chunks := chunkBy(contacts, 100)
for _, chunk := range chunks {
err = sendgrid.DeleteContacts(context.Background(), chunk)
if err != nil {
logger.Fatal(err)
}
}
}

Expand Down
13 changes: 8 additions & 5 deletions api/pkg/services/marketting_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,20 @@ func (service *MarketingService) AddToList(ctx context.Context, user *entities.U
ctxLogger.Info(fmt.Sprintf("user [%s] added to list [%s] with job [%s]", user.ID, service.sendgridListID, id))
}

// ClearList removes all new contacts from the sendgrid list.
func (service *MarketingService) ClearList(ctx context.Context) error {
// DeleteContacts deletes contacts from sendgrid
func (service *MarketingService) DeleteContacts(ctx context.Context, contactIDs []string) error {
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
defer span.End()

request := sendgrid.GetRequest(service.sendgridAPIKey, fmt.Sprintf("/v3/contactdb/lists/%s/recipients", service.sendgridListID), "https://api.sendgrid.com")
request.Method = "GET"
request := sendgrid.GetRequest(service.sendgridAPIKey, "/v3/marketing/contacts", "https://api.sendgrid.com")
request.Method = "DELETE"
request.QueryParams = map[string]string{
"ids": strings.Join(contactIDs, ","),
}

response, err := sendgrid.API(request)
if err != nil {
return stacktrace.Propagate(err, fmt.Sprintf("cannot get all contacts in a sendgrid list [%s]", service.sendgridListID))
return stacktrace.Propagate(err, fmt.Sprintf("cannot delete contacts in a sendgrid list [%s]", service.sendgridListID))
}

ctxLogger.Info(spew.Sdump(response.Body))
Expand Down

0 comments on commit b6841ed

Please sign in to comment.