fixed subscription to transaction creation

This commit is contained in:
Fran Jurmanović
2021-06-20 17:19:52 +02:00
parent c251015497
commit 276771f349
2 changed files with 21 additions and 84 deletions

View File

@@ -90,9 +90,11 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
} }
if len(*transactions) > 0 { if len(*transactions) > 0 {
as.Db.Model(transactions).Insert() for _, trans := range *transactions {
subModel.LastTransactionDate = (*transactions)[len(*transactions)-1].TransactionDate _, err := as.Db.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert()
as.Db.Model(subModel).WherePK().Update() if err != nil {
as.Db.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update()
}
}
} }
} }

View File

@@ -47,26 +47,11 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
transactions := new([]models.Transaction) transactions := new([]models.Transaction)
subscriptions := new([]models.Subscription) subscriptions := new([]models.Subscription)
wg.Add(1) query2 := as.Db.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
go func() { if walletId != "" {
defer wg.Done() query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") }
if walletId != "" { query2.Select()
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()
now := time.Now() 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) firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 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 { for _, trans := range *transactions {
addWhere(wallets, trans.WalletID, trans) 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 { for i, wallet := range *wallets {
// wg.Add(1)
// go func() {
// defer wg.Done()
for _, trans := range wallet.Transactions { for _, trans := range wallet.Transactions {
if trans.TransactionDate.Local().Before(firstOfNextMonth) && trans.TransactionDate.Local().After(firstOfMonth) { if trans.TransactionDate.Local().Before(firstOfNextMonth) && trans.TransactionDate.Local().After(firstOfMonth) {
if trans.TransactionType.Type == "expense" { 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() wg.Wait()
for _, wallet := range *wallets { for _, wallet := range *wallets {
@@ -175,8 +112,6 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
wm.Currency = "USD" wm.Currency = "USD"
wm.WalletId = walletId wm.WalletId = walletId
//common.GenerateEmbed(query, embed).Select()
return wm return wm
} }