@@ -12,6 +12,7 @@ import (
1212 "github.com/gin-gonic/gin"
1313 "github.com/google/uuid"
1414 "github.com/shopspring/decimal"
15+ "gorm.io/gorm"
1516)
1617
1718type TransactionListResponse struct {
@@ -40,6 +41,7 @@ type TransactionQueryFilter struct {
4041 DestinationAccountID string `form:"destination"`
4142 EnvelopeID string `form:"envelope"`
4243 Reconciled bool `form:"reconciled"`
44+ AccountID string `form:"account" createField:"false"`
4345}
4446
4547func (f TransactionQueryFilter ) ToCreate (c * gin.Context ) (models.TransactionCreate , error ) {
@@ -196,6 +198,7 @@ func CreateTransaction(c *gin.Context) {
196198// @Param amount query decimal.Decimal false "Filter by amount"
197199// @Param note query string false "Filter by note"
198200// @Param budget query string false "Filter by budget ID"
201+ // @Param account query string false "Filter by ID of associated account, regardeless of source or destination"
199202// @Param source query string false "Filter by source account ID"
200203// @Param destination query string false "Filter by destination account ID"
201204// @Param envelope query string false "Filter by envelope ID"
@@ -216,10 +219,30 @@ func GetTransactions(c *gin.Context) {
216219 return
217220 }
218221
219- var transactions []models. Transaction
220- database .DB .Order ("date(date) DESC" ).Where (& models.Transaction {
222+ var query * gorm. DB
223+ query = database .DB .Order ("date(date) DESC" ).Where (& models.Transaction {
221224 TransactionCreate : create ,
222- }, queryFields ... ).Find (& transactions )
225+ }, queryFields ... )
226+
227+ if filter .AccountID != "" {
228+ accountID , err := httputil .UUIDFromString (c , filter .AccountID )
229+ if err != nil {
230+ return
231+ }
232+
233+ query = query .Where (& models.Transaction {
234+ TransactionCreate : models.TransactionCreate {
235+ SourceAccountID : accountID ,
236+ },
237+ }).Or (& models.Transaction {
238+ TransactionCreate : models.TransactionCreate {
239+ DestinationAccountID : accountID ,
240+ },
241+ })
242+ }
243+
244+ var transactions []models.Transaction
245+ query .Find (& transactions )
223246
224247 // When there are no resources, we want an empty list, not null
225248 // Therefore, we use make to create a slice with zero elements
0 commit comments