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

feat: add pagination to ListAllOrganizations API #724

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
VERSION := $(shell git describe --tags ${TAG})
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto ui compose-up-dev
.DEFAULT_GOAL := build
PROTON_COMMIT := "fecbdc10ba9cfb77b27cdca1722d23460eabbf04"
PROTON_COMMIT := "c705b55e27b55f9dfa60aebb41d9683e861029d6"

ui:
@echo " > generating ui build"
Expand Down
6 changes: 6 additions & 0 deletions core/organization/filter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package organization

import (
"github.com/raystack/frontier/pkg/pagination"
)

type Filter struct {
UserID string

IDs []string
State State

Pagination pagination.Pagination
}
16 changes: 11 additions & 5 deletions internal/api/v1beta1/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package v1beta1
import (
"context"

"github.com/raystack/frontier/core/role"
"go.uber.org/zap"

"github.com/raystack/frontier/core/audit"
"github.com/raystack/frontier/core/role"
"github.com/raystack/frontier/pkg/pagination"
"github.com/raystack/frontier/pkg/utils"

"github.com/raystack/frontier/internal/bootstrap/schema"
Expand Down Expand Up @@ -49,8 +50,9 @@ type OrganizationService interface {
func (h Handler) ListOrganizations(ctx context.Context, request *frontierv1beta1.ListOrganizationsRequest) (*frontierv1beta1.ListOrganizationsResponse, error) {
var orgs []*frontierv1beta1.Organization
orgList, err := h.orgService.List(ctx, organization.Filter{
State: organization.State(request.GetState()),
UserID: request.GetUserId(),
State: organization.State(request.GetState()),
UserID: request.GetUserId(),
Pagination: pagination.Pagination{},
})
if err != nil {
return nil, err
Expand All @@ -72,9 +74,12 @@ func (h Handler) ListOrganizations(ctx context.Context, request *frontierv1beta1

func (h Handler) ListAllOrganizations(ctx context.Context, request *frontierv1beta1.ListAllOrganizationsRequest) (*frontierv1beta1.ListAllOrganizationsResponse, error) {
var orgs []*frontierv1beta1.Organization
paginate := pagination.NewPagination(request.GetPageNum(), request.GetPageSize())

orgList, err := h.orgService.List(ctx, organization.Filter{
State: organization.State(request.GetState()),
UserID: request.GetUserId(),
State: organization.State(request.GetState()),
UserID: request.GetUserId(),
Pagination: paginate,
})
if err != nil {
return nil, err
Expand All @@ -91,6 +96,7 @@ func (h Handler) ListAllOrganizations(ctx context.Context, request *frontierv1be

return &frontierv1beta1.ListAllOrganizationsResponse{
Organizations: orgs,
Count: paginate.Count,
}, nil
}

Expand Down
20 changes: 17 additions & 3 deletions internal/api/v1beta1/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/raystack/frontier/core/project"
"github.com/raystack/frontier/core/serviceuser"

"github.com/raystack/frontier/pkg/pagination"
"github.com/raystack/frontier/pkg/utils"

"github.com/raystack/frontier/core/organization"
Expand Down Expand Up @@ -847,7 +848,12 @@ func TestHandler_ListAllOrganizations(t *testing.T) {
name: "should return internal error if org service return some error",
setup: func(os *mocks.OrganizationService) {
os.EXPECT().List(mock.AnythingOfType("context.backgroundCtx"),
organization.Filter{}).Return([]organization.Organization{}, errors.New("test error"))
organization.Filter{
Pagination: pagination.Pagination{
PageNum: 1,
PageSize: 50,
},
}).Return([]organization.Organization{}, errors.New("test error"))
},
req: &frontierv1beta1.ListAllOrganizationsRequest{},
want: nil,
Expand All @@ -856,7 +862,11 @@ func TestHandler_ListAllOrganizations(t *testing.T) {
{
name: "should return empty list of orgs if org service return nil error",
setup: func(os *mocks.OrganizationService) {
os.EXPECT().List(mock.AnythingOfType("context.backgroundCtx"), organization.Filter{}).Return([]organization.Organization{}, nil)
os.EXPECT().List(mock.AnythingOfType("context.backgroundCtx"), organization.Filter{
Pagination: pagination.Pagination{
PageNum: 1,
PageSize: 50,
}}).Return([]organization.Organization{}, nil)
},
req: &frontierv1beta1.ListAllOrganizationsRequest{},
want: &frontierv1beta1.ListAllOrganizationsResponse{},
Expand All @@ -869,7 +879,11 @@ func TestHandler_ListAllOrganizations(t *testing.T) {
for _, o := range testOrgMap {
testOrgList = append(testOrgList, o)
}
os.EXPECT().List(mock.AnythingOfType("context.backgroundCtx"), organization.Filter{}).Return(testOrgList, nil)
os.EXPECT().List(mock.AnythingOfType("context.backgroundCtx"), organization.Filter{
Pagination: pagination.Pagination{
PageNum: 1,
PageSize: 50,
}}).Return(testOrgList, nil)
},
req: &frontierv1beta1.ListAllOrganizationsRequest{},
want: &frontierv1beta1.ListAllOrganizationsResponse{
Expand Down
20 changes: 20 additions & 0 deletions internal/store/postgres/organization_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ func (r OrganizationRepository) List(ctx context.Context, flt organization.Filte
"id": goqu.Op{"in": flt.IDs},
})
}
offset := flt.Pagination.Offset()
limit := flt.Pagination.PageSize

totalCountStmt := stmt.Select(goqu.COUNT("*"))
totalCountQuery, _, err := totalCountStmt.ToSQL()
if err != nil {
return []organization.Organization{}, fmt.Errorf("%w: %s", queryErr, err)
}

anujk14 marked this conversation as resolved.
Show resolved Hide resolved
var totalCount int32
if err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "Count", func(ctx context.Context) error {
return r.dbc.GetContext(ctx, &totalCount, totalCountQuery)
}); err != nil {
return nil, fmt.Errorf("%w: %s", dbErr, err)
}

flt.Pagination.SetCount(totalCount)

stmt = stmt.Limit(uint(limit)).Offset(uint(offset)).Order(goqu.C("created_at").Desc())

query, params, err := stmt.ToSQL()
if err != nil {
return []organization.Organization{}, fmt.Errorf("%w: %s", queryErr, err)
Expand Down
12 changes: 9 additions & 3 deletions internal/store/postgres/organization_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"testing"

"github.com/raystack/frontier/pkg/pagination"

"github.com/raystack/frontier/internal/bootstrap/schema"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -268,14 +270,17 @@ func (s *OrganizationRepositoryTestSuite) TestList() {
var testCases = []testCase{
{
Description: "should get all organizations",
Filter: organization.Filter{
Pagination: pagination.Pagination{},
},
ExpectedOrganizations: []organization.Organization{
{
Name: "org-1",
Name: "org-2", // descending order of creation
State: organization.Enabled,
Metadata: metadata.Metadata{},
},
{
Name: "org-2",
Name: "org-1",
State: organization.Enabled,
Metadata: metadata.Metadata{},
},
Expand All @@ -284,7 +289,8 @@ func (s *OrganizationRepositoryTestSuite) TestList() {
{
Description: "should return empty list and no error if no organizations found",
Filter: organization.Filter{
State: organization.Disabled,
State: organization.Disabled,
Pagination: pagination.Pagination{},
},
ErrString: "",
},
Expand Down
34 changes: 34 additions & 0 deletions pkg/pagination/pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pagination

const (
DefaultPageSize = 50
DefaultPageNum = 1
)

type Pagination struct {
PageNum int32
PageSize int32
Count int32 // total number of records in DB
}

func NewPagination(pageNum, pageSize int32) Pagination {
if pageNum == 0 {
pageNum = DefaultPageNum
}
if pageSize == 0 {
pageSize = DefaultPageSize
}

return Pagination{
PageNum: pageNum,
PageSize: pageSize,
}
}

func (p *Pagination) Offset() int32 {
return p.PageSize * (p.PageNum - 1)
}

func (p *Pagination) SetCount(count int32) {
p.Count = count
}
16 changes: 16 additions & 0 deletions proto/apidocs.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ paths:
in: query
required: false
type: string
- name: page_size
description: The maximum number of organizations to return per page. The default is 50.
in: query
required: false
type: integer
format: int32
- name: page_num
description: The page number to return. The default is 1.
in: query
required: false
type: integer
format: int32
tags:
- Organization
/v1beta1/admin/organizations/{org_id}/billing/{billing_id}/checkouts:
Expand Down Expand Up @@ -11077,6 +11089,10 @@ definitions:
items:
type: object
$ref: '#/definitions/v1beta1Organization'
count:
type: integer
format: int32
description: Total number of records present
v1beta1ListAllUsersResponse:
type: object
properties:
Expand Down
Loading
Loading