Skip to content

Commit

Permalink
feat: Add bulk invitation creation support
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaspt committed Nov 12, 2024
1 parent a2346ff commit b354456
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
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.

36 changes: 36 additions & 0 deletions invitation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ 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) {
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.InvitationList, error) {
req := clerk.NewAPIRequest(http.MethodPost, fmt.Sprintf("%s/bulk", path))
req.SetParams(params)

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

return &clerk.InvitationList{
APIResource: res.APIResource,
Invitations: res.Invitations,
TotalCount: int64(len(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]},
},
}

invitationList, err := BulkCreate(context.Background(), &params)
require.NoError(t, err)
require.Equal(t, int64(len(invitations)), invitationList.TotalCount)

for i, invitation := range invitationList.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

0 comments on commit b354456

Please sign in to comment.