check subscriptions and create pending transactions

This commit is contained in:
Fran Jurmanović
2021-10-27 23:49:26 +02:00
parent efc3c3214e
commit 4f667c9034
4 changed files with 68 additions and 19 deletions

View File

@@ -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 {