fixed wallet not displaying next month correctly

This commit is contained in:
Fran Jurmanović
2021-07-05 20:39:26 +02:00
parent 29bcbb6e91
commit 1228925881
2 changed files with 27 additions and 7 deletions

View File

@@ -77,11 +77,11 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.
currentLocation := now.Location()
firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
tzFirstOfNextMonth := firstOfNextMonth.In(subModel.StartDate.Location())
//tzFirstOfNextMonth := firstOfNextMonth.In(subModel.StartDate.Location())
startDate := subModel.StartDate
stopDate := tzFirstOfNextMonth
if subModel.HasEnd && subModel.EndDate.Before(tzFirstOfNextMonth) {
stopDate := firstOfNextMonth
if subModel.HasEnd && subModel.EndDate.Before(firstOfNextMonth) {
stopDate = subModel.EndDate
}

View File

@@ -2,7 +2,6 @@ package services
import (
"context"
"sync"
"time"
"wallet-api/pkg/models"
"wallet-api/pkg/utl/common"
@@ -50,7 +49,6 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
wm := new(models.WalletHeader)
wallets := new([]models.WalletTransactions)
var wg sync.WaitGroup
transactions := new([]models.Transaction)
subscriptions := new([]models.Subscription)
@@ -85,6 +83,30 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
query.Select()
tx.Commit()
for _, sub := range *subscriptions {
stopDate := firstOfMonthAfterNext
if sub.HasEnd && sub.EndDate.Before(firstOfMonthAfterNext) {
stopDate = sub.EndDate
}
startDate := sub.StartDate
for startDate.Before(stopDate) {
trans := sub.ToTrans()
trans.TransactionDate = startDate
if startDate.After(firstOfNextMonth) || startDate.Equal(firstOfNextMonth) {
*transactions = append(*transactions, *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 _, trans := range *transactions {
addWhere(wallets, trans.WalletID, trans)
}
@@ -117,8 +139,6 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
}
}
wg.Wait()
for _, wallet := range *wallets {
wm.LastMonth += wallet.LastMonth
wm.CurrentBalance += wallet.CurrentBalance + wallet.LastMonth