From 276771f34982b52360be8484e462f2a2fdc1416c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Sun, 20 Jun 2021 17:19:52 +0200 Subject: [PATCH] fixed subscription to transaction creation --- pkg/services/subscriptions.go | 10 ++-- pkg/services/wallets.go | 95 ++++++----------------------------- 2 files changed, 21 insertions(+), 84 deletions(-) diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index 3f6e6ea..bed7782 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -90,9 +90,11 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { } if len(*transactions) > 0 { - as.Db.Model(transactions).Insert() - subModel.LastTransactionDate = (*transactions)[len(*transactions)-1].TransactionDate - as.Db.Model(subModel).WherePK().Update() - + for _, trans := range *transactions { + _, err := as.Db.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert() + if err != nil { + as.Db.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update() + } + } } } diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 805db98..d08b5fb 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -47,26 +47,11 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin transactions := new([]models.Transaction) subscriptions := new([]models.Subscription) - wg.Add(1) - go func() { - defer wg.Done() - query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") - if walletId != "" { - query.Where("? = ?", pg.Ident("wallet_id"), walletId) - } - query.Select() - }() - wg.Add(1) - go func() { - defer wg.Done() - query2 := as.Db.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) - } - query2.Select() - }() - - wg.Wait() + query2 := as.Db.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) + } + query2.Select() now := time.Now() @@ -77,37 +62,21 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin 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 { + as.Ss.SubToTrans(&sub) + } + + query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") + if walletId != "" { + query.Where("? = ?", pg.Ident("wallet_id"), walletId) + } + query.Select() + for _, trans := range *transactions { addWhere(wallets, trans.WalletID, trans) } - for _, sub := range *subscriptions { - as.Ss.SubToTrans(&sub) - // startDate := sub.StartDate.Local() - // stopDate := firstOfMonthAfterNext - // if sub.HasEnd { - // stopDate = sub.EndDate.Local() - // } - // for startDate.Before(stopDate) { - // trans := sub.ToTrans() - // trans.TransactionDate = startDate - // addWhere(&wallets, sub.WalletID, *trans) - // if sub.SubscriptionType.Type == "monthly" { - // startDate = startDate.AddDate(0, sub.CustomRange, 0) - // } else if sub.SubscriptionType.Type == "weekly" { - // startDate = startDate.AddDate(0, 0, 7*sub.CustomRange) - // } else if sub.SubscriptionType.Type == "daily" { - // startDate = startDate.AddDate(0, 0, sub.CustomRange) - // } else { - // startDate = startDate.AddDate(sub.CustomRange, 0, 0) - // } - // } - } - for i, wallet := range *wallets { - // wg.Add(1) - // go func() { - // defer wg.Done() for _, trans := range wallet.Transactions { if trans.TransactionDate.Local().Before(firstOfNextMonth) && trans.TransactionDate.Local().After(firstOfMonth) { if trans.TransactionType.Type == "expense" { @@ -130,40 +99,8 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin } } - // }() } - // for _, sub := range *subscriptions { - // wg.Add(1) - // go func() { - // defer wg.Done() - // startDate := sub.StartDate - // now := time.Now() - // for startDate.Before(now) { - // if startDate.Before(firstOfNextMonth) && startDate.After(firstOfMonth) { - // if sub.TransactionType.Type == "expense" { - // subCurrentBalance -= sub.Amount - // } else { - // subCurrentBalance += sub.Amount - // } - // } else if startDate.Before(firstOfMonthAfterNext) && startDate.After(firstOfNextMonth) { - // if sub.TransactionType.Type == "expense" { - // subNextMonth -= sub.Amount - // } else { - // subNextMonth += sub.Amount - // } - // } else if startDate.Before(firstOfMonth) { - // if sub.TransactionType.Type == "expense" { - // subLastMonthBalance -= sub.Amount - // } else { - // subLastMonthBalance += sub.Amount - // } - // } - - // } - // }() - // } - wg.Wait() for _, wallet := range *wallets { @@ -175,8 +112,6 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin wm.Currency = "USD" wm.WalletId = walletId - //common.GenerateEmbed(query, embed).Select() - return wm }