implemented wallet headers

This commit is contained in:
Fran Jurmanović
2021-06-10 00:13:35 +02:00
parent 636a367d3e
commit 0fc69a0af2
7 changed files with 113 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package services
import (
"time"
"wallet-api/pkg/models"
"github.com/go-pg/pg/v10"
@@ -13,11 +14,18 @@ type TransactionService struct {
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
tm := new(models.Transaction)
amount, _ := body.Amount.Int64()
tm.Init()
tm.WalletID = body.WalletID
tm.TransactionTypeID = body.TransactionTypeID
tm.Description = body.Description
tm.TransactionDate = body.TransactionDate
tm.Amount = int(amount)
if body.TransactionDate.IsZero() {
tm.TransactionDate = time.Now()
}
as.Db.Model(tm).Insert()

View File

@@ -1,6 +1,7 @@
package services
import (
"time"
"wallet-api/pkg/models"
"wallet-api/pkg/utl/common"
@@ -36,3 +37,51 @@ func (as *WalletService) GetAll(am *models.Auth, filtered *models.FilteredRespon
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
FilteredResponse(query, wm, filtered)
}
func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId string) *models.WalletHeader {
wm := new(models.WalletHeader)
//wallets := new([]models.Wallet)
transactions := new([]models.Transaction)
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
if walletId != "" {
query.Where("? = ?", pg.Ident("wallet_id"), walletId)
}
query.Select()
currentBalance := 0
lastMonthBalance := 0
nextMonth := 0
now := time.Now()
currentYear, currentMonth, _ := now.Date()
currentLocation := now.Location()
firstOfMonth := time.Date(currentYear, currentMonth, 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)
for _, trans := range *transactions {
if trans.TransactionDate.Before(firstOfNextMonth) && trans.TransactionDate.After(firstOfMonth) {
currentBalance += trans.Amount
} else if trans.TransactionDate.Before(firstOfMonthAfterNext) && trans.TransactionDate.After(firstOfNextMonth) {
nextMonth += trans.Amount
} else if trans.TransactionDate.Before(firstOfMonth) {
lastMonthBalance += trans.Amount
currentBalance += trans.Amount
}
}
wm.CurrentBalance = currentBalance
wm.LastMonth = lastMonthBalance
wm.NextMonth = currentBalance + nextMonth
wm.Currency = "USD"
wm.WalletId = walletId
//common.GenerateEmbed(query, embed).Select()
return wm
}