Skip to content

Commit

Permalink
feat: add new methods for organization invitation API
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLopes7 committed Sep 30, 2024
1 parent 000038e commit 8ac9efb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
35 changes: 25 additions & 10 deletions organization_invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@ package clerk

import "encoding/json"

type publicOrganizationData struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
ImageURL string `json:"image_url,omitempty"`
HasImage bool `json:"has_image"`
}

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"`
Object string `json:"object"`
ID string `json:"id"`
EmailAddress string `json:"email_address"`
Role string `json:"role"`
OrganizationID string `json:"organization_id"`
PublicOrganizationData *publicOrganizationData `json:"public_organization_data,omitempty"`
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"`
}

type OrganizationInvitationList struct {
APIResource
OrganizationInvitations []*OrganizationInvitation `json:"data"`
TotalCount int64 `json:"total_count"`
}
15 changes: 15 additions & 0 deletions organizationinvitation/api.go

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

54 changes: 54 additions & 0 deletions organizationinvitation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"net/http"
"net/url"

"github.com/clerk/clerk-sdk-go/v2"
)
Expand Down Expand Up @@ -47,3 +48,56 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Organ
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}

type ListParams struct {
clerk.APIParams
clerk.ListParams
Statuses *[]string
}

func (p *ListParams) ToQuery() url.Values {
q := p.ListParams.ToQuery()

if p.Statuses != nil && len(*p.Statuses) > 0 {
q["status"] = *p.Statuses
}

return q
}

// List returns a list of organization invitations
func (c *Client) List(ctx context.Context, organizationID string, params *ListParams) (*clerk.OrganizationInvitation, error) {
path, err := clerk.JoinPath(path, organizationID, "/invitations")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
invitation := &clerk.OrganizationInvitation{}
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}

// Get retrieves the detail for an organization invitation.
func (c *Client) Get(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) {
path, err := clerk.JoinPath(path, organizationID, "/invitations", id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
invitation := &clerk.OrganizationInvitation{}
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}

// Revoke marks the organization invitation as revoked.
func (c *Client) Revoke(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) {
path, err := clerk.JoinPath(path, organizationID, "/invitations", id, "/revoke")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
invitation := &clerk.OrganizationInvitation{}
err = c.Backend.Call(ctx, req, invitation)
return invitation, err
}

0 comments on commit 8ac9efb

Please sign in to comment.