Skip to content

Commit 855bd10

Browse files
committed
feat: add ability to filter transactions by amount greater/smaller than a parameter
1 parent 6bb1ff8 commit 855bd10

File tree

5 files changed

+80
-11
lines changed

5 files changed

+80
-11
lines changed

api/docs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,24 @@ const docTemplate = `{
24002400
],
24012401
"summary": "Get transactions",
24022402
"parameters": [
2403+
{
2404+
"type": "string",
2405+
"description": "Filter by amount",
2406+
"name": "amount",
2407+
"in": "query"
2408+
},
2409+
{
2410+
"type": "string",
2411+
"description": "Amount less than or equal to this",
2412+
"name": "amountLessOrEqual",
2413+
"in": "query"
2414+
},
2415+
{
2416+
"type": "string",
2417+
"description": "Amount more than or equal to this",
2418+
"name": "amountMoreOrEqual",
2419+
"in": "query"
2420+
},
24032421
{
24042422
"type": "string",
24052423
"description": "Filter by note",

api/swagger.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,24 @@
23882388
],
23892389
"summary": "Get transactions",
23902390
"parameters": [
2391+
{
2392+
"type": "string",
2393+
"description": "Filter by amount",
2394+
"name": "amount",
2395+
"in": "query"
2396+
},
2397+
{
2398+
"type": "string",
2399+
"description": "Amount less than or equal to this",
2400+
"name": "amountLessOrEqual",
2401+
"in": "query"
2402+
},
2403+
{
2404+
"type": "string",
2405+
"description": "Amount more than or equal to this",
2406+
"name": "amountMoreOrEqual",
2407+
"in": "query"
2408+
},
23912409
{
23922410
"type": "string",
23932411
"description": "Filter by note",

api/swagger.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,18 @@ paths:
24862486
get:
24872487
description: Returns a list of transactions
24882488
parameters:
2489+
- description: Filter by amount
2490+
in: query
2491+
name: amount
2492+
type: string
2493+
- description: Amount less than or equal to this
2494+
in: query
2495+
name: amountLessOrEqual
2496+
type: string
2497+
- description: Amount more than or equal to this
2498+
in: query
2499+
name: amountMoreOrEqual
2500+
type: string
24892501
- description: Filter by note
24902502
in: query
24912503
name: note

pkg/controllers/transaction.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type TransactionLinks struct {
3535
type TransactionQueryFilter struct {
3636
Date time.Time `form:"date"`
3737
Amount decimal.Decimal `form:"amount"`
38+
AmountLessOrEqual decimal.Decimal `form:"amountLessOrEqual" filterField:"false"` // Amount less than or equal to this
39+
AmountMoreOrEqual decimal.Decimal `form:"amountMoreOrEqual" filterField:"false"` // Amount more than or equal to this
3840
Note string `form:"note" filterField:"false"`
3941
BudgetID string `form:"budget"`
4042
SourceAccountID string `form:"source"`
@@ -199,17 +201,19 @@ func (co Controller) CreateTransaction(c *gin.Context) {
199201
// @Failure 404
200202
// @Failure 500 {object} httperrors.HTTPError
201203
// @Router /v1/transactions [get]
202-
// @Param date query time.Time false "Filter by date"
203-
// @Param amount query decimal.Decimal false "Filter by amount"
204-
// @Param note query string false "Filter by note"
205-
// @Param budget query string false "Filter by budget ID"
206-
// @Param account query string false "Filter by ID of associated account, regardeless of source or destination"
207-
// @Param source query string false "Filter by source account ID"
208-
// @Param destination query string false "Filter by destination account ID"
209-
// @Param envelope query string false "Filter by envelope ID"
210-
// @Param reconciled query bool false "DEPRECATED. Filter by reconcilication state"
211-
// @Param reconciledSource query bool false "Reconcilication state in source account"
212-
// @Param reconciledDestination query bool false "Reconcilication state in destination account"
204+
// @Param date query time.Time false "Filter by date"
205+
// @Param amount query string false "Filter by amount"
206+
// @Param amountLessOrEqual query string false "Amount less than or equal to this"
207+
// @Param amountMoreOrEqual query string false "Amount more than or equal to this"
208+
// @Param note query string false "Filter by note"
209+
// @Param budget query string false "Filter by budget ID"
210+
// @Param account query string false "Filter by ID of associated account, regardeless of source or destination"
211+
// @Param source query string false "Filter by source account ID"
212+
// @Param destination query string false "Filter by destination account ID"
213+
// @Param envelope query string false "Filter by envelope ID"
214+
// @Param reconciled query bool false "DEPRECATED. Filter by reconcilication state"
215+
// @Param reconciledSource query bool false "Reconcilication state in source account"
216+
// @Param reconciledDestination query bool false "Reconcilication state in destination account"
213217
func (co Controller) GetTransactions(c *gin.Context) {
214218
var filter TransactionQueryFilter
215219
if err := c.Bind(&filter); err != nil {
@@ -248,6 +252,14 @@ func (co Controller) GetTransactions(c *gin.Context) {
248252
})
249253
}
250254

255+
if !filter.AmountLessOrEqual.IsZero() {
256+
query = query.Where("transactions.amount <= ?", filter.AmountLessOrEqual)
257+
}
258+
259+
if !filter.AmountMoreOrEqual.IsZero() {
260+
query = query.Where("transactions.amount >= ?", filter.AmountMoreOrEqual)
261+
}
262+
251263
if filter.Note != "" {
252264
query = query.Where("note LIKE ?", fmt.Sprintf("%%%s%%", filter.Note))
253265
} else if slices.Contains(setFields, "Note") {

pkg/controllers/transaction_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ func (suite *TestSuiteStandard) TestGetTransactionsFilter() {
174174
{"Non-existing Account", "account=534a3562-c5e8-46d1-a2e2-e96c00e7efec", 0},
175175
{"Existing Account 2", fmt.Sprintf("account=%s", a2.Data.ID), 3},
176176
{"Existing Account 1", fmt.Sprintf("account=%s", a1.Data.ID), 2},
177+
{"Amount less or equal to 2.71", "amountLessOrEqual=2.71", 0},
178+
{"Amount less or equal to 2.718", "amountLessOrEqual=2.718", 2},
179+
{"Amount less or equal to 1000", "amountLessOrEqual=1000", 2},
180+
{"Amount more or equal to 2.718", "amountMoreOrEqual=2.718", 3},
181+
{"Amount more or equal to 11235.813", "amountMoreOrEqual=11235.813", 1},
182+
{"Amount more or equal to 99999", "amountMoreOrEqual=99999", 0},
183+
{"Amount more or equal to 100", "amountMoreOrEqual=100", 1},
184+
{"Amount more or equal to 100 and less than 10", "amountMoreOrEqual=100&amountLessOrEqual=10", 0},
185+
{"Amount more or equal to 1 and less than 3", "amountMoreOrEqual=1&amountLessOrEqual=3", 2},
177186
}
178187

179188
for _, tt := range tests {

0 commit comments

Comments
 (0)