Skip to content

Commit

Permalink
fix: Update list orgs endpoint to support elevated permissions check
Browse files Browse the repository at this point in the history
  • Loading branch information
mzhong9723 committed Oct 31, 2024
1 parent 5e7d1ed commit 0d80f08
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next release

- Add `EmailAddressQuery`, `PhoneNumberQuery` and `UsernameQuery` to `user.ListParams`.
- Add support for `missing_member_with_elevated_permissions` checks to the `organization.List` method.

## 2.1.0

Expand Down
32 changes: 16 additions & 16 deletions organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import "encoding/json"

type Organization struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
ImageURL *string `json:"image_url"`
HasImage bool `json:"has_image"`
MembersCount *int64 `json:"members_count,omitempty"`
HasMemberWithElevatedPermissions *bool `json:"has_member_with_elevated_permissions,omitempty"`
PendingInvitationsCount *int64 `json:"pending_invitations_count,omitempty"`
MaxAllowedMemberships int64 `json:"max_allowed_memberships"`
AdminDeleteEnabled bool `json:"admin_delete_enabled"`
PublicMetadata json.RawMessage `json:"public_metadata"`
PrivateMetadata json.RawMessage `json:"private_metadata"`
CreatedBy string `json:"created_by"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
ImageURL *string `json:"image_url"`
HasImage bool `json:"has_image"`
MembersCount *int64 `json:"members_count,omitempty"`
MissingMemberWithElevatedPermissions *bool `json:"missing_member_with_elevated_permissions,omitempty"`
PendingInvitationsCount *int64 `json:"pending_invitations_count,omitempty"`
MaxAllowedMemberships int64 `json:"max_allowed_memberships"`
AdminDeleteEnabled bool `json:"admin_delete_enabled"`
PublicMetadata json.RawMessage `json:"public_metadata"`
PrivateMetadata json.RawMessage `json:"private_metadata"`
CreatedBy string `json:"created_by"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type OrganizationList struct {
Expand Down
4 changes: 2 additions & 2 deletions organization/api.go

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

41 changes: 15 additions & 26 deletions organization/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,14 @@ 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"`
IncludeHasMemberWithElevatedPermissions *bool `json:"include_has_member_with_elevated_permissions,omitempty"`
}

