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: Organization Invitations API #227

Merged
merged 1 commit into from
Feb 8, 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
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"`
georgepsarakis marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading