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 bulk invitation creation support #351

Merged
merged 6 commits into from
Nov 12, 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 @@ -2,6 +2,7 @@

## Next release

- Add support for bulk invitation creation with the `invitation.BulkCreate` method.
- Add `NameQuery` to `user.ListParams`.

## 2.1.1
Expand Down
5 changes: 5 additions & 0 deletions invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Invitation struct {
UpdatedAt int64 `json:"updated_at"`
}

type Invitations struct {
APIResource
Invitations []*Invitation `json:"data"`
}

type InvitationList struct {
APIResource
Invitations []*Invitation `json:"data"`
Expand Down
5 changes: 5 additions & 0 deletions invitation/api.go

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

39 changes: 39 additions & 0 deletions invitation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,45 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Invit
return invitation, err
}

type BulkCreateParams struct {
clerk.APIParams
Invitations []*CreateParams
}

func (b BulkCreateParams) MarshalJSON() ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Why do we need this custom marshaling method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the custom marshaling because the API's request for bulk is an array of regular requests, so it’s not bound to a key like {"invitations": [...]}. To be accurate in SDK, the correct type would be type BulkCreateParams []*CreateParams. However this wouldn’t be compatible with req.SetParams.

Do you have a better alternative to suggest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining! It is indeed a problem with the current implementation.

Do you have a better alternative to suggest?

Not really. 😞 As discussed offline, let's settle on the exported types for now and solidify the API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve refactored the types as we discussed a bit here. Please take another look when you get a chance.

return json.Marshal(b.Invitations)
}

type bulkCreateResponse struct {
clerk.APIResource
Invitations []*clerk.Invitation
}

func (b *bulkCreateResponse) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &b.Invitations)
}

// BulkCreate creates multiple invitations.
func (c *Client) BulkCreate(ctx context.Context, params *BulkCreateParams) (*clerk.Invitations, error) {
path, err := clerk.JoinPath(path, "bulk")
if err != nil {
return nil, err
}

req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)

res := &bulkCreateResponse{}
if err := c.Backend.Call(ctx, req, res); err != nil {
return nil, err
}

return &clerk.Invitations{
APIResource: res.APIResource,
Invitations: res.Invitations,
}, nil
}

// Revoke revokes a pending invitation.
func (c *Client) Revoke(ctx context.Context, id string) (*clerk.Invitation, error) {
path, err := clerk.JoinPath(path, id, "revoke")
Expand Down
40 changes: 40 additions & 0 deletions invitation/invitation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,46 @@ func TestInvitationCreate_Error(t *testing.T) {
require.Equal(t, "create-error-code", apiErr.Errors[0].Code)
}

func TestBulkInvitationCreate(t *testing.T) {
emailAddresses := []string{"[email protected]", "[email protected]"}
ids := []string{"inv_123", "inv_456"}
invitations := []*clerk.Invitation{
{ID: ids[0], EmailAddress: emailAddresses[0]},
{ID: ids[1], EmailAddress: emailAddresses[1]},
}

clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`[{"email_address":"%s"},{"email_address":"%s"}]`, emailAddresses[0], emailAddresses[1])),
Out: json.RawMessage(fmt.Sprintf(
`[{"id":"%s","email_address":"%s"},{"id":"%s","email_address":"%s"}]`,
ids[0], emailAddresses[0], ids[1], emailAddresses[1],
)),
Method: http.MethodPost,
Path: "/v1/invitations/bulk",
},
},
}))

params := BulkCreateParams{
Invitations: []*CreateParams{
{EmailAddress: emailAddresses[0]},
{EmailAddress: emailAddresses[1]},
},
}

response, err := BulkCreate(context.Background(), &params)
require.NoError(t, err)
require.Len(t, invitations, 2)

for i, invitation := range response.Invitations {
require.Equal(t, ids[i], invitation.ID)
require.Equal(t, emailAddresses[i], invitation.EmailAddress)
}
}

func TestInvitationRevoke(t *testing.T) {
id := "inv_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
Expand Down
Loading