diff --git a/pkg/models/subscriptions.go b/pkg/models/subscriptions.go index 65a1a86..5384216 100644 --- a/pkg/models/subscriptions.go +++ b/pkg/models/subscriptions.go @@ -50,31 +50,34 @@ func (cm *Subscription) ToTrans() *Transaction { } 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 + trans := cm.TransactionType + if trans != nil { + 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 } - 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 } + return true } \ No newline at end of file diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index 3d6d6bb..1db9553 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -33,9 +33,13 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub tm.StartDate = time.Now() } - as.Db.Model(tm).Insert() + tx, _ := as.Db.Begin() + defer tx.Rollback() - as.SubToTrans(tm) + tx.Model(tm).Insert() + + as.SubToTrans(tm, tx) + tx.Commit() return tm } @@ -43,22 +47,24 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) { wm := new([]models.Subscription) - query := as.Db.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) + tx, _ := as.Db.Begin() + defer tx.Rollback() + + query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } for _, sub := range *wm { if sub.HasNew() { - as.SubToTrans(&sub) + as.SubToTrans(&sub, tx) } } FilteredResponse(query, wm, filtered) + tx.Commit() } -func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { - tx, _ := as.Db.Begin() - defer tx.Rollback() +func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) { now := time.Now() @@ -107,5 +113,4 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { } } } - tx.Commit() } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 4580e0c..d11015a 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -48,13 +48,16 @@ func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered query2.Select() for _, sub := range *sm { - as.Ss.SubToTrans(&sub) + if sub.HasNew() { + as.Ss.SubToTrans(&sub, tx) + } } query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id) if walletId != "" { query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) } + FilteredResponse(query, wm, filtered) tx.Commit() diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 48d183e..462eb92 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -67,7 +67,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal for _, sub := range *subscriptions { if sub.HasNew() { - as.Ss.SubToTrans(&sub) + as.Ss.SubToTrans(&sub, tx) } }