Skip to content

Commit

Permalink
Merge pull request #1954 from stakwork/fix/gorm_falsify
Browse files Browse the repository at this point in the history
PR: Updated ProcessBountyPayment function
  • Loading branch information
elraphty authored Nov 15, 2024
2 parents d054a05 + 3aedcb4 commit 27c7a69
Show file tree
Hide file tree
Showing 6 changed files with 955 additions and 489 deletions.
15 changes: 15 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,21 @@ func (db database) GetBounty(id uint) NewBounty {
return b
}

func (db database) UpdateBountyPaymentStatuses(bounty NewBounty) (NewBounty, error) {

bountyUpdates := map[string]interface{}{
"paid": bounty.Paid,
"payment_pending": bounty.PaymentPending,
"payment_failed": bounty.PaymentFailed,
"completed": bounty.Completed,
"paid_date": bounty.PaidDate,
"completion_date": bounty.CompletionDate,
}

db.db.Model(&NewBounty{}).Where("created", bounty.Created).Updates(bountyUpdates)
return bounty, nil
}

func (db database) UpdateBounty(b NewBounty) (NewBounty, error) {
db.db.Where("created", b.Created).Updates(&b)
return b, nil
Expand Down
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Database interface {
GetBountyByCreated(created uint) (NewBounty, error)
GetBounty(id uint) NewBounty
UpdateBounty(b NewBounty) (NewBounty, error)
UpdateBountyPaymentStatuses(bounty NewBounty) (NewBounty, error)
UpdateBountyPayment(b NewBounty) (NewBounty, error)
GetListedOffers(r *http.Request) ([]PeopleExtra, error)
UpdateBot(uuid string, u map[string]interface{}) bool
Expand Down
11 changes: 10 additions & 1 deletion db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,17 @@ func (db database) ProcessBountyPayment(payment NewPaymentHistory, bounty NewBou
return err
}

bountyUpdates := map[string]interface{}{
"paid": bounty.Paid,
"payment_pending": bounty.PaymentPending,
"payment_failed": bounty.PaymentFailed,
"completed": bounty.Completed,
"paid_date": bounty.PaidDate,
"completion_date": bounty.CompletionDate,
}

// updatge bounty status
if err = tx.Where("created", bounty.Created).Updates(&bounty).Error; err != nil {
if err = tx.Model(&NewBounty{}).Where("created", bounty.Created).Updates(bountyUpdates).Error; err != nil {
tx.Rollback()
return err
}
Expand Down
7 changes: 5 additions & 2 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,13 @@ func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request

// if the payment has a completed status
if v2KeysendRes.Status == db.PaymentComplete {
bounty.Paid = true
bounty.PaymentFailed = false
bounty.PaymentPending = false
bounty.Paid = true
bounty.PaidDate = &now
bounty.Completed = true
bounty.CompletionDate = &now

paymentHistory.Status = true
paymentHistory.PaymentStatus = db.PaymentComplete
paymentHistory.Tag = v2KeysendRes.Tag
Expand Down Expand Up @@ -725,6 +726,7 @@ func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request
bounty.PaidDate = &now
bounty.Completed = true
bounty.CompletionDate = &now

paymentHistory.Status = true
paymentHistory.PaymentStatus = db.PaymentPending
paymentHistory.Tag = v2KeysendRes.Tag
Expand Down Expand Up @@ -998,11 +1000,12 @@ func (h *bountyHandler) UpdateBountyPaymentStatus(w http.ResponseWriter, r *http
bounty.PaymentPending = false
bounty.PaymentFailed = false
bounty.Paid = true

bounty.PaidDate = &now
bounty.Completed = true
bounty.CompletionDate = &now

h.db.UpdateBounty(bounty)
h.db.UpdateBountyPaymentStatuses(bounty)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(msg)
Expand Down
5 changes: 3 additions & 2 deletions handlers/v2_payments_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func InitV2PaymentsCron() {
if bounty.ID > 0 {
now := time.Now()

bounty.Paid = true
bounty.PaymentPending = false
bounty.PaymentFailed = false
bounty.Paid = true

bounty.PaidDate = &now
bounty.Completed = true
bounty.CompletionDate = &now

db.DB.UpdateBounty(bounty)
db.DB.UpdateBountyPaymentStatuses(bounty)
}
} else if tagResult.Status == db.PaymentFailed {
// Handle failed payments
Expand Down
Loading

0 comments on commit 27c7a69

Please sign in to comment.