From 8ac9efb25ce913e2e2c20abd1c28391b6f26ea5f Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Mon, 30 Sep 2024 17:45:56 -0300 Subject: [PATCH] feat: add new methods for organization invitation API --- organization_invitation.go | 35 +++++++++++++++------ organizationinvitation/api.go | 15 +++++++++ organizationinvitation/client.go | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 10 deletions(-) diff --git a/organization_invitation.go b/organization_invitation.go index c1ccafb5..844faf46 100644 --- a/organization_invitation.go +++ b/organization_invitation.go @@ -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"` } diff --git a/organizationinvitation/api.go b/organizationinvitation/api.go index 4ff154bd..cf2a4e69 100644 --- a/organizationinvitation/api.go +++ b/organizationinvitation/api.go @@ -13,6 +13,21 @@ func Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationInvit return getClient().Create(ctx, params) } +// List returns a list of organization invitations +func List(ctx context.Context, organizationID string, params *ListParams) (*clerk.OrganizationInvitation, error) { + return getClient().List(ctx, organizationID, params) +} + +// Get retrieves the detail for an organization invitation. +func Get(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) { + return getClient().Get(ctx, organizationID, id) +} + +// Revoke marks the organization invitation as revoked. +func Revoke(ctx context.Context, organizationID, id string) (*clerk.OrganizationInvitation, error) { + return getClient().Revoke(ctx, organizationID, id) +} + func getClient() *Client { return &Client{ Backend: clerk.GetBackend(), diff --git a/organizationinvitation/client.go b/organizationinvitation/client.go index 97deedd0..ae405822 100644 --- a/organizationinvitation/client.go +++ b/organizationinvitation/client.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "net/http" + "net/url" "github.com/clerk/clerk-sdk-go/v2" ) @@ -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 +}