Skip to content

Commit

Permalink
fix: approval/grant postgres repository filters
Browse files Browse the repository at this point in the history
Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
  • Loading branch information
kushsharma committed Oct 7, 2023
1 parent 40a44f1 commit 3a02c22
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 42 deletions.
1 change: 1 addition & 0 deletions api/handler/v1beta1/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *GRPCServer) ListUserGrants(ctx context.Context, req *guardianv1beta1.Li
}

filter := domain.ListGrantsFilter{
Q: req.GetQ(),
Statuses: req.GetStatuses(),
AccountIDs: req.GetAccountIds(),
AccountTypes: req.GetAccountTypes(),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.0.0
gorm.io/driver/postgres v1.4.6
gorm.io/gorm v1.24.3
gorm.io/gorm v1.25.1
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2193,8 +2193,8 @@ gorm.io/gorm v1.20.5/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.3 h1:WL2ifUmzR/SLp85CSURAfybcHnGZ+yLSGSxgYXlFBHg=
gorm.io/gorm v1.24.3/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
Expand Down
51 changes: 12 additions & 39 deletions internal/store/postgres/approval_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,19 @@ func NewApprovalRepository(db *Store) *ApprovalRepository {
return &ApprovalRepository{db}
}

func (r *ApprovalRepository) ListApprovals(ctx context.Context, conditions *domain.ListApprovalsFilter) ([]*domain.Approval, error) {
if err := utils.ValidateStruct(conditions); err != nil {
func (r *ApprovalRepository) ListApprovals(ctx context.Context, filters *domain.ListApprovalsFilter) ([]*domain.Approval, error) {
if err := utils.ValidateStruct(filters); err != nil {
return nil, err
}

var models []*model.Approval
err := r.store.Tx(ctx, func(tx *gorm.DB) error {
tx = tx.Preload("Appeal.Resource")
tx = tx.Joins("Appeal")
tx = tx.Joins(`JOIN "approvers" ON "approvals"."id" = "approvers"."approval_id"`)

if conditions.CreatedBy != "" {
tx = tx.Where(`"approvers"."email" = ?`, conditions.CreatedBy)
}
if conditions.Statuses != nil {
tx = tx.Where(`"approvals"."status" IN ?`, conditions.Statuses)
}
if conditions.AccountID != "" {
tx = tx.Where(`"Appeal"."account_id" = ?`, conditions.AccountID)
}

if len(conditions.AppealStatuses) == 0 {
tx = tx.Where(`"Appeal"."status" != ?`, domain.AppealStatusCanceled)
} else {
tx = tx.Where(`"Appeal"."status" IN ?`, conditions.AppealStatuses)
}

if conditions.OrderBy != nil {
tx = addOrderByClause(tx, conditions.OrderBy, addOrderByClauseOptions{
statusColumnName: `"approvals"."status"`,
statusesOrder: AppealStatusDefaultSort,
})
tx = applyFilter(tx, filters)
if filters.Size > 0 {
tx = tx.Limit(filters.Size)
}

if conditions.Size > 0 {
tx = tx.Limit(conditions.Size)
}

if conditions.Offset > 0 {
tx = tx.Offset(conditions.Offset)
if filters.Offset > 0 {
tx = tx.Offset(filters.Offset)
}

return tx.Find(&models).Error
Expand All @@ -91,14 +64,14 @@ func (r *ApprovalRepository) ListApprovals(ctx context.Context, conditions *doma
}

func (r *ApprovalRepository) GetApprovalsTotalCount(ctx context.Context, filter *domain.ListApprovalsFilter) (int64, error) {
db := r.store.db.WithContext(ctx)
db = applyFilter(db, filter)

var count int64
if err := db.Model(&model.Approval{}).Count(&count).Error; err != nil {
err := r.store.Tx(ctx, func(tx *gorm.DB) error {
tx = applyFilter(tx, filter)
return tx.Model(&model.Approval{}).Count(&count).Error
})
if err != nil {
return 0, err
}

return count, nil
}

Expand Down
10 changes: 10 additions & 0 deletions internal/store/postgres/grant_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func NewGrantRepository(db *Store) *GrantRepository {
func (r *GrantRepository) List(ctx context.Context, filter domain.ListGrantsFilter) ([]domain.Grant, error) {
var models []model.Grant
err := r.store.Tx(ctx, func(tx *gorm.DB) error {
tx = tx.Joins("JOIN resources ON grants.resource_id = resources.id")
if filter.Q != "" {
// NOTE: avoid adding conditions before this grouped where clause.
// Otherwise, it will be wrapped in parentheses and the query will be invalid.
tx = tx.Where(tx.
Where(`"grants"."account_id" LIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)).
Or(`"grants"."role" LIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)).
Or(`"resources"."urn" LIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)),
)
}
if filter.AccountIDs != nil {
tx = tx.Where(`"grants"."account_id" IN ?`, filter.AccountIDs)
}
Expand Down

0 comments on commit 3a02c22

Please sign in to comment.