Skip to content

Commit a19fb0d

Browse files
committed
Fix the marketting service
1 parent 71a3378 commit a19fb0d

File tree

4 files changed

+30
-51
lines changed

4 files changed

+30
-51
lines changed

api/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ require (
1818
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1919
github.com/dgraph-io/ristretto v1.0.0
2020
github.com/dustin/go-humanize v1.0.1
21-
github.com/getbrevo/brevo-go v1.1.3
2221
github.com/gofiber/contrib/otelfiber v1.0.10
2322
github.com/gofiber/fiber/v2 v2.52.8
2423
github.com/gofiber/swagger v1.1.1
@@ -83,7 +82,6 @@ require (
8382
github.com/PuerkitoBio/goquery v1.10.3 // indirect
8483
github.com/andybalholm/brotli v1.2.0 // indirect
8584
github.com/andybalholm/cascadia v1.3.3 // indirect
86-
github.com/antihax/optional v1.0.0 // indirect
8785
github.com/cenkalti/backoff/v5 v5.0.2 // indirect
8886
github.com/cespare/xxhash/v2 v2.3.0 // indirect
8987
github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect

api/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwTo
6464
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
6565
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
6666
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
67-
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
68-
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
6967
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
7068
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
7169
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
@@ -109,8 +107,6 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
109107
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
110108
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
111109
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
112-
github.com/getbrevo/brevo-go v1.1.3 h1:8TYrhhxbfAJLGArlPzCDKzbNfzvjIykBRhTDzLJqmyw=
113-
github.com/getbrevo/brevo-go v1.1.3/go.mod h1:ExhytIoPxt/cOBl6ZEMeEZNLUKrWEYA5U3hM/8WP2bg=
114110
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
115111
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
116112
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=

api/pkg/di/container.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"strconv"
1010
"time"
1111

12-
brevo "github.com/getbrevo/brevo-go/lib"
13-
1412
"github.com/pusher/pusher-http-go/v5"
1513

1614
"github.com/NdoleStudio/httpsms/docs"
@@ -427,21 +425,6 @@ func (container *Container) FirebaseAuthClient() (client *auth.Client) {
427425
return authClient
428426
}
429427

430-
// BrevoClient creates a new instance of brevo.APIClient
431-
func (container *Container) BrevoClient() (client *brevo.APIClient) {
432-
container.logger.Debug(fmt.Sprintf("creating %T", client))
433-
434-
apiKey := os.Getenv("BREVO_API_KEY")
435-
if apiKey == "" {
436-
container.logger.Info("BREVO_API_KEY environment variable is not set")
437-
return nil
438-
}
439-
440-
cfg := brevo.NewConfiguration()
441-
cfg.AddDefaultHeader("api-key", apiKey)
442-
return brevo.NewAPIClient(cfg)
443-
}
444-
445428
// CloudTasksClient creates a new instance of cloudtasks.Client
446429
func (container *Container) CloudTasksClient() (client *cloudtasks.Client) {
447430
container.logger.Debug(fmt.Sprintf("creating %T", client))
@@ -919,7 +902,7 @@ func (container *Container) MarketingService() (service *services.MarketingServi
919902
container.Logger(),
920903
container.Tracer(),
921904
container.FirebaseAuthClient(),
922-
container.BrevoClient(),
905+
os.Getenv("BREVO_API_KEY"),
923906
)
924907
}
925908

api/pkg/services/marketting_service.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/carlmjohnson/requests"
9+
810
"firebase.google.com/go/auth"
911
"github.com/NdoleStudio/httpsms/pkg/entities"
1012
"github.com/NdoleStudio/httpsms/pkg/telemetry"
11-
brevo "github.com/getbrevo/brevo-go/lib"
1213
"github.com/gofiber/fiber/v2"
1314
"github.com/palantir/stacktrace"
1415
)
@@ -18,21 +19,21 @@ type MarketingService struct {
1819
logger telemetry.Logger
1920
tracer telemetry.Tracer
2021
authClient *auth.Client
21-
brevoClient *brevo.APIClient
22+
brevoAPIKey string
2223
}
2324

2425
// NewMarketingService creates a new instance of the MarketingService
2526
func NewMarketingService(
2627
logger telemetry.Logger,
2728
tracer telemetry.Tracer,
2829
authClient *auth.Client,
29-
brevoClient *brevo.APIClient,
30+
brevoAPIKey string,
3031
) *MarketingService {
3132
return &MarketingService{
3233
logger: logger.WithService(fmt.Sprintf("%T", &MarketingService{})),
3334
tracer: tracer,
3435
authClient: authClient,
35-
brevoClient: brevoClient,
36+
brevoAPIKey: brevoAPIKey,
3637
}
3738
}
3839

@@ -41,17 +42,16 @@ func (service *MarketingService) DeleteUser(ctx context.Context, userID entities
4142
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
4243
defer span.End()
4344

44-
if service.brevoClient == nil {
45-
ctxLogger.Warn(stacktrace.NewError("brevo client is not initialized, skipping adding user to list"))
46-
return
47-
}
48-
49-
response, err := service.brevoClient.ContactsApi.DeleteContact(ctx, userID.String())
45+
err := requests.URL(fmt.Sprintf("https://api.brevo.com/v3/contacts/%s?identifierType=ext_id", userID)).
46+
Header("api-key", service.brevoAPIKey).
47+
Delete().
48+
CheckStatus(fiber.StatusNoContent).
49+
Fetch(ctx)
5050
if err != nil {
5151
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, fmt.Sprintf("cannot delete user with id [%s] from brevo list", userID)))
5252
}
5353

