Skip to content

Commit

Permalink
added transactions for budget withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Jun 10, 2024
1 parent 5a13079 commit 3616fc1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
17 changes: 13 additions & 4 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,22 @@ func (db database) AddAndUpdateBudget(invoice NewInvoiceList) NewPaymentHistory
}

func (db database) WithdrawBudget(sender_pubkey string, workspace_uuid string, amount uint) {
tx := db.db.Begin()
var err error

// get Workspace budget and add payment to total budget
WorkspaceBudget := db.GetWorkspaceBudget(workspace_uuid)
totalBudget := WorkspaceBudget.TotalBudget

newBudget := totalBudget - amount
db.db.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspace_uuid).Updates(map[string]interface{}{

if err = tx.Model(&NewBountyBudget{}).Where("workspace_uuid = ?", workspace_uuid).Updates(map[string]interface{}{
"total_budget": newBudget,
})
}).Error; err != nil {
tx.Rollback()
}

now := time.Now()

budgetHistory := NewPaymentHistory{
WorkspaceUuid: workspace_uuid,
Amount: amount,
Expand All @@ -360,7 +365,11 @@ func (db database) WithdrawBudget(sender_pubkey string, workspace_uuid string, a
ReceiverPubKey: "",
BountyId: 0,
}
db.AddPaymentHistory(budgetHistory)

if err = tx.Create(&budgetHistory).Error; err != nil {
tx.Rollback()
}
tx.Commit()
}

func (db database) AddPaymentHistory(payment NewPaymentHistory) NewPaymentHistory {
Expand Down
19 changes: 15 additions & 4 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,20 @@ func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Requ
body, err := io.ReadAll(r.Body)
r.Body.Close()

if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
h.m.Unlock()
return
}

err = json.Unmarshal(body, &request)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
h.m.Unlock()
return
}

log.Printf("[bounty] [BountyBudgetWithdraw] Logging body: orguuid: %s, pubkey: %s, invoice: %s", request.OrgUuid, pubKeyFromAuth, request.PaymentRequest)
log.Printf("[bounty] [BountyBudgetWithdraw] Logging body: workspace_uuid: %s, pubkey: %s, invoice: %s", request.OrgUuid, pubKeyFromAuth, request.PaymentRequest)

// check if user is the admin of the workspace
// or has a withdraw bounty budget role
Expand All @@ -644,8 +650,7 @@ func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Requ
}

amount := utils.GetInvoiceAmount(request.PaymentRequest)

if err == nil && amount > 0 {
if amount > 0 {
// check if the workspace bounty balance
// is greater than the amount
orgBudget := h.db.GetWorkspaceBudget(request.OrgUuid)
Expand Down Expand Up @@ -693,6 +698,12 @@ func (h *bountyHandler) NewBountyBudgetWithdraw(w http.ResponseWriter, r *http.R
body, err := io.ReadAll(r.Body)
r.Body.Close()

if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
h.m.Unlock()
return
}

err = json.Unmarshal(body, &request)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
Expand All @@ -713,7 +724,7 @@ func (h *bountyHandler) NewBountyBudgetWithdraw(w http.ResponseWriter, r *http.R

amount := utils.GetInvoiceAmount(request.PaymentRequest)

if err == nil && amount > 0 {
if amount > 0 {
// check if the workspace bounty balance
// is greater than the amount
orgBudget := h.db.GetWorkspaceBudget(request.WorkspaceUuid)
Expand Down

0 comments on commit 3616fc1

Please sign in to comment.