From 0d80f08b2d8933d98ec4f749014c6ee7a2017f1d Mon Sep 17 00:00:00 2001 From: Mary Zhong Date: Wed, 23 Oct 2024 17:00:09 -0400 Subject: [PATCH] fix: Update list orgs endpoint to support elevated permissions check --- CHANGELOG.md | 1 + organization.go | 32 +++++++++++++------------- organization/api.go | 4 ++-- organization/client.go | 41 ++++++++++++--------------------- organization/client_test.go | 46 +++++++++++-------------------------- 5 files changed, 48 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e411259..366f8e9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/organization.go b/organization.go index d811acfa..7ab3c441 100644 --- a/organization.go +++ b/organization.go @@ -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 { diff --git a/organization/api.go b/organization/api.go index 557fe326..fa942759 100644 --- a/organization/api.go +++ b/organization/api.go @@ -15,8 +15,8 @@ func Create(ctx context.Context, params *CreateParams) (*clerk.Organization, err // Get retrieves details for an organization. // The organization can be fetched by either the ID or its slug. -func Get(ctx context.Context, idOrSlug string, params *GetParams) (*clerk.Organization, error) { - return getClient().Get(ctx, idOrSlug, params) +func Get(ctx context.Context, idOrSlug string) (*clerk.Organization, error) { + return getClient().Get(ctx, idOrSlug) } // Update updates an organization. diff --git a/organization/client.go b/organization/client.go index 328fb756..8b1c4dd1 100644 --- a/organization/client.go +++ b/organization/client.go @@ -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 @@ -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. @@ -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) @@ -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 } diff --git a/organization/client_test.go b/organization/client_test.go index cf96ae3a..4809e1eb 100644 --- a/organization/client_test.go +++ b/organization/client_test.go @@ -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) { @@ -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, @@ -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) @@ -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 {