Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for waitlist entries endpoints #360

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add support for the OAuth Applications API. Added the oauthapplication package for API operations and a clerk.OAuthApplication type.
- Add support for multiple invitation templates with the `TemplateSlug` field in `invitation.Create`.
- Add support for listing and creating waitlist entries with the `waitlistentry.List` and `waitlistentry.Create` methods.

## 2.2.0

Expand Down
18 changes: 18 additions & 0 deletions waitlist_entries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package clerk

type WaitlistEntry struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
EmailAddress string `json:"email_address"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Invitation *Invitation `json:"invitation"`
}

type WaitlistEntriesList struct {
APIResource
WaitlistEntries []*WaitlistEntry `json:"data"`
TotalCount int64 `json:"total_count"`
}
25 changes: 25 additions & 0 deletions waitlistentry/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions waitlistentry/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package waitlistentry

import (
"context"
"net/http"
"net/url"

"github.com/clerk/clerk-sdk-go/v2"
)

//go:generate go run ../cmd/gen/main.go

const path = "/waitlist_entries"

// Client is used to invoke the Waitlist Entries API.
type Client struct {
Backend clerk.Backend
}

func NewClient(config *clerk.ClientConfig) *Client {
return &Client{
Backend: clerk.NewBackend(&config.BackendConfig),
}
}

type ListParams struct {
clerk.APIParams
clerk.ListParams
OrderBy *string `json:"order_by,omitempty"`
Query *string `json:"query,omitempty"`
Statuses []string `json:"status,omitempty"`
}

// ToQuery returns query string values from the params.
func (params *ListParams) ToQuery() url.Values {
q := params.ListParams.ToQuery()
if params.OrderBy != nil {
q.Set("order_by", *params.OrderBy)
}
if params.Query != nil {
q.Set("query", *params.Query)
}
for _, status := range params.Statuses {
q.Add("status", status)
}
return q
}

// List returns all waitlist entries.
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.WaitlistEntriesList, error) {
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
list := &clerk.WaitlistEntriesList{}
err := c.Backend.Call(ctx, req, list)
return list, err
}

type CreateParams struct {
clerk.APIParams
EmailAddress string `json:"email_address"`
}

// Create adds a new waitlist entry.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.WaitlistEntry, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
invitation := &clerk.WaitlistEntry{}
err := c.Backend.Call(ctx, req, invitation)
return invitation, err
}
139 changes: 139 additions & 0 deletions waitlistentry/waitlistentry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package waitlistentry

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"

"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/clerktest"
"github.com/stretchr/testify/require"
)

func TestWaitlistList(t *testing.T) {
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{
"data": [{"id":"wle_123","email_address":"[email protected]"}],
"total_count": 1
}`),
Path: "/v1/waitlist_entries",
Method: http.MethodGet,
},
},
}))

list, err := List(context.Background(), &ListParams{})
require.NoError(t, err)
require.Equal(t, int64(1), list.TotalCount)
require.Equal(t, 1, len(list.WaitlistEntries))
require.Equal(t, "wle_123", list.WaitlistEntries[0].ID)
require.Equal(t, "[email protected]", list.WaitlistEntries[0].EmailAddress)
}

func TestWaitlistEntriesListWithParams(t *testing.T) {
limit := int64(10)
offset := int64(20)
orderBy := "-created_at"
query := "[email protected]"
status1 := "pending"
status2 := "invited"

clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{
"data": [
{"id":"wle_123","email_address":"[email protected]"},
{"id":"wle_124","email_address":"[email protected]","invitation":{"id":"inv_124","email_address":"[email protected]"}}
],
"total_count": 2
}`),
Path: "/v1/waitlist_entries",
Method: http.MethodGet,
Query: &url.Values{
"limit": []string{fmt.Sprintf("%d", limit)},
"offset": []string{fmt.Sprintf("%d", offset)},
"order_by": []string{orderBy},
"query": []string{query},
"status": []string{status1, status2},
},
},
},
}))

list, err := List(context.Background(), &ListParams{
ListParams: clerk.ListParams{
Limit: &limit,
Offset: &offset,
},
OrderBy: &orderBy,
Query: &query,
Statuses: []string{status1, status2},
})
require.NoError(t, err)
require.Equal(t, int64(2), list.TotalCount)
require.Len(t, list.WaitlistEntries, 2)
require.Equal(t, "wle_123", list.WaitlistEntries[0].ID)
require.Equal(t, "[email protected]", list.WaitlistEntries[0].EmailAddress)
require.Nil(t, list.WaitlistEntries[0].Invitation)
require.Equal(t, "wle_124", list.WaitlistEntries[1].ID)
require.Equal(t, "[email protected]", list.WaitlistEntries[1].EmailAddress)
require.NotNil(t, list.WaitlistEntries[1].Invitation)
require.Equal(t, "inv_124", list.WaitlistEntries[1].Invitation.ID)
require.Equal(t, "[email protected]", list.WaitlistEntries[1].Invitation.EmailAddress)
}

func TestWaitlistEntryCreate(t *testing.T) {
emailAddress := "[email protected]"
id := "inv_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`{"email_address":"%s"}`, emailAddress)),
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","email_address":"%s"}`, id, emailAddress)),
Method: http.MethodPost,
Path: "/v1/waitlist_entries",
},
},
}))

entry, err := Create(context.Background(), &CreateParams{
EmailAddress: emailAddress,
})
require.NoError(t, err)
require.Equal(t, id, entry.ID)
require.Equal(t, emailAddress, entry.EmailAddress)
}

func TestWaitlistEntryCreate_Error(t *testing.T) {
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Status: http.StatusBadRequest,
Out: json.RawMessage(`{
"errors":[{
"code":"create-error-code"
}],
"clerk_trace_id":"create-trace-id"
}`),
},
},
}))

_, err := Create(context.Background(), &CreateParams{})
require.Error(t, err)
apiErr, ok := err.(*clerk.APIErrorResponse)
require.True(t, ok)
require.Equal(t, "create-trace-id", apiErr.TraceID)
require.Equal(t, 1, len(apiErr.Errors))
require.Equal(t, "create-error-code", apiErr.Errors[0].Code)
}
Loading