Skip to content

Commit ed79e15

Browse files
committed
feat: add date filtering with fromDate and untilDate
1 parent afaee94 commit ed79e15

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

api/docs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,6 +2406,18 @@ const docTemplate = `{
24062406
"name": "date",
24072407
"in": "query"
24082408
},
2409+
{
2410+
"type": "string",
2411+
"description": "Transactions at and after this date. Ignores exact time, matches on the day of the RFC3339 timestamp provided.",
2412+
"name": "fromDate",
2413+
"in": "query"
2414+
},
2415+
{
2416+
"type": "string",
2417+
"description": "Transactions before and at this date. Ignores exact time, matches on the day of the RFC3339 timestamp provided.",
2418+
"name": "untilDate",
2419+
"in": "query"
2420+
},
24092421
{
24102422
"type": "string",
24112423
"description": "Filter by amount",

api/swagger.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,18 @@
23942394
"name": "date",
23952395
"in": "query"
23962396
},
2397+
{
2398+
"type": "string",
2399+
"description": "Transactions at and after this date. Ignores exact time, matches on the day of the RFC3339 timestamp provided.",
2400+
"name": "fromDate",
2401+
"in": "query"
2402+
},
2403+
{
2404+
"type": "string",
2405+
"description": "Transactions before and at this date. Ignores exact time, matches on the day of the RFC3339 timestamp provided.",
2406+
"name": "untilDate",
2407+
"in": "query"
2408+
},
23972409
{
23982410
"type": "string",
23992411
"description": "Filter by amount",

api/swagger.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,16 @@ paths:
24912491
in: query
24922492
name: date
24932493
type: string
2494+
- description: Transactions at and after this date. Ignores exact time, matches
2495+
on the day of the RFC3339 timestamp provided.
2496+
in: query
2497+
name: fromDate
2498+
type: string
2499+
- description: Transactions before and at this date. Ignores exact time, matches
2500+
on the day of the RFC3339 timestamp provided.
2501+
in: query
2502+
name: untilDate
2503+
type: string
24942504
- description: Filter by amount
24952505
in: query
24962506
name: amount

pkg/controllers/transaction.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type TransactionLinks struct {
3434

3535
type TransactionQueryFilter struct {
3636
Date time.Time `form:"date" filterField:"false"`
37+
FromDate time.Time `form:"fromDate" filterField:"false"`
38+
UntilDate time.Time `form:"untilDate" filterField:"false"`
3739
Amount decimal.Decimal `form:"amount"`
3840
AmountLessOrEqual decimal.Decimal `form:"amountLessOrEqual" filterField:"false"` // Amount less than or equal to this
3941
AmountMoreOrEqual decimal.Decimal `form:"amountMoreOrEqual" filterField:"false"` // Amount more than or equal to this
@@ -201,6 +203,8 @@ func (co Controller) CreateTransaction(c *gin.Context) {
201203
// @Failure 500 {object} httperrors.HTTPError
202204
// @Router /v1/transactions [get]
203205
// @Param date query string false "Date of the transaction. Ignores exact time, matches on the day of the RFC3339 timestamp provided."
206+
// @Param fromDate query string false "Transactions at and after this date. Ignores exact time, matches on the day of the RFC3339 timestamp provided."
207+
// @Param untilDate query string false "Transactions before and at this date. Ignores exact time, matches on the day of the RFC3339 timestamp provided."
204208
// @Param amount query string false "Filter by amount"
205209
// @Param amountLessOrEqual query string false "Amount less than or equal to this"
206210
// @Param amountMoreOrEqual query string false "Amount more than or equal to this"
@@ -240,6 +244,14 @@ func (co Controller) GetTransactions(c *gin.Context) {
240244
query = query.Where("transactions.date >= date(?)", date).Where("transactions.date < date(?)", date.AddDate(0, 0, 1))
241245
}
242246

247+
if !filter.FromDate.IsZero() {
248+
query = query.Where("transactions.date >= date(?)", time.Date(filter.FromDate.Year(), filter.FromDate.Month(), filter.FromDate.Day(), 0, 0, 0, 0, time.UTC))
249+
}
250+
251+
if !filter.UntilDate.IsZero() {
252+
query = query.Where("transactions.date <= date(?)", time.Date(filter.UntilDate.Year(), filter.UntilDate.Month(), filter.UntilDate.Day(), 0, 0, 0, 0, time.UTC))
253+
}
254+
243255
if filter.AccountID != "" {
244256
accountID, ok := httputil.UUIDFromString(c, filter.AccountID)
245257
if !ok {

pkg/controllers/transaction_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ func (suite *TestSuiteStandard) TestGetTransactionsFilter() {
160160
}{
161161
{"Exact Time", fmt.Sprintf("date=%s", time.Date(2021, 2, 6, 5, 1, 0, 585, time.UTC).Format(time.RFC3339Nano)), 1},
162162
{"Same date", fmt.Sprintf("date=%s", time.Date(2021, 2, 6, 7, 0, 0, 700, time.UTC).Format(time.RFC3339Nano)), 1},
163+
{"After date", fmt.Sprintf("fromDate=%s", time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)), 2},
164+
{"Before date", fmt.Sprintf("untilDate=%s", time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)), 1},
165+
{"After all dates", fmt.Sprintf("fromDate=%s", time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)), 0},
166+
{"Before all dates", fmt.Sprintf("untilDate=%s", time.Date(2010, 8, 17, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)), 0},
167+
{"Between two dates", fmt.Sprintf("untilDate=%s&fromDate=%s", time.Date(2019, 8, 17, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano), time.Date(2015, 12, 24, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)), 2},
168+
{"Impossible between two dates", fmt.Sprintf("fromDate=%s&untilDate=%s", time.Date(2019, 8, 17, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano), time.Date(2015, 12, 24, 0, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)), 0},
163169
{"Exact Amount", fmt.Sprintf("amount=%s", decimal.NewFromFloat(2.718).String()), 2},
164170
{"Note", "note=Not important", 1},
165171
{"No note", "note=", 1},

0 commit comments

Comments
 (0)