From b6841ed2a397ea60e3ebabc22f2f841ff2e80739 Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Wed, 29 Nov 2023 23:33:49 +0200 Subject: [PATCH] Add code to remove contacts from sendgrid list --- api/cmd/experiments/main.go | 32 ++++++++++++++++++++++++-- api/pkg/services/marketting_service.go | 13 +++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/api/cmd/experiments/main.go b/api/cmd/experiments/main.go index a2b8b730..7ab7be23 100644 --- a/api/cmd/experiments/main.go +++ b/api/cmd/experiments/main.go @@ -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" @@ -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) + } } } diff --git a/api/pkg/services/marketting_service.go b/api/pkg/services/marketting_service.go index 49b52bd9..b6513dec 100644 --- a/api/pkg/services/marketting_service.go +++ b/api/pkg/services/marketting_service.go @@ -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))