From 505e7e9b79f8d0d14fd0d298a6775e473503c766 Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Tue, 21 May 2024 18:12:37 +0900 Subject: [PATCH] trivial. fix user filter for organizations --- internal/repository/organization.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/repository/organization.go b/internal/repository/organization.go index 0b53d9bf..9b2340ed 100644 --- a/internal/repository/organization.go +++ b/internal/repository/organization.go @@ -2,6 +2,7 @@ package repository import ( "context" + "fmt" "github.com/google/uuid" "github.com/openinfradev/tks-api/internal/model" @@ -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 {