diff --git a/pkg/controllers/transactions.go b/pkg/controllers/transactions.go index 67930a7..4416ec8 100644 --- a/pkg/controllers/transactions.go +++ b/pkg/controllers/transactions.go @@ -67,9 +67,10 @@ func (wc *TransactionController) GetAll(c *gin.Context) { fr := FilteredResponse(c) wallet, _ := c.GetQuery("walletId") - transactionStatusId, _ := c.GetQuery("transactionStatusId") + noPendingQry, _ := c.GetQuery("noPending") + noPending := noPendingQry != "" - wc.TransactionService.GetAll(c, body, wallet, fr, transactionStatusId) + wc.TransactionService.GetAll(c, body, wallet, fr, noPending) c.JSON(200, fr) } diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go index a9d3782..10a86dc 100644 --- a/pkg/middleware/cors.go +++ b/pkg/middleware/cors.go @@ -23,4 +23,4 @@ func CORSMiddleware() gin.HandlerFunc { c.Next() } -} +} \ No newline at end of file diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 35d0644..b0e3ff0 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -67,28 +67,24 @@ Gets all rows from subscription type table. *[]models.SubscriptionType: List of subscription type objects. */ // Gets filtered rows from transaction table. -func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, transactionStatusId string) { +func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, noPending bool) { db := as.Db.WithContext(ctx) wm := new([]models.Transaction) - sm := new([]models.Subscription) + transactionStatus := new(models.TransactionStatus) tx, _ := db.Begin() defer tx.Rollback() - query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) - if walletId != "" { - query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId) - } - if transactionStatusId != "" { - query2 = query2.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatusId) - } - query2.Select() + tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select() query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } + if noPending { + query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id) + } FilteredResponse(query, wm, filtered) @@ -109,15 +105,15 @@ Checks subscriptions and create transacitons. func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) { db := as.Db.WithContext(ctx) - transactionStatus := new(models.TransactionStatus) wm := new([]models.Transaction) sm := new([]models.Subscription) + transactionStatus := new(models.TransactionStatus) tx, _ := db.Begin() defer tx.Rollback() - tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select() - query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id) + tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select() + query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId) } @@ -133,6 +129,7 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet if walletId != "" { query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } + query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id) FilteredResponse(query, wm, filtered) diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 709fd17..8c0c043 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -128,10 +128,12 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI wallets := new([]models.WalletTransactions) transactions := new([]models.Transaction) subscriptions := new([]models.Subscription) + transactionStatus := new(models.TransactionStatus) tx, _ := db.Begin() defer tx.Rollback() + tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "completed").Select() query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType") if walletId != "" { query2.Where("? = ?", pg.Ident("wallet_id"), walletId) @@ -151,6 +153,7 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI if walletId != "" { query.Where("? = ?", pg.Ident("wallet_id"), walletId) } + query = query.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id) query.Select() tx.Commit()