mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
check subscriptions and create pending transactions
This commit is contained in:
@@ -30,6 +30,7 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou
|
||||
s.GET("", wc.GetAll)
|
||||
s.PUT("/:id", wc.Edit)
|
||||
s.GET("/:id", wc.Get)
|
||||
s.GET("check", wc.Check)
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -65,7 +66,28 @@ func (wc *TransactionController) GetAll(c *gin.Context) {
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
wc.TransactionService.GetAll(c, body, wallet, fr)
|
||||
transactionStatusId, _ := c.GetQuery("transactionStatusId")
|
||||
|
||||
wc.TransactionService.GetAll(c, body, wallet, fr, transactionStatusId)
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
|
||||
/*
|
||||
Check
|
||||
Args:
|
||||
*gin.Context: Gin Application Context
|
||||
*/
|
||||
// ROUTE (GET /transactions)
|
||||
func (wc *TransactionController) Check(c *gin.Context) {
|
||||
body := new(models.Auth)
|
||||
auth := c.MustGet("auth")
|
||||
body.Id = auth.(*models.Auth).Id
|
||||
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
wc.TransactionService.Check(c, body, wallet, fr)
|
||||
|
||||
c.JSON(200, fr)
|
||||
}
|
||||
|
||||
@@ -51,8 +51,6 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
|
||||
defer tx.Rollback()
|
||||
|
||||
tx.Model(tm).Insert()
|
||||
|
||||
as.SubToTrans(tm, tx)
|
||||
tx.Commit()
|
||||
|
||||
return tm
|
||||
@@ -82,10 +80,6 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri
|
||||
qry := tx.Model(wm)
|
||||
common.GenerateEmbed(qry, params.Embed).WherePK().Select()
|
||||
|
||||
if (*wm).HasNew() {
|
||||
as.SubToTrans(wm, tx)
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return wm
|
||||
@@ -114,11 +108,6 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
|
||||
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
|
||||
for _, sub := range *wm {
|
||||
if sub.HasNew() {
|
||||
as.SubToTrans(&sub, tx)
|
||||
}
|
||||
}
|
||||
FilteredResponse(query, wm, filtered)
|
||||
tx.Commit()
|
||||
}
|
||||
@@ -202,7 +191,9 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.
|
||||
currentYear, currentMonth, _ := now.Date()
|
||||
currentLocation := now.Location()
|
||||
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
firstOfNextMonth := time.Date(currentYear, currentMonth+1, 1, 0, 0, 0, 0, currentLocation)
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select()
|
||||
//tzFirstOfNextMonth := firstOfNextMonth.In(subModel.StartDate.Location())
|
||||
|
||||
startDate := subModel.StartDate
|
||||
@@ -222,6 +213,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.
|
||||
for startDate.Before(stopDate) {
|
||||
trans := subModel.ToTrans()
|
||||
trans.TransactionDate = startDate
|
||||
trans.TransactionStatusID = transactionStatus.Id
|
||||
if startDate.After(subModel.LastTransactionDate) && (startDate.Before(now) || startDate.Equal(now)) {
|
||||
*transactions = append(*transactions, *trans)
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ Gets all rows from subscription type table.
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, transactionStatusId string) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.Transaction)
|
||||
@@ -80,6 +80,47 @@ func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walle
|
||||
if walletId != "" {
|
||||
query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
if transactionStatusId != "" {
|
||||
query2 = query2.Where("? = ?", pg.Ident("transaction_status_id"), transactionStatusId)
|
||||
}
|
||||
query2.Select()
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
/*
|
||||
Check
|
||||
|
||||
Checks subscriptions and create transacitons.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
transactionStatus := new(models.TransactionStatus)
|
||||
wm := new([]models.Transaction)
|
||||
sm := new([]models.Subscription)
|
||||
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
tx.Model(transactionStatus).Where("? = ?", pg.Ident("status"), "pending").Select()
|
||||
|
||||
query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Where("? = ?", pg.Ident("transaction_status_id"), transactionStatus.Id)
|
||||
if walletId != "" {
|
||||
query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
query2.Select()
|
||||
|
||||
for _, sub := range *sm {
|
||||
|
||||
@@ -147,12 +147,6 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
|
||||
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 _, sub := range *subscriptions {
|
||||
if sub.HasNew() {
|
||||
as.Ss.SubToTrans(&sub, tx)
|
||||
}
|
||||
}
|
||||
|
||||
query := tx.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
|
||||
if walletId != "" {
|
||||
query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
|
||||
Reference in New Issue
Block a user