Skip to content

Commit

Permalink
feat: Update contact by email
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Jan 19, 2025
1 parent 2f16f04 commit 3c9d1d7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 5 deletions.
14 changes: 11 additions & 3 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CreateContactRequest struct {

type UpdateContactRequest struct {
Id string `json:"id"`
Email string `json:"email,omitempty"`
AudienceId string `json:"audience_id"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Expand Down Expand Up @@ -201,11 +202,18 @@ func (s *ContactsSvcImpl) UpdateWithContext(ctx context.Context, params *UpdateC
return UpdateContactResponse{}, errors.New("[ERROR]: AudienceId is missing")
}

if params.Id == "" {
return UpdateContactResponse{}, errors.New("[ERROR]: Id is missing")
if params.Id == "" && params.Email == "" {
return UpdateContactResponse{}, &MissingRequiredFieldsError{message: "[ERROR]: Missing `id` or `email` field."}
}

path := "audiences/" + params.AudienceId + "/contacts/" + params.Id
var val string
if params.Id != "" {
val = params.Id
} else {
val = params.Email
}

path := "audiences/" + params.AudienceId + "/contacts/" + val

// Prepare request
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, params)
Expand Down
100 changes: 99 additions & 1 deletion contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestGetContact(t *testing.T) {
assert.Equal(t, contact.Unsubscribed, false)
}

func TestUpdateContact(t *testing.T) {
func TestUpdateContactById(t *testing.T) {
setup()
defer teardown()

Expand Down Expand Up @@ -187,3 +187,101 @@ func TestUpdateContact(t *testing.T) {
assert.Equal(t, resp.Data.Id, "479e3145-dd38-476b-932c-529ceb705947")
assert.Equal(t, resp.Error, struct{}{})
}

func TestUpdateContactByEmail(t *testing.T) {
setup()
defer teardown()

audienceId := "709d076c-2bb1-4be6-94ed-3f8f32622db6"
email := "[email protected]"

mux.HandleFunc("/audiences/"+audienceId+"/contacts/"+email, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPatch)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)

var ret interface{}
ret = `
{
"data": {
"id": "479e3145-dd38-476b-932c-529ceb705947"
},
"error": null
}`

fmt.Fprint(w, ret)
})

req := &UpdateContactRequest{
AudienceId: audienceId,
Email: email,
FirstName: "Updated First Name",
}
resp, err := client.Contacts.Update(req)
if err != nil {
t.Errorf("Contacts.Update returned error: %v", err)
}
assert.NotNil(t, resp.Data)
assert.Equal(t, resp.Data.Id, "479e3145-dd38-476b-932c-529ceb705947")
assert.Equal(t, resp.Error, struct{}{})
}

func TestUpdateContactIdMissing(t *testing.T) {
setup()
defer teardown()

audienceId := "709d076c-2bb1-4be6-94ed-3f8f32622db6"
id := ""

params := &UpdateContactRequest{
AudienceId: audienceId,
Id: id,
FirstName: "Updated First Name",
}
resp, err := client.Contacts.Update(params)

var missingRequiredFieldsErr *MissingRequiredFieldsError

assert.Equal(t, resp, UpdateContactResponse{})
assert.ErrorAsf(t, err, &missingRequiredFieldsErr, "expected error to be of type %T, got %T", &missingRequiredFieldsErr, err)
assert.Containsf(t, err.Error(), "Missing `id` or `email` field.", "expected error containing %q, got %s", "Missing `id` or `email` field.", err)
}

func TestUpdateContactEmailMissing(t *testing.T) {
setup()
defer teardown()

audienceId := "709d076c-2bb1-4be6-94ed-3f8f32622db6"

params := &UpdateContactRequest{
AudienceId: audienceId,
Email: "",
FirstName: "Updated First Name",
}
resp, err := client.Contacts.Update(params)

var missingRequiredFieldsErr *MissingRequiredFieldsError

assert.Equal(t, resp, UpdateContactResponse{})
assert.ErrorAsf(t, err, &missingRequiredFieldsErr, "expected error to be of type %T, got %T", &missingRequiredFieldsErr, err)
assert.Containsf(t, err.Error(), "Missing `id` or `email` field.", "expected error containing %q, got %s", "Missing `id` or `email` field.", err)
}

func TestUpdateContactAudienceIdMissing(t *testing.T) {
setup()
defer teardown()

audienceId := ""
id := "123123123"

params := &UpdateContactRequest{
AudienceId: audienceId,
Id: id,
FirstName: "Updated First Name",
}
resp, err := client.Contacts.Update(params)

assert.NotNil(t, err)
assert.Equal(t, resp, UpdateContactResponse{})
assert.Containsf(t, err.Error(), "[ERROR]: AudienceId is missing", "expected error containing %q, got %s", "[ERROR]: AudienceId is missing", err)
}
14 changes: 13 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package resend

import "errors"
import (
"errors"
"fmt"
)

// MissingRequiredFieldsError is used when a required field is missing before making an API request
type MissingRequiredFieldsError struct {
message string
}

func (e *MissingRequiredFieldsError) Error() string {
return fmt.Sprintf("%s", e.message)
}

// BroadcastsSvc errors
var (
Expand Down

0 comments on commit 3c9d1d7

Please sign in to comment.