Skip to content

Commit a60e1db

Browse files
committed
feat: treat account name and note filter parameters like "contains"
1 parent 07def0b commit a60e1db

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

pkg/controllers/account.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/envelope-zero/backend/pkg/models"
1010
"github.com/gin-gonic/gin"
1111
"github.com/google/uuid"
12+
"golang.org/x/exp/slices"
1213
)
1314

1415
type AccountListResponse struct {
@@ -30,8 +31,8 @@ type AccountLinks struct {
3031
}
3132

3233
type AccountQueryFilter struct {
33-
Name string `form:"name"`
34-
Note string `form:"note"`
34+
Name string `form:"name" filterField:"false"` // Fuzzy filter for the account name
35+
Note string `form:"note" filterField:"false"` // Fuzzy filter for the note
3536
BudgetID string `form:"budget"`
3637
OnBudget bool `form:"onBudget"`
3738
External bool `form:"external"`
@@ -44,8 +45,6 @@ func (a AccountQueryFilter) ToCreate(c *gin.Context) (models.AccountCreate, bool
4445
}
4546

4647
return models.AccountCreate{
47-
Name: a.Name,
48-
Note: a.Note,
4948
BudgetID: budgetID,
5049
OnBudget: a.OnBudget,
5150
External: a.External,
@@ -165,18 +164,32 @@ func (co Controller) GetAccounts(c *gin.Context) {
165164
}
166165

167166
// Get the set parameters in the query string
168-
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
167+
queryFields, setFields := httputil.GetURLFields(c.Request.URL, filter)
169168

170169
// Convert the QueryFilter to a Create struct
171170
create, ok := filter.ToCreate(c)
172171
if !ok {
173172
return
174173
}
175174

176-
var accounts []models.Account
177-
if !queryWithRetry(c, co.DB.Where(&models.Account{
175+
query := co.DB.Where(&models.Account{
178176
AccountCreate: create,
179-
}, queryFields...).Find(&accounts)) {
177+
}, queryFields...)
178+
179+
if filter.Name != "" {
180+
query = query.Where("name LIKE ?", fmt.Sprintf("%%%s%%", filter.Name))
181+
} else if slices.Contains(setFields, "Name") {
182+
query = query.Where("name = ''")
183+
}
184+
185+
if filter.Note != "" {
186+
query = query.Where("note LIKE ?", fmt.Sprintf("%%%s%%", filter.Note))
187+
} else if slices.Contains(setFields, "Note") {
188+
query = query.Where("note = ''")
189+
}
190+
191+
var accounts []models.Account
192+
if !queryWithRetry(c, query.Find(&accounts)) {
180193
return
181194
}
182195

pkg/controllers/account_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,36 @@ func (suite *TestSuiteStandard) TestGetAccountsFilter() {
100100
External: true,
101101
})
102102

103+
_ = suite.createTestAccount(models.AccountCreate{
104+
Name: "",
105+
Note: "specific note",
106+
BudgetID: b1.Data.ID,
107+
})
108+
109+
_ = suite.createTestAccount(models.AccountCreate{
110+
Name: "Name only",
111+
Note: "",
112+
BudgetID: b1.Data.ID,
113+
})
114+
103115
tests := []struct {
104116
name string
105117
query string
106118
len int
107119
}{
108120
{"Name single", "name=Exact Account Match", 1},
109121
{"Name multiple", "name=External Account Filter", 2},
122+
{"Fuzzy name", "name=Account", 3},
110123
{"Note", "note=A different note", 1},
111-
{"Budget", fmt.Sprintf("budget=%s", b1.Data.ID), 2},
124+
{"Fuzzy Note", "note=note", 4},
125+
{"Empty name with note", "name=&note=specific", 1},
126+
{"Empty note with name", "note=&name=Name", 1},
127+
{"Empty note and name", "note=&name=&onBudget=false", 0},
128+
{"Budget", fmt.Sprintf("budget=%s", b1.Data.ID), 4},
112129
{"On budget", "onBudget=true", 1},
113-
{"Off budget", "onBudget=false", 2},
130+
{"Off budget", "onBudget=false", 4},
114131
{"External", "external=true", 2},
115-
{"Internal", "external=false", 1},
132+
{"Internal", "external=false", 3},
116133
}
117134

118135
for _, tt := range tests {
@@ -126,7 +143,7 @@ func (suite *TestSuiteStandard) TestGetAccountsFilter() {
126143
for _, d := range re.Data {
127144
accountNames = append(accountNames, d.Name)
128145
}
129-
assert.Equal(t, tt.len, len(re.Data), "Existing accounts: %#v", strings.Join(accountNames, ", "))
146+
assert.Equal(t, tt.len, len(re.Data), "Existing accounts: %#v, Request-ID: %s", strings.Join(accountNames, ", "), r.Header().Get("x-request-id"))
130147
})
131148
}
132149

0 commit comments

Comments
 (0)