54-
ctxLogger.Info(fmt.Sprintf("deleted user with ID [%s] from brevo list with status [%s]", userID, response.Status))
54+
ctxLogger.Info(fmt.Sprintf("deleted user with ID [%s] from brevo list with status [%s]", userID, fiber.StatusNoContent))
5555
return nil
5656
}
5757

@@ -60,32 +60,34 @@ func (service *MarketingService) AddToList(ctx context.Context, user *entities.U
6060
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
6161
defer span.End()
6262

63-
if service.brevoClient == nil {
64-
ctxLogger.Warn(stacktrace.NewError("brevo client is not initialized, skipping adding user to list"))
65-
return
66-
}
67-
6863
userRecord, err := service.authClient.GetUser(ctx, string(user.ID))
6964
if err != nil {
7065
msg := fmt.Sprintf("cannot get auth user with id [%s]", user.ID)
7166
ctxLogger.Error(stacktrace.Propagate(err, msg))
7267
return
7368
}
7469

75-
contact, _, err := service.brevoClient.ContactsApi.CreateContact(ctx, brevo.CreateContact{
76-
Email: userRecord.Email,
77-
Attributes: service.brevoAttributes(userRecord),
78-
ExtId: userRecord.UID,
79-
ListIds: []int64{9},
80-
UpdateEnabled: true,
81-
})
70+
var response string
71+
err = requests.URL("https://api.brevo.com/v3/contacts").
72+
Header("api-key", service.brevoAPIKey).
73+
Post().
74+
BodyJSON(fiber.Map{
75+
"email": userRecord.Email,
76+
"ext_id": userRecord.UID,
77+
"attributes": service.brevoAttributes(userRecord),
78+
"listIds": []int64{9},
79+
"updateEnabled": true,
80+
}).
81+
CheckStatus(fiber.StatusCreated, fiber.StatusNoContent).
82+
ToString(&response).
83+
Fetch(ctx)
8284
if err != nil {
8385
msg := fmt.Sprintf("cannot add user with id [%s] to brevo list", user.ID)
8486
ctxLogger.Error(stacktrace.Propagate(err, msg))
8587
return
8688
}
8789

88-
ctxLogger.Info(fmt.Sprintf("user [%s] added to list brevo list with brevo ID [%d]", user.ID, contact.Id))
90+
ctxLogger.Info(fmt.Sprintf("user [%s] added to list brevo list with brevo response [%s]", user.ID, response))
8991
}
9092

9193
func (service *MarketingService) brevoAttributes(user *auth.UserRecord) map[string]any {
@@ -96,11 +98,11 @@ func (service *MarketingService) brevoAttributes(user *auth.UserRecord) map[stri
9698

9799
parts := strings.Split(name, " ")
98100
if len(parts) == 1 {
99-
return fiber.Map{"FNAME": name}
101+
return fiber.Map{"FIRSTNAME": name}
100102
}
101103

102104
return fiber.Map{
103-
"FNAME": strings.Join(parts[0:len(parts)-1], " "),
104-
"LNAME": parts[len(parts)-1],
105+
"FIRSTNAME": strings.Join(parts[0:len(parts)-1], " "),
106+
"LASTNAME": parts[len(parts)-1],
105107
}
106108
}

0 commit comments

Comments
 (0)