From 4f667c9034dec2908e851b323f14fdbe9bdbe4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Wed, 27 Oct 2021 23:49:26 +0200 Subject: [PATCH] check subscriptions and create pending transactions --- pkg/controllers/transactions.go | 24 +++++++++++++++++- pkg/services/subscriptions.go | 14 +++-------- pkg/services/transactions.go | 43 ++++++++++++++++++++++++++++++++- pkg/services/wallets.go | 6 ----- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/pkg/controllers/transactions.go b/pkg/controllers/transactions.go index 6e2294d..819ca80 100644 --- a/pkg/controllers/transactions.go +++ b/pkg/controllers/transactions.go @@ -30,6 +30,7 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou s.GET("", wc.GetAll) s.PUT("/:id", wc.Edit) s.GET("/:id", wc.Get) + s.GET("check", wc.Check) return wc } @@ -65,7 +66,28 @@ func (wc *TransactionController) GetAll(c *gin.Context) { fr := FilteredResponse(c) wallet, _ := c.GetQuery("walletId") - wc.TransactionService.GetAll(c, body, wallet, fr) + transactionStatusId, _ := c.GetQuery("transactionStatusId") + + wc.TransactionService.GetAll(c, body, wallet, fr, transactionStatusId) + + c.JSON(200, fr) +} + +/* +Check + Args: + *gin.Context: Gin Application Context +*/ +// ROUTE (GET /transactions) +func (wc *TransactionController) Check(c *gin.Context) { + body := new(models.Auth) + auth := c.MustGet("auth") + body.Id = auth.(*models.Auth).Id + + fr := FilteredResponse(c) + wallet, _ := c.GetQuery("walletId") + + wc.TransactionService.Check(c, body, wallet, fr) c.JSON(200, fr) } diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index 2fcbcec..aca02b1 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -51,8 +51,6 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip defer tx.Rollback() tx.Model(tm).Insert() - - as.SubToTrans(tm, tx) tx.Commit() return tm @@ -82,10 +80,6 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri qry := tx.Model(wm) common.GenerateEmbed(qry, params.Embed).WherePK().Select() - if (*wm).HasNew() { - as.SubToTrans(wm, tx) - } - tx.Commit() return wm @@ -114,11 +108,6 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } - for _, sub := range *wm { - if sub.HasNew() { - as.SubToTrans(&sub, tx) - } - } FilteredResponse(query, wm, filtered) tx.Commit() } @@ -202,7 +191,9 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg. currentYear, currentMonth, _ := now.Date() currentLocation := now.Location() + transactionStatus := new(models.TransactionStatus) firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation) + tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select() //tzFirstOfNextMonth := firstOfNextMonth.In(subModel.StartDate.Location()) startDate := subModel.StartDate @@ -222,6 +213,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg. for startDate.Before(stopDate) { trans := subModel.ToTrans() trans.TransactionDate = startDate + trans.TransactionStatusID = transactionStatus.Id if startDate.After(subModel.LastTransactionDate) && (startDate.Before(now) || startDate.Equal(now)) { *transactions = append(*transactions, *trans) } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 8715219..d0fa4d8 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -67,7 +67,7 @@ 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) { +func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, transactionStatusId string) { db := as.Db.WithContext(ctx) wm := new([]models.Transaction) @@ -80,6 +80,47 @@ func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walle if walletId != "" { query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId) } + if transactionStatusId != "" { + query2 = query2.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatusId) + } + query2.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) + } + + FilteredResponse(query, wm, filtered) + + tx.Commit() +} + +/* +Check + +Checks subscriptions and create transacitons. + Args: + context.Context: Application context + string: Relations to embed + Returns: + *[]models.SubscriptionType: List of subscription type objects. +*/ +// Gets filtered rows from transaction table. +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) + + 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) + if walletId != "" { + query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId) + } query2.Select() for _, sub := range *sm { diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index ecd0627..709fd17 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -147,12 +147,6 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation) firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation) - for _, sub := range *subscriptions { - if sub.HasNew() { - as.Ss.SubToTrans(&sub, tx) - } - } - query := tx.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") if walletId != "" { query.Where("? = ?", pg.Ident("wallet_id"), walletId)