Skip to content

Commit

Permalink
Merge pull request #499 from openinfradev/minor_fix
Browse files Browse the repository at this point in the history
trivial. fix user filter for organizations
  • Loading branch information
ktkfree authored May 21, 2024
2 parents 73bb151 + 505e7e9 commit 419a724
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions internal/repository/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/model"
Expand Down Expand Up @@ -88,13 +89,25 @@ func (r *OrganizationRepository) Fetch(ctx context.Context, pg *pagination.Pagin
db := r.db.WithContext(ctx).Preload(clause.Associations).Model(&model.Organization{})

// [TODO] more pretty!
adminQuery := ""
for _, filter := range pg.Filters {
if filter.Relation == "Admin" {
db = db.Joins("join users on users.id::text = organizations.admin_id::text").
Where("users.name ilike ?", "%"+filter.Values[0]+"%")
break
if adminQuery != "" {
adminQuery = adminQuery + " OR "
}

switch filter.Column {
case "name":
adminQuery = adminQuery + fmt.Sprintf("users.name ilike '%%%s%%'", filter.Values[0])
case "account_id":
adminQuery = adminQuery + fmt.Sprintf("users.account_id ilike '%%%s%%'", filter.Values[0])
case "email":
adminQuery = adminQuery + fmt.Sprintf("users.email ilike '%%%s%%'", filter.Values[0])
}
}
}
db = db.Joins("join users on users.id::text = organizations.admin_id::text").
Where(adminQuery)

_, res := pg.Fetch(db, &out)
if res.Error != nil {
Expand Down

0 comments on commit 419a724

Please sign in to comment.