Skip to content

Commit afaee94

Browse files
committed
fix: date filter now filters for the date
It filtered for an exact time match before which does not make a lot of sense.
1 parent 8df81e8 commit afaee94

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

api/docs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,12 @@ const docTemplate = `{
24002400
],
24012401
"summary": "Get transactions",
24022402
"parameters": [
2403+
{
2404+
"type": "string",
2405+
"description": "Date of the transaction. Ignores exact time, matches on the day of the RFC3339 timestamp provided.",
2406+
"name": "date",
2407+
"in": "query"
2408+
},
24032409
{
24042410
"type": "string",
24052411
"description": "Filter by amount",

api/swagger.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,12 @@
23882388
],
23892389
"summary": "Get transactions",
23902390
"parameters": [
2391+
{
2392+
"type": "string",
2393+
"description": "Date of the transaction. Ignores exact time, matches on the day of the RFC3339 timestamp provided.",
2394+
"name": "date",
2395+
"in": "query"
2396+
},
23912397
{
23922398
"type": "string",
23932399
"description": "Filter by amount",

api/swagger.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,11 @@ paths:
24862486
get:
24872487
description: Returns a list of transactions
24882488
parameters:
2489+
- description: Date of the transaction. Ignores exact time, matches on the day
2490+
of the RFC3339 timestamp provided.
2491+
in: query
2492+
name: date
2493+
type: string
24892494
- description: Filter by amount
24902495
in: query
24912496
name: amount

pkg/controllers/transaction.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type TransactionLinks struct {
3333
}
3434

3535
type TransactionQueryFilter struct {
36-
Date time.Time `form:"date"`
36+
Date time.Time `form:"date" filterField:"false"`
3737
Amount decimal.Decimal `form:"amount"`
3838
AmountLessOrEqual decimal.Decimal `form:"amountLessOrEqual" filterField:"false"` // Amount less than or equal to this
3939
AmountMoreOrEqual decimal.Decimal `form:"amountMoreOrEqual" filterField:"false"` // Amount more than or equal to this
@@ -76,7 +76,6 @@ func (f TransactionQueryFilter) ToCreate(c *gin.Context) (models.TransactionCrea
7676
}
7777

7878
return models.TransactionCreate{
79-
Date: f.Date,
8079
Amount: f.Amount,
8180
BudgetID: budgetID,
8281
SourceAccountID: sourceAccountID,
@@ -201,19 +200,19 @@ func (co Controller) CreateTransaction(c *gin.Context) {
201200
// @Failure 404
202201
// @Failure 500 {object} httperrors.HTTPError
203202
// @Router /v1/transactions [get]
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"
203+
// @Param date query string false "Date of the transaction. Ignores exact time, matches on the day of the RFC3339 timestamp provided."
204+
// @Param amount query string false "Filter by amount"
205+
// @Param amountLessOrEqual query string false "Amount less than or equal to this"
206+
// @Param amountMoreOrEqual query string false "Amount more than or equal to this"
207+
// @Param note query string false "Filter by note"
208+
// @Param budget query string false "Filter by budget ID"
209+
// @Param account query string false "Filter by ID of associated account, regardeless of source or destination"
210+
// @Param source query string false "Filter by source account ID"
211+
// @Param destination query string false "Filter by destination account ID"
212+
// @Param envelope query string false "Filter by envelope ID"
213+
// @Param reconciled query bool false "DEPRECATED. Filter by reconcilication state"
214+
// @Param reconciledSource query bool false "Reconcilication state in source account"
215+
// @Param reconciledDestination query bool false "Reconcilication state in destination account"
217216
func (co Controller) GetTransactions(c *gin.Context) {
218217
var filter TransactionQueryFilter
219218
if err := c.Bind(&filter); err != nil {
@@ -235,6 +234,12 @@ func (co Controller) GetTransactions(c *gin.Context) {
235234
TransactionCreate: create,
236235
}, queryFields...)
237236

237+
// Filter for the transaction being at the same date
238+
if !filter.Date.IsZero() {
239+
date := time.Date(filter.Date.Year(), filter.Date.Month(), filter.Date.Day(), 0, 0, 0, 0, time.UTC)
240+
query = query.Where("transactions.date >= date(?)", date).Where("transactions.date < date(?)", date.AddDate(0, 0, 1))
241+
}
242+
238243
if filter.AccountID != "" {
239244
accountID, ok := httputil.UUIDFromString(c, filter.AccountID)
240245
if !ok {

pkg/controllers/transaction_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ func (suite *TestSuiteStandard) TestGetTransactionsFilter() {
158158
query string
159159
len int
160160
}{
161-
{"Exact Date", fmt.Sprintf("date=%s", time.Date(2021, 2, 6, 5, 1, 0, 585, time.UTC).Format(time.RFC3339Nano)), 1},
161+
{"Exact Time", fmt.Sprintf("date=%s", time.Date(2021, 2, 6, 5, 1, 0, 585, time.UTC).Format(time.RFC3339Nano)), 1},
162+
{"Same date", fmt.Sprintf("date=%s", time.Date(2021, 2, 6, 7, 0, 0, 700, time.UTC).Format(time.RFC3339Nano)), 1},
162163
{"Exact Amount", fmt.Sprintf("amount=%s", decimal.NewFromFloat(2.718).String()), 2},
163164
{"Note", "note=Not important", 1},
164165
{"No note", "note=", 1},

0 commit comments

Comments
 (0)