Skip to content

Commit

Permalink
mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal committed May 21, 2024
1 parent e8a7555 commit 8d4f1d3
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type bountyHandler struct {
userHasManageBountyRoles func(pubKeyFromAuth string, uuid string) bool
}

var m sync.Mutex

func NewBountyHandler(httpClient HttpClient, database db.Database) *bountyHandler {
dbConf := db.NewDatabaseConfig(&gorm.DB{})
return &bountyHandler{
Expand Down Expand Up @@ -447,7 +449,6 @@ func (h *bountyHandler) GenerateBountyResponse(bounties []db.NewBounty) []db.Bou
}

func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request) {
var m sync.Mutex
m.Lock()

ctx := r.Context()
Expand Down Expand Up @@ -584,7 +585,6 @@ func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request
}

func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Request) {
var m sync.Mutex
m.Lock()

ctx := r.Context()
Expand All @@ -600,10 +600,9 @@ func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Requ
body, err := io.ReadAll(r.Body)
r.Body.Close()

merr := json.Unmarshal(body, &request)
if merr != nil {
err = json.Unmarshal(body, &request)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
log.Println("Could not marshal WithdrawRequest")
return
}

Expand All @@ -615,7 +614,6 @@ func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Requ
hasRole := h.userHasAccess(pubKeyFromAuth, request.OrgUuid, db.WithdrawBudget)
if !hasRole {
w.WriteHeader(http.StatusUnauthorized)
log.Printf("User %s on Workspace %s doesn't have appropriate permissions to withdraw bounty budget", pubKeyFromAuth, request.OrgUuid)
errMsg := formatPayError("You don't have appropriate permissions to withdraw bounty budget")
json.NewEncoder(w).Encode(errMsg)
return
Expand All @@ -637,16 +635,13 @@ func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Requ
if paymentSuccess.Success {
// withdraw amount from workspace budget
h.db.WithdrawBudget(pubKeyFromAuth, request.OrgUuid, amount)
log.Printf("Paid Lightning invoice %s and withdrew from workspace %s budget", request.PaymentRequest, request.OrgUuid)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(paymentSuccess)
} else {
log.Printf("Could not pay Lightning invoice %s", request.PaymentRequest)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(paymentError)
}
} else {
log.Printf("Budget Balance Error amount %d exceeds Workspace %s budget balance", amount, request.OrgUuid)
w.WriteHeader(http.StatusForbidden)
errMsg := formatPayError("Could not pay lightning invoice")
json.NewEncoder(w).Encode(errMsg)
Expand All @@ -657,7 +652,6 @@ func (h *bountyHandler) BountyBudgetWithdraw(w http.ResponseWriter, r *http.Requ

// Todo: change back to NewBountyBudgetWithdraw
func (h *bountyHandler) NewBountyBudgetWithdraw(w http.ResponseWriter, r *http.Request) {
var m sync.Mutex
m.Lock()

ctx := r.Context()
Expand Down

0 comments on commit 8d4f1d3

Please sign in to comment.