mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
Merge branch 'feature/seperate-transactions' into develop
This commit is contained in:
@@ -50,7 +50,8 @@ func (cm *Subscription) ToTrans() *Transaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cm *Subscription) HasNew() bool {
|
func (cm *Subscription) HasNew() bool {
|
||||||
trans := cm.TransactionType;
|
trans := cm.TransactionType
|
||||||
|
if trans != nil {
|
||||||
switch trans.Type {
|
switch trans.Type {
|
||||||
case "monthly":
|
case "monthly":
|
||||||
lastDate := time.Now().AddDate(0, -cm.CustomRange, 0)
|
lastDate := time.Now().AddDate(0, -cm.CustomRange, 0)
|
||||||
@@ -77,4 +78,6 @@ func (cm *Subscription) HasNew() bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
@@ -33,9 +33,13 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
|
|||||||
tm.StartDate = time.Now()
|
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
|
return tm
|
||||||
}
|
}
|
||||||
@@ -43,20 +47,25 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
|
|||||||
func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
func (as *SubscriptionService) GetAll(am *models.Auth, walletId string, filtered *models.FilteredResponse) {
|
||||||
wm := new([]models.Subscription)
|
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 != "" {
|
if walletId != "" {
|
||||||
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sub := range *wm {
|
for _, sub := range *wm {
|
||||||
if sub.HasNew() {
|
if sub.HasNew() {
|
||||||
as.SubToTrans(&sub)
|
as.SubToTrans(&sub, tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FilteredResponse(query, wm, filtered)
|
FilteredResponse(query, wm, filtered)
|
||||||
|
tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
|
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) {
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
currentYear, currentMonth, _ := now.Date()
|
currentYear, currentMonth, _ := now.Date()
|
||||||
@@ -75,7 +84,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
|
|||||||
|
|
||||||
if subModel.SubscriptionType == nil {
|
if subModel.SubscriptionType == nil {
|
||||||
st := new(models.SubscriptionType)
|
st := new(models.SubscriptionType)
|
||||||
as.Db.Model(st).Where("? = ?", pg.Ident("id"), subModel.SubscriptionTypeID).Select()
|
tx.Model(st).Where("? = ?", pg.Ident("id"), subModel.SubscriptionTypeID).Select()
|
||||||
subModel.SubscriptionType = st
|
subModel.SubscriptionType = st
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,9 +107,9 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
|
|||||||
|
|
||||||
if len(*transactions) > 0 {
|
if len(*transactions) > 0 {
|
||||||
for _, trans := range *transactions {
|
for _, trans := range *transactions {
|
||||||
_, err := as.Db.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert()
|
_, err := tx.Model(&trans).Where("? = ?", pg.Ident("transaction_date"), trans.TransactionDate).Where("? = ?", pg.Ident("subscription_id"), trans.SubscriptionID).OnConflict("DO NOTHING").SelectOrInsert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
as.Db.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update()
|
tx.Model(subModel).Set("? = ?", pg.Ident("last_transaction_date"), trans.TransactionDate).WherePK().Update()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,19 +38,27 @@ func (as *TransactionService) GetAll(am *models.Auth, walletId string, filtered
|
|||||||
wm := new([]models.Transaction)
|
wm := new([]models.Transaction)
|
||||||
sm := new([]models.Subscription)
|
sm := new([]models.Subscription)
|
||||||
|
|
||||||
query2 := as.Db.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
tx, _ := as.Db.Begin()
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
query2 := tx.Model(sm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||||
if walletId != "" {
|
if walletId != "" {
|
||||||
query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
query2 = query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
}
|
}
|
||||||
query2.Select()
|
query2.Select()
|
||||||
|
|
||||||
for _, sub := range *sm {
|
for _, sub := range *sm {
|
||||||
as.Ss.SubToTrans(&sub)
|
if sub.HasNew() {
|
||||||
|
as.Ss.SubToTrans(&sub, tx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := as.Db.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
query := tx.Model(wm).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id)
|
||||||
if walletId != "" {
|
if walletId != "" {
|
||||||
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
}
|
}
|
||||||
|
|
||||||
FilteredResponse(query, wm, filtered)
|
FilteredResponse(query, wm, filtered)
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models
|
|||||||
check := new(models.User)
|
check := new(models.User)
|
||||||
exceptionReturn := new(models.Exception)
|
exceptionReturn := new(models.Exception)
|
||||||
|
|
||||||
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
tx, _ := us.Db.Begin()
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
tx.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||||
if check.Username != "" || check.Email != "" {
|
if check.Username != "" || check.Email != "" {
|
||||||
exceptionReturn.Message = "User already exists"
|
exceptionReturn.Message = "User already exists"
|
||||||
exceptionReturn.ErrorCode = "400101"
|
exceptionReturn.ErrorCode = "400101"
|
||||||
@@ -33,7 +36,7 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models
|
|||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|
||||||
registerBody.Password = string(hashedPassword)
|
registerBody.Password = string(hashedPassword)
|
||||||
_, err = us.Db.Model(registerBody).Insert()
|
_, err = tx.Model(registerBody).Insert()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exceptionReturn.Message = "Error creating user"
|
exceptionReturn.Message = "Error creating user"
|
||||||
@@ -41,6 +44,8 @@ func (us *UsersService) Create(registerBody *models.User) (*models.User, *models
|
|||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
return registerBody, exceptionReturn
|
return registerBody, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +89,10 @@ func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse,
|
|||||||
me := new(models.Exception)
|
me := new(models.Exception)
|
||||||
um := new(models.User)
|
um := new(models.User)
|
||||||
|
|
||||||
err := us.Db.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
tx, _ := us.Db.Begin()
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
err := tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
me.ErrorCode = "404101"
|
me.ErrorCode = "404101"
|
||||||
@@ -93,7 +101,7 @@ func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse,
|
|||||||
return mm, me
|
return mm, me
|
||||||
}
|
}
|
||||||
um.IsActive = false
|
um.IsActive = false
|
||||||
_, err = us.Db.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update()
|
_, err = tx.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
me.ErrorCode = "400105"
|
me.ErrorCode = "400105"
|
||||||
@@ -104,6 +112,8 @@ func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse,
|
|||||||
|
|
||||||
mm.Message = "User successfully deactivated."
|
mm.Message = "User successfully deactivated."
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
return mm, me
|
return mm, me
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,10 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
|
|||||||
transactions := new([]models.Transaction)
|
transactions := new([]models.Transaction)
|
||||||
subscriptions := new([]models.Subscription)
|
subscriptions := new([]models.Subscription)
|
||||||
|
|
||||||
query2 := as.Db.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
|
tx, _ := as.Db.Begin()
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
query2 := tx.Model(subscriptions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType").Relation("SubscriptionType")
|
||||||
if walletId != "" {
|
if walletId != "" {
|
||||||
query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
query2.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
}
|
}
|
||||||
@@ -64,11 +67,11 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
|
|||||||
|
|
||||||
for _, sub := range *subscriptions {
|
for _, sub := range *subscriptions {
|
||||||
if sub.HasNew() {
|
if sub.HasNew() {
|
||||||
as.Ss.SubToTrans(&sub)
|
as.Ss.SubToTrans(&sub, tx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
|
query := tx.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
|
||||||
if walletId != "" {
|
if walletId != "" {
|
||||||
query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
}
|
}
|
||||||
@@ -117,6 +120,8 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
|
|||||||
wm.Currency = "USD"
|
wm.Currency = "USD"
|
||||||
wm.WalletId = walletId
|
wm.WalletId = walletId
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
return wm
|
return wm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user