Skip to content

Commit

Permalink
feat: Organization Invitations API (#227)
Browse files Browse the repository at this point in the history
Added support for the Create operation of the Organization Invitations
API.
  • Loading branch information
gkats authored Feb 8, 2024
1 parent d7d5a50 commit b851ad1
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
17 changes: 17 additions & 0 deletions organization_invitation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clerk

import "encoding/json"

type OrganizationInvitation struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
EmailAddress string `json:"email_address"`
Role string `json:"role"`
OrganizationID string `json:"organization_id"`
Status string `json:"status"`
PublicMetadata json.RawMessage `json:"public_metadata"`
PrivateMetadata json.RawMessage `json:"private_metadata"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
21 changes: 21 additions & 0 deletions organizationinvitation/api.go

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

53 changes: 53 additions & 0 deletions organizationinvitation/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Package organizationinvitation provides the Organization Invitations API.
package organizationinvitation

import (
"context"
"encoding/json"
"net/http"

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

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

const path = "/organizations"

// Client is used to invoke the Organization Invitations API.
type Client struct {
Backend clerk.Backend
}

type ClientConfig struct {
clerk.BackendConfig
}

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

type CreateParams struct {
clerk.APIParams
EmailAddress *string `json:"email_address,omitempty"`
Role *string `json:"role,omitempty"`
RedirectURL *string `json:"redirect_url,omitempty"`
InviterUserID *string `json:"inviter_user_id,omitempty"`
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata *json.RawMessage `json:"private_metadata,omitempty"`
OrganizationID string `json:"-"`
}

// Create creates and sends an invitation to join an organization.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationInvitation, error) {
path, err := clerk.JoinPath(path, params.OrganizationID, "/invitations")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
invitation := &clerk.OrganizationInvitation{}
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}
64 changes: 64 additions & 0 deletions organizationinvitation/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package organizationinvitation

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

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

func TestOrganizationInvitationClientCreate(t *testing.T) {
t.Parallel()
id := "orginv_123"
organizationID := "org_123"
emailAddress := "[email protected]"
config := &ClientConfig{}
config.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","organization_id":"%s"}`, id, emailAddress, organizationID)),
Method: http.MethodPost,
Path: "/v1/organizations/" + organizationID + "/invitations",
},
}
client := NewClient(config)
invitation, err := client.Create(context.Background(), &CreateParams{
OrganizationID: organizationID,
EmailAddress: clerk.String(emailAddress),
})
require.NoError(t, err)
require.Equal(t, id, invitation.ID)
require.Equal(t, organizationID, invitation.OrganizationID)
require.Equal(t, emailAddress, invitation.EmailAddress)
}

func TestOrganizationInvitationClientCreate_Error(t *testing.T) {
t.Parallel()
config := &ClientConfig{}
config.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"
}`),
},
}
client := NewClient(config)
_, err := client.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)
}

0 comments on commit b851ad1

Please sign in to comment.