mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fixed transaction generation
This commit is contained in:
@@ -48,3 +48,33 @@ func (cm *Subscription) ToTrans() *Transaction {
|
|||||||
trans.SubscriptionID = cm.Id
|
trans.SubscriptionID = cm.Id
|
||||||
return trans
|
return trans
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cm *Subscription) HasNew() bool {
|
||||||
|
trans := cm.TransactionType;
|
||||||
|
switch trans.Type {
|
||||||
|
case "monthly":
|
||||||
|
lastDate := time.Now().AddDate(0, -cm.CustomRange, 0)
|
||||||
|
if cm.LastTransactionDate.Before(lastDate) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
case "weekly":
|
||||||
|
lastDate := time.Now().AddDate(0, 0, -(7*cm.CustomRange))
|
||||||
|
if cm.LastTransactionDate.Before(lastDate) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
case "daily":
|
||||||
|
lastDate := time.Now().AddDate(0, 0, -cm.CustomRange)
|
||||||
|
if cm.LastTransactionDate.Before(lastDate) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
lastDate := time.Now().AddDate(-cm.CustomRange, 0, 0)
|
||||||
|
if cm.LastTransactionDate.Before(lastDate) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -75,7 +75,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
|
|||||||
for startDate.Before(stopDate) {
|
for startDate.Before(stopDate) {
|
||||||
trans := subModel.ToTrans()
|
trans := subModel.ToTrans()
|
||||||
trans.TransactionDate = startDate
|
trans.TransactionDate = startDate
|
||||||
if startDate.After(subModel.LastTransactionDate) {
|
if startDate.After(subModel.LastTransactionDate) && (startDate.Before(now) || startDate.Equal(now)) {
|
||||||
*transactions = append(*transactions, *trans)
|
*transactions = append(*transactions, *trans)
|
||||||
}
|
}
|
||||||
if subModel.SubscriptionType.Type == "monthly" {
|
if subModel.SubscriptionType.Type == "monthly" {
|
||||||
|
|||||||
@@ -63,7 +63,9 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
|
|||||||
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 {
|
for _, sub := range *subscriptions {
|
||||||
as.Ss.SubToTrans(&sub)
|
if sub.HasNew() {
|
||||||
|
as.Ss.SubToTrans(&sub)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
|
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
|
||||||
@@ -78,7 +80,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
|
|||||||
|
|
||||||
for i, wallet := range *wallets {
|
for i, wallet := range *wallets {
|
||||||
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) || trans.TransactionDate.Local().Equal(firstOfMonth) {
|
||||||
if trans.TransactionType.Type == "expense" {
|
if trans.TransactionType.Type == "expense" {
|
||||||
(*wallets)[i].CurrentBalance -= trans.Amount
|
(*wallets)[i].CurrentBalance -= trans.Amount
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user