Skip to content

Commit

Permalink
feat: Support 'order_by' & 'query' parameters on Organization Role & …
Browse files Browse the repository at this point in the history
…Permission List methods

As part of the List operations for Organization Roles & Permissions, we now
support the 'order_by' & 'query' parameters along with the regular pagination
  • Loading branch information
chanioxaris committed Nov 16, 2023
1 parent 3299215 commit 6795e3a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
14 changes: 12 additions & 2 deletions clerk/instance_organization_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

type ListInstanceOrganizationPermissionsParams struct {
Limit *int `json:"limit,omitempty"`
Offset *int `json:"offset,omitempty"`
Limit *int
Offset *int
Query *string
OrderBy *string
}

func (s *InstanceService) ListOrganizationPermissions(params ListInstanceOrganizationPermissionsParams) (*PermissionsResponse, error) {
Expand All @@ -15,6 +17,14 @@ func (s *InstanceService) ListOrganizationPermissions(params ListInstanceOrganiz
paginationParams := PaginationParams{Limit: params.Limit, Offset: params.Offset}
query := req.URL.Query()
addPaginationParams(query, paginationParams)

if params.Query != nil {
query.Set("query", *params.Query)
}
if params.OrderBy != nil {
query.Set("order_by", *params.OrderBy)
}

req.URL.RawQuery = query.Encode()

response := &PermissionsResponse{}
Expand Down
12 changes: 8 additions & 4 deletions clerk/instance_organization_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func TestInstanceService_List_happyPathWithParameters(t *testing.T) {

actualQuery := req.URL.Query()
expectedQuery := url.Values(map[string][]string{
"limit": {"3"},
"offset": {"2"},
"limit": {"3"},
"offset": {"2"},
"query": {"my-query"},
"order_by": {"created_at"},
})
assert.Equal(t, expectedQuery, actualQuery)
fmt.Fprint(w, expectedResponse)
Expand All @@ -37,8 +39,10 @@ func TestInstanceService_List_happyPathWithParameters(t *testing.T) {
_ = json.Unmarshal([]byte(expectedResponse), want)

got, _ := client.Instances().ListOrganizationPermissions(ListInstanceOrganizationPermissionsParams{
Limit: intToPtr(3),
Offset: intToPtr(2),
Limit: intToPtr(3),
Offset: intToPtr(2),
Query: stringToPtr("my-query"),
OrderBy: stringToPtr("created_at"),
})
if len(got.Data) != len(want.Data) {
t.Errorf("Was expecting %d organization permissions to be returned, instead got %d", len(want.Data), len(got.Data))
Expand Down
14 changes: 12 additions & 2 deletions clerk/instance_organization_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func (s *InstanceService) CreateOrganizationRole(params CreateInstanceOrganizati
}

type ListInstanceOrganizationRoleParams struct {
Limit *int `json:"limit,omitempty"`
Offset *int `json:"offset,omitempty"`
Limit *int
Offset *int
Query *string
OrderBy *string
}

func (s *InstanceService) ListOrganizationRole(params ListInstanceOrganizationRoleParams) (*RolesResponse, error) {
Expand All @@ -34,6 +36,14 @@ func (s *InstanceService) ListOrganizationRole(params ListInstanceOrganizationRo
paginationParams := PaginationParams{Limit: params.Limit, Offset: params.Offset}
query := req.URL.Query()
addPaginationParams(query, paginationParams)

if params.Query != nil {
query.Set("query", *params.Query)
}
if params.OrderBy != nil {
query.Set("order_by", *params.OrderBy)
}

req.URL.RawQuery = query.Encode()

var orgRolesResponse *RolesResponse
Expand Down
16 changes: 15 additions & 1 deletion clerk/instance_organization_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,27 @@ func TestOrganizationsService_List_happyPath(t *testing.T) {
mux.HandleFunc("/organization_roles", func(w http.ResponseWriter, req *http.Request) {
testHttpMethod(t, req, "GET")
testHeader(t, req, "Authorization", "Bearer token")

expectedQuery := url.Values{
"limit": {"5"},
"offset": {"6"},
"query": {"my-query"},
"order_by": {"created_at"},
}
assert.Equal(t, expectedQuery, req.URL.Query())

fmt.Fprint(w, expectedResponse)
})

var want *RolesResponse
_ = json.Unmarshal([]byte(expectedResponse), &want)

got, _ := client.Instances().ListOrganizationRole(ListInstanceOrganizationRoleParams{})
got, _ := client.Instances().ListOrganizationRole(ListInstanceOrganizationRoleParams{
Limit: intToPtr(5),
Offset: intToPtr(6),
Query: stringToPtr("my-query"),
OrderBy: stringToPtr("created_at"),
})
if len(got.Data) != len(want.Data) {
t.Errorf("Was expecting %d organization roles to be returned, instead got %d", len(want.Data), len(got.Data))
}
Expand Down

0 comments on commit 6795e3a

Please sign in to comment.