diff --git a/user/api.go b/user/api.go index 7966f23..f093eec 100644 --- a/user/api.go +++ b/user/api.go @@ -90,6 +90,11 @@ func ListOrganizationMemberships(ctx context.Context, id string, params *ListOrg return getClient().ListOrganizationMemberships(ctx, id, params) } +// ListOrganizationInvitations lists all the user's organization invitations. +func ListOrganizationInvitations(ctx context.Context, params *ListOrganizationInvitationsParams) (*clerk.OrganizationInvitationList, error) { + return getClient().ListOrganizationInvitations(ctx, params) +} + // DeletePasskey deletes a passkey by its identification ID. func DeletePasskey(ctx context.Context, userID, identificationID string) (*clerk.DeletedResource, error) { return getClient().DeletePasskey(ctx, userID, identificationID) diff --git a/user/client.go b/user/client.go index fe78e43..960af92 100644 --- a/user/client.go +++ b/user/client.go @@ -429,6 +429,37 @@ func (c *Client) ListOrganizationMemberships(ctx context.Context, id string, par return list, err } +type ListOrganizationInvitationsParams struct { + clerk.APIParams + clerk.ListParams + UserID string `json:"-"` + Statuses *[]string `json:"statuses,omitempty"` +} + +// ToQuery returns url.Values from the params. +func (params *ListOrganizationInvitationsParams) ToQuery() url.Values { + query := params.ListParams.ToQuery() + + if params.Statuses != nil { + query["status"] = *params.Statuses + } + + return query +} + +// ListOrganizationInvitations lists all the user's organization invitations. +func (c *Client) ListOrganizationInvitations(ctx context.Context, params *ListOrganizationInvitationsParams) (*clerk.OrganizationInvitationList, error) { + path, err := clerk.JoinPath(path, params.UserID, "/organization_invitations") + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodGet, path) + req.SetParams(params) + list := &clerk.OrganizationInvitationList{} + err = c.Backend.Call(ctx, req, list) + return list, err +} + // DeletePasskey deletes a passkey by its identification ID. func (c *Client) DeletePasskey(ctx context.Context, userID, identificationID string) (*clerk.DeletedResource, error) { path, err := clerk.JoinPath(path, userID, "/passkeys", identificationID) diff --git a/user/client_test.go b/user/client_test.go index fe9c3a2..837d7f3 100644 --- a/user/client_test.go +++ b/user/client_test.go @@ -423,6 +423,45 @@ func TestUserClientListOrganizationMemberships(t *testing.T) { require.Equal(t, userID, list.OrganizationMemberships[0].PublicUserData.UserID) } +func TestUserClientListOrganizationInvitations(t *testing.T) { + t.Parallel() + invitationID := "orginv_123" + organizationID := "org_123" + userID := "user_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Out: json.RawMessage(fmt.Sprintf(`{ + "data": [{ + "id":"%s", + "organization_id":"%s" + }], + "total_count": 1 + }`, + invitationID, organizationID)), + Method: http.MethodGet, + Path: "/v1/users/" + userID + "/organization_invitations", + Query: &url.Values{ + "limit": []string{"1"}, + "offset": []string{"2"}, + "status": []string{"accepted"}, + }, + }, + } + client := NewClient(config) + params := &ListOrganizationInvitationsParams{ + Statuses: &[]string{"accepted"}, + UserID: userID, + } + params.Limit = clerk.Int64(1) + params.Offset = clerk.Int64(2) + list, err := client.ListOrganizationInvitations(context.Background(), params) + require.NoError(t, err) + require.Equal(t, invitationID, list.OrganizationInvitations[0].ID) + require.Equal(t, organizationID, list.OrganizationInvitations[0].OrganizationID) +} + func TestUserClientDeletePasskey(t *testing.T) { t.Parallel() userID := "user_123"