@@ -5,11 +5,12 @@ import (
55 "fmt"
66 "strings"
77
8- "github.com/carlmjohnson/requests "
8+ semconv "go.opentelemetry.io/otel/semconv/v1.10.0 "
99
1010 "firebase.google.com/go/auth"
1111 "github.com/NdoleStudio/httpsms/pkg/entities"
1212 "github.com/NdoleStudio/httpsms/pkg/telemetry"
13+ plunk "github.com/NdoleStudio/plunk-go"
1314 "github.com/gofiber/fiber/v2"
1415 "github.com/palantir/stacktrace"
1516)
@@ -19,90 +20,93 @@ type MarketingService struct {
1920 logger telemetry.Logger
2021 tracer telemetry.Tracer
2122 authClient * auth.Client
22- brevoAPIKey string
23+ plunkClient * plunk. Client
2324}
2425
2526// NewMarketingService creates a new instance of the MarketingService
2627func NewMarketingService (
2728 logger telemetry.Logger ,
2829 tracer telemetry.Tracer ,
2930 authClient * auth.Client ,
30- brevoAPIKey string ,
31+ plunkClient * plunk. Client ,
3132) * MarketingService {
3233 return & MarketingService {
3334 logger : logger .WithService (fmt .Sprintf ("%T" , & MarketingService {})),
3435 tracer : tracer ,
3536 authClient : authClient ,
36- brevoAPIKey : brevoAPIKey ,
37+ plunkClient : plunkClient ,
3738 }
3839}
3940
40- // DeleteUser a user if exists in the sendgrid list
41- func (service * MarketingService ) DeleteUser (ctx context.Context , userID entities. UserID ) error {
41+ // DeleteContact a user if exists as a contact
42+ func (service * MarketingService ) DeleteContact (ctx context.Context , email string ) error {
4243 ctx , span , ctxLogger := service .tracer .StartWithLogger (ctx , service .logger )
4344 defer span .End ()
4445
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 )
46+ response , _ , err := service .plunkClient .Contacts .List (ctx , map [string ]string {"limit" : "1" , "search" : email })
5047 if err != nil {
51- return service .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot delete user with id [%s] from brevo list " , userID )))
48+ return service .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot search for contact with email [%s]" , email )))
5249 }
5350
54- ctxLogger .Info (fmt .Sprintf ("deleted user with ID [%s] from brevo list with status [%s]" , userID , fiber .StatusNoContent ))
51+ if len (response .Contacts ) == 0 {
52+ ctxLogger .Info (fmt .Sprintf ("no contact found with email [%s], skipping deletion" , email ))
53+ return nil
54+ }
55+
56+ contact := response .Contacts [0 ]
57+ if _ , err = service .plunkClient .Contacts .Delete (ctx , contact .ID ); err != nil {
58+ return service .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , fmt .Sprintf ("cannot delete user with ID [%s] from contacts" , contact .Data [string (semconv .EnduserIDKey )])))
59+ }
60+
61+ ctxLogger .Info (fmt .Sprintf ("deleted user with ID [%s] from as marketting contact with ID [%s]" , contact .Data [string (semconv .EnduserIDKey )], contact .ID ))
5562 return nil
5663}
5764
58- // AddToList adds a new user on the onboarding automation.
59- func (service * MarketingService ) AddToList (ctx context.Context , user * entities.User ) {
65+ // CreateContact adds a new user on the onboarding automation.
66+ func (service * MarketingService ) CreateContact (ctx context.Context , userID entities.UserID ) error {
6067 ctx , span , ctxLogger := service .tracer .StartWithLogger (ctx , service .logger )
6168 defer span .End ()
6269
63- userRecord , err := service .authClient .GetUser (ctx , string ( user . ID ))
70+ userRecord , err := service .authClient .GetUser (ctx , userID . String ( ))
6471 if err != nil {
65- msg := fmt .Sprintf ("cannot get auth user with id [%s]" , user .ID )
66- ctxLogger .Error (stacktrace .Propagate (err , msg ))
67- return
72+ msg := fmt .Sprintf ("cannot get auth user with id [%s]" , userID )
73+ return service .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , msg ))
6874 }
6975
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 )
76+ data := service .attributes (userRecord )
77+ data [string (semconv .ServiceNameKey )] = "httpsms.com"
78+ data [string (semconv .EnduserIDKey )] = userRecord .UID
79+
80+ event , _ , err := service .plunkClient .Tracker .TrackEvent (ctx , & plunk.TrackEventRequest {
81+ Email : userRecord .Email ,
82+ Event : "contact.created" ,
83+ Subscribed : true ,
84+ Data : data ,
85+ })
8486 if err != nil {
85- msg := fmt .Sprintf ("cannot add user with id [%s] to brevo list" , user .ID )
86- ctxLogger .Error (stacktrace .Propagate (err , msg ))
87- return
87+ msg := fmt .Sprintf ("cannot create contact for user with id [%s]" , userID )
88+ return service .tracer .WrapErrorSpan (span , stacktrace .Propagate (err , msg ))
8889 }
8990
90- ctxLogger .Info (fmt .Sprintf ("user [%s] added to list brevo list with brevo response [%s]" , user .ID , response ))
91+ ctxLogger .Info (fmt .Sprintf ("user [%s] added to marketting list with contact ID [%s] and event ID [%s]" , userID , event .Data .Contact , event .Data .Event ))
92+ return nil
9193}
9294
93- func (service * MarketingService ) brevoAttributes (user * auth.UserRecord ) map [string ]any {
95+ func (service * MarketingService ) attributes (user * auth.UserRecord ) map [string ]any {
9496 name := strings .TrimSpace (user .DisplayName )
9597 if name == "" {
9698 return fiber.Map {}
9799 }
98100
99101 parts := strings .Split (name , " " )
100102 if len (parts ) == 1 {
101- return fiber.Map {"FIRSTNAME" : name }
103+ return fiber.Map {
104+ "firstName" : name ,
105+ }
102106 }
103107
104108 return fiber.Map {
105- "FIRSTNAME " : strings .Join (parts [0 :len (parts )- 1 ], " " ),
106- "LASTNAME " : parts [len (parts )- 1 ],
109+ "firstName " : strings .Join (parts [0 :len (parts )- 1 ], " " ),
110+ "lastName " : parts [len (parts )- 1 ],
107111 }
108112}
0 commit comments