func (params *GetParams) ToQuery() url.Values {
q := url.Values{}
if params.IncludeMembersCount != nil {
q.Set("include_members_count", strconv.FormatBool(*params.IncludeMembersCount))
}
if params.IncludeHasMemberWithElevatedPermissions != nil {
q.Set("include_has_member_with_elevated_permissions", strconv.FormatBool(*params.IncludeHasMemberWithElevatedPermissions))
}
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, params *GetParams) (*clerk.Organization, error) {
func (c *Client) Get(ctx context.Context, idOrSlug string) (*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 Expand Up @@ -203,11 +185,12 @@ func (c *Client) DeleteLogo(ctx context.Context, id string) (*clerk.Organization
type ListParams struct {
clerk.APIParams
clerk.ListParams
IncludeMembersCount *bool `json:"include_members_count,omitempty"`
IncludeHasMemberWithElevatedPermissions *bool `json:"include_has_member_with_elevated_permissions,omitempty"`
OrderBy *string `json:"order_by,omitempty"`
Query *string `json:"query,omitempty"`
UserIDs []string `json:"user_id,omitempty"`
IncludeMembersCount *bool `json:"include_members_count,omitempty"`
IncludeMissingMemberWithElevatedPermissions *bool `json:"include_missing_member_with_elevated_permissions,omitempty"`
OrderBy *string `json:"order_by,omitempty"`
Query *string `json:"query,omitempty"`
UserIDs []string `json:"user_id,omitempty"`
FilterBy []string `json:"filter_by,omitempty"`
}

// ToQuery returns query string values from the params.
Expand All @@ -216,8 +199,8 @@ func (params *ListParams) ToQuery() url.Values {
if params.IncludeMembersCount != nil {
q.Set("include_members_count", strconv.FormatBool(*params.IncludeMembersCount))
}
if params.IncludeHasMemberWithElevatedPermissions != nil {
q.Set("include_has_member_with_elevated_permissions", strconv.FormatBool(*params.IncludeHasMemberWithElevatedPermissions))
if params.IncludeMissingMemberWithElevatedPermissions != nil {
q.Set("include_missing_member_with_elevated_permissions", strconv.FormatBool(*params.IncludeMissingMemberWithElevatedPermissions))
}
if params.OrderBy != nil {
q.Set("order_by", *params.OrderBy)
Expand All @@ -228,6 +211,12 @@ func (params *ListParams) ToQuery() url.Values {
if params.UserIDs != nil {
q["user_id"] = params.UserIDs
}
if params.UserIDs != nil {
q["user_id"] = params.UserIDs
}
if params.FilterBy != nil {
q["filter_by"] = params.FilterBy
}
return q
}

Expand Down
46 changes: 14 additions & 32 deletions organization/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,20 @@ func TestOrganizationClientGet(t *testing.T) {
t.Parallel()
id := "org_123"
name := "Acme Inc"
membersCount := int64(1)
hasMemberWithElevatedPermissions := true
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(
`{"id":"%s","name":"%s","members_count":%d,"has_member_with_elevated_permissions":%t}`,
id,
name,
membersCount,
hasMemberWithElevatedPermissions,
)),
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"},
"include_has_member_with_elevated_permissions": []string{"true"},
},
},
}
params := &GetParams{
IncludeMembersCount: clerk.Bool(true),
IncludeHasMemberWithElevatedPermissions: clerk.Bool(true),
}
client := NewClient(config)
organization, err := client.Get(context.Background(), id, params)
organization, err := client.Get(context.Background(), id)
require.NoError(t, err)
require.Equal(t, id, organization.ID)
require.Equal(t, name, organization.Name)
require.Equal(t, membersCount, *organization.MembersCount)
require.NotNil(t, organization.HasMemberWithElevatedPermissions)
require.Equal(t, hasMemberWithElevatedPermissions, *organization.HasMemberWithElevatedPermissions)

}

func TestOrganizationClientUpdate(t *testing.T) {
Expand Down Expand Up @@ -176,7 +156,7 @@ func TestOrganizationClientList(t *testing.T) {
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{
"data": [{"id":"org_123","name":"Acme Inc","members_count":1,"has_member_with_elevated_permissions":true}],
"data": [{"id":"org_123","name":"Acme Inc","members_count":1,"missing_member_with_elevated_permissions":true}],
"total_count": 1
}`),
Method: http.MethodGet,
Expand All @@ -187,18 +167,20 @@ func TestOrganizationClientList(t *testing.T) {
"order_by": []string{"-created_at"},
"query": []string{"Acme"},
"user_id": []string{"user_123", "user_456"},
"filter_by": []string{"missing_member_with_elevated_permissions"},
"include_members_count": []string{"true"},
"include_has_member_with_elevated_permissions": []string{"true"},
"include_missing_member_with_elevated_permissions": []string{"true"},
},
},
}
client := NewClient(config)
params := &ListParams{
OrderBy: clerk.String("-created_at"),
Query: clerk.String("Acme"),
UserIDs: []string{"user_123", "user_456"},
IncludeMembersCount: clerk.Bool(true),
IncludeHasMemberWithElevatedPermissions: clerk.Bool(true),
OrderBy: clerk.String("-created_at"),
Query: clerk.String("Acme"),
UserIDs: []string{"user_123", "user_456"},
FilterBy: []string{"missing_member_with_elevated_permissions"},
IncludeMembersCount: clerk.Bool(true),
IncludeMissingMemberWithElevatedPermissions: clerk.Bool(true),
}
params.Limit = clerk.Int64(1)
params.Offset = clerk.Int64(2)
Expand All @@ -210,8 +192,8 @@ func TestOrganizationClientList(t *testing.T) {
require.Equal(t, "Acme Inc", list.Organizations[0].Name)
require.NotNil(t, list.Organizations[0].MembersCount)
require.Equal(t, int64(1), *list.Organizations[0].MembersCount)
require.NotNil(t, *list.Organizations[0].HasMemberWithElevatedPermissions)
require.Equal(t, true, *list.Organizations[0].HasMemberWithElevatedPermissions)
require.NotNil(t, *list.Organizations[0].MissingMemberWithElevatedPermissions)
require.Equal(t, true, *list.Organizations[0].MissingMemberWithElevatedPermissions)
}

type testFile struct {
Expand Down

0 comments on commit 0d80f08

Please sign in to comment.