Skip to content

Commit

Permalink
feat: add subscriber credential update sdk method
Browse files Browse the repository at this point in the history
  • Loading branch information
mahendraHegde committed Sep 26, 2023
1 parent 29a0e27 commit 55cc1f2
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type ChannelType string
type GeneralError error
type ProviderIdType string

const Version = "v1"

Expand All @@ -23,6 +24,18 @@ const (
DIRECT ChannelType = "DIRECT"
)

const (
slack ProviderIdType = "slack"
discord ProviderIdType = "discord"
msteams ProviderIdType = "msteams"
mattermost ProviderIdType = "mattermost"
fcm ProviderIdType = "fcm"
apns ProviderIdType = "apns"
expo ProviderIdType = "expo"
oneSignal ProviderIdType = "one-signal"
pushWebhook ProviderIdType = "push-webhook"
)

type Data struct {
Acknowledged bool `json:"acknowledged"`
Status string `json:"status"`
Expand Down Expand Up @@ -231,6 +244,18 @@ type SubscriberUnseenCountResponse struct {
} `json:"data"`
}

type Credentials struct {
WebhookUrl string `json:"webhookUrl,omitempty"`
Channel string `json:"channel,omitempty"`
DeviceTokens []string `json:"deviceTokens,omitempty"`
}

type SubscriberCredentialPayload struct {
Credentials Credentials `json:"credentials"`
IntegrationIdentifier string `json:"integrationIdentifier,omitempty"`
ProviderId ProviderIdType `json:"providerId"`
}

type CTA struct {
Type string `json:"type"`
Action struct {
Expand Down
20 changes: 20 additions & 0 deletions lib/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ISubscribers interface {
Identify(ctx context.Context, subscriberID string, data interface{}) (SubscriberResponse, error)
Get(ctx context.Context, subscriberID string) (SubscriberResponse, error)
Update(ctx context.Context, subscriberID string, data interface{}) (SubscriberResponse, error)
UpdateCredentials(ctx context.Context, subscriberID string, payload SubscriberCredentialPayload) (SubscriberResponse, error)
Delete(ctx context.Context, subscriberID string) (SubscriberResponse, error)
GetNotificationFeed(ctx context.Context, subscriberID string, opts *SubscriberNotificationFeedOptions) (*SubscriberNotificationFeedResponse, error)
GetUnseenCount(ctx context.Context, subscriberID string, opts *SubscriberUnseenCountOptions) (*SubscriberUnseenCountResponse, error)
Expand Down Expand Up @@ -85,6 +86,25 @@ func (s *SubscriberService) Update(ctx context.Context, subscriberID string, dat
return resp, nil
}

func (s *SubscriberService) UpdateCredentials(ctx context.Context, subscriberID string, data SubscriberCredentialPayload) (SubscriberResponse, error) {
var resp SubscriberResponse
URL := s.client.config.BackendURL.JoinPath("subscribers", subscriberID, "credentials")

jsonBody, _ := json.Marshal(data)

req, err := http.NewRequestWithContext(ctx, http.MethodPut, URL.String(), bytes.NewBuffer(jsonBody))
if err != nil {
return resp, err
}

_, err = s.client.sendRequest(req, &resp)
if err != nil {
return resp, err
}

return resp, nil
}

func (s *SubscriberService) Delete(ctx context.Context, subscriberID string) (SubscriberResponse, error) {
var resp SubscriberResponse
URL := s.client.config.BackendURL.JoinPath("subscribers", subscriberID)
Expand Down
55 changes: 55 additions & 0 deletions lib/subscribers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,61 @@ func TestSubscriberService_Update_Success(t *testing.T) {
})
}

func TestSubscriberService_Update_Credentials_Success(t *testing.T) {
var (
updateSubscriberCreds lib.SubscriberCredentialPayload
receivedBody lib.SubscriberCredentialPayload
expectedRequest lib.SubscriberCredentialPayload
expectedResponse lib.SubscriberResponse
)

subscriberService := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if err := json.NewDecoder(req.Body).Decode(&receivedBody); err != nil {
log.Printf("error in unmarshalling %+v", err)
w.WriteHeader(http.StatusBadRequest)
return
}

t.Run("Header must contain ApiKey", func(t *testing.T) {
authKey := req.Header.Get("Authorization")
assert.True(t, strings.Contains(authKey, novuApiKey))
assert.True(t, strings.HasPrefix(authKey, "ApiKey"))
})

t.Run("URL and request method is as expected", func(t *testing.T) {
expectedURL := "/v1/subscribers/" + subscriberID + "/credentials"
assert.Equal(t, http.MethodPut, req.Method)
assert.Equal(t, expectedURL, req.RequestURI)
})

t.Run("Request is as expected", func(t *testing.T) {
fileToStruct(filepath.Join("../testdata", "update_subscriber_credentials.json"), &expectedRequest)
assert.Equal(t, expectedRequest, receivedBody)
})

var resp lib.SubscriberResponse
fileToStruct(filepath.Join("../testdata", "subscriber_response.json"), &resp)

w.WriteHeader(http.StatusOK)
bb, _ := json.Marshal(resp)
w.Write(bb)
}))

ctx := context.Background()
fileToStruct(filepath.Join("../testdata", "update_subscriber_credentials.json"), &updateSubscriberCreds)

c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(subscriberService.URL)})

resp, err := c.SubscriberApi.UpdateCredentials(ctx, subscriberID, updateSubscriberCreds)
require.Nil(t, err)
assert.NotNil(t, resp)

t.Run("Response is as expected", func(t *testing.T) {
fileToStruct(filepath.Join("../testdata", "subscriber_response.json"), &expectedResponse)
assert.Equal(t, expectedResponse, resp)
})
}

func TestSubscriberService_Delete_Success(t *testing.T) {
var expectedResponse lib.SubscriberResponse

Expand Down
11 changes: 11 additions & 0 deletions testdata/update_subscriber_credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"credentials": {
"webhookUrl": "webhook",
"channel": "channel",
"deviceTokens": [
"token1"
]
},
"providerId": "slack",
"integrationIdentifier": "123"
}

0 comments on commit 55cc1f2

Please sign in to comment.