Skip to content

Commit 71f2ad6

Browse files
committed
feat: treat transaction note filter parameters like "contains"
1 parent 9adb3f5 commit 71f2ad6

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pkg/controllers/transaction.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gin-gonic/gin"
1212
"github.com/google/uuid"
1313
"github.com/shopspring/decimal"
14+
"golang.org/x/exp/slices"
1415
"gorm.io/gorm"
1516
)
1617

@@ -34,7 +35,7 @@ type TransactionLinks struct {
3435
type TransactionQueryFilter struct {
3536
Date time.Time `form:"date"`
3637
Amount decimal.Decimal `form:"amount"`
37-
Note string `form:"note"`
38+
Note string `form:"note" filterField:"false"`
3839
BudgetID string `form:"budget"`
3940
SourceAccountID string `form:"source"`
4041
DestinationAccountID string `form:"destination"`
@@ -73,7 +74,6 @@ func (f TransactionQueryFilter) ToCreate(c *gin.Context) (models.TransactionCrea
7374
return models.TransactionCreate{
7475
Date: f.Date,
7576
Amount: f.Amount,
76-
Note: f.Note,
7777
BudgetID: budgetID,
7878
SourceAccountID: sourceAccountID,
7979
DestinationAccountID: destinationAccountID,
@@ -224,7 +224,7 @@ func (co Controller) GetTransactions(c *gin.Context) {
224224
}
225225

226226
// Get the fields set in the filter
227-
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
227+
queryFields, setFields := httputil.GetURLFields(c.Request.URL, filter)
228228

229229
// Convert the QueryFilter to a Create struct
230230
create, ok := filter.ToCreate(c)
@@ -254,6 +254,12 @@ func (co Controller) GetTransactions(c *gin.Context) {
254254
})
255255
}
256256

257+
if filter.Note != "" {
258+
query = query.Where("note LIKE ?", fmt.Sprintf("%%%s%%", filter.Note))
259+
} else if slices.Contains(setFields, "Note") {
260+
query = query.Where("note = ''")
261+
}
262+
257263
var transactions []models.Transaction
258264
if !queryWithRetry(c, query.Find(&transactions)) {
259265
return

pkg/controllers/transaction_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func (suite *TestSuiteStandard) TestGetTransactionsFilter() {
156156
{"Exact Amount", fmt.Sprintf("amount=%s", decimal.NewFromFloat(2.718).String()), 2},
157157
{"Note", "note=Not important", 1},
158158
{"No note", "note=", 1},
159+
{"Fuzzy note", "note=important", 2},
159160
{"Budget Match", fmt.Sprintf("budget=%s", b.Data.ID), 3},
160161
{"Envelope 2", fmt.Sprintf("envelope=%s", e2.Data.ID), 1},
161162
{"Non-existing Source Account", "source=3340a084-acf8-4cb4-8f86-9e7f88a86190", 0},

0 commit comments

Comments
 (0)