diff --git a/organization_invitation.go b/organization_invitation.go new file mode 100644 index 00000000..c1ccafb5 --- /dev/null +++ b/organization_invitation.go @@ -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"` +} diff --git a/organizationinvitation/api.go b/organizationinvitation/api.go new file mode 100644 index 00000000..95cf996c --- /dev/null +++ b/organizationinvitation/api.go @@ -0,0 +1,21 @@ +// Code generated by "gen"; DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. +// Last generated at 2024-02-08 07:40:39.951639957 +0000 UTC +package organizationinvitation + +import ( + "context" + + "github.com/clerk/clerk-sdk-go/v2" +) + +// Create creates and sends an invitation to join an organization. +func Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationInvitation, error) { + return getClient().Create(ctx, params) +} + +func getClient() *Client { + return &Client{ + Backend: clerk.GetBackend(), + } +} diff --git a/organizationinvitation/client.go b/organizationinvitation/client.go new file mode 100644 index 00000000..0a7ae166 --- /dev/null +++ b/organizationinvitation/client.go @@ -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 +} diff --git a/organizationinvitation/client_test.go b/organizationinvitation/client_test.go new file mode 100644 index 00000000..cc5b1441 --- /dev/null +++ b/organizationinvitation/client_test.go @@ -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 := "foo@bar.com" + 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) +}