From 8d4f1d3f19968d2b9b692c91ec698f67d63f19e7 Mon Sep 17 00:00:00 2001 From: kevkevin Date: Tue, 21 May 2024 09:46:57 -0500 Subject: [PATCH] mutex --- handlers/bounty.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index d8a4879e1..8741fb51d 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -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{ @@ -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() @@ -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() @@ -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 } @@ -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 @@ -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) @@ -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()