Skip to content

Commit

Permalink
feat: Add support for waitlist entries endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaspt committed Nov 29, 2024
1 parent 8c51f93 commit 2f9459e
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 0 deletions.
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
}
114 changes: 114 additions & 0 deletions waitlistentry/waitlistentry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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.Equal(t, 2, len(list.WaitlistEntries))
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)
}

0 comments on commit 2f9459e

Please sign in to comment.