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

Adds support for fetching orgs with include_members_count #362

Merged
merged 3 commits into from
Dec 4, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add support for the OAuth Applications API. Added the oauthapplication package for API operations and a clerk.OAuthApplication type.
- Add support for multiple invitation templates with the `TemplateSlug` field in `invitation.Create`.
- Add support for listing and creating waitlist entries with the `waitlistentry.List` and `waitlistentry.Create` methods.
- Add support for fetching an organization with its members count, via a new `organizations.GetWithParams` method.

## 2.2.0

Expand Down
20 changes: 20 additions & 0 deletions organization/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,34 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Organ
return organization, err
}

type GetParams struct {
clerk.APIParams
IncludeMembersCount *bool `json:"include_members_count,omitempty"`
}

func (params *GetParams) ToQuery() url.Values {
q := url.Values{}
if params.IncludeMembersCount != nil {
q.Set("include_members_count", strconv.FormatBool(*params.IncludeMembersCount))
}
return q
}

// Get retrieves details for an organization.
// The organization can be fetched by either the ID or its slug.
func (c *Client) Get(ctx context.Context, idOrSlug string) (*clerk.Organization, error) {
return c.GetWithParams(ctx, idOrSlug, &GetParams{})
}

// GetWithParams retrieves details for an organization.
// The organization can be fetched by either the ID or its slug.
func (c *Client) GetWithParams(ctx context.Context, idOrSlug string, params *GetParams) (*clerk.Organization, error) {
path, err := clerk.JoinPath(path, idOrSlug)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
organization := &clerk.Organization{}
err = c.Backend.Call(ctx, req, organization)
return organization, err
Expand Down
25 changes: 25 additions & 0 deletions organization/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ func TestOrganizationClientGet(t *testing.T) {
require.Equal(t, name, organization.Name)
}

func TestOrganizationClientGetWithParams(t *testing.T) {
t.Parallel()
id := "org_123"
name := "Acme Inc"
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","name":"%s"}`, id, name)),
Method: http.MethodGet,
Path: "/v1/organizations/" + id,
Query: &url.Values{
"include_members_count": []string{"true"},
},
},
}
client := NewClient(config)
organization, err := client.GetWithParams(context.Background(), id, &GetParams{
IncludeMembersCount: clerk.Bool(true),
})
require.NoError(t, err)
require.Equal(t, id, organization.ID)
require.Equal(t, name, organization.Name)
}

func TestOrganizationClientUpdate(t *testing.T) {
t.Parallel()
id := "org_123"
Expand Down
Loading