Skip to content

Commit

Permalink
Merge pull request #1541 from wright-eric/fix/update-bounty
Browse files Browse the repository at this point in the history
fix: do not update created at on edit bounty
  • Loading branch information
elraphty authored Feb 21, 2024
2 parents c3272cf + 61dab61 commit fd91a43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
15 changes: 8 additions & 7 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
)

type bountyHandler struct {
httpClient HttpClient
db db.Database
httpClient HttpClient
db db.Database
generateBountyResponse func(bounties []db.Bounty) []db.BountyResponse
}

func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler {
return &bountyHandler{
httpClient: httpClient,
db: db,
httpClient: httpClient,
db: db,
generateBountyResponse: GenerateBountyResponse,
}
}
Expand Down Expand Up @@ -188,9 +188,6 @@ func (h *bountyHandler) CreateOrEditBounty(w http.ResponseWriter, r *http.Reques

//Check if bounty exists
bounty.Updated = &now
if bounty.Created == 0 {
bounty.Created = time.Now().Unix()
}

if bounty.Type == "" {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -227,6 +224,10 @@ func (h *bountyHandler) CreateOrEditBounty(w http.ResponseWriter, r *http.Reques
h.db.UpdateBountyNullColumn(bounty, "assignee")
}

if bounty.ID == 0 && bounty.Created == 0 {
bounty.Created = time.Now().Unix()
}

if bounty.Title != "" && bounty.ID != 0 {
// get bounty from DB
dbBounty := h.db.GetBounty(bounty.ID)
Expand Down
42 changes: 42 additions & 0 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/auth"
Expand Down Expand Up @@ -191,6 +192,47 @@ func TestCreateOrEditBounty(t *testing.T) {
mockDb.AssertExpectations(t)
})

t.Run("should not update created at when bounty is updated", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.CreateOrEditBounty)
now := time.Now().UnixMilli()
mockOrg := db.Organization{
ID: 1,
Uuid: "org-1",
Name: "custom org",
OwnerPubKey: "org-key",
}
existingBounty := db.Bounty{
ID: 1,
Type: "coding",
Title: "first bounty",
Description: "first bounty description",
OrgUuid: "org-1",
OwnerID: "second-user",
Created: now,
}
updatedBounty := existingBounty
updatedBounty.Title = "first bounty updated"
mockDb.On("UpdateBountyBoolColumn", mock.AnythingOfType("db.Bounty"), "show").Return(existingBounty)
mockDb.On("UpdateBountyNullColumn", mock.AnythingOfType("db.Bounty"), "assignee").Return(existingBounty)
mockDb.On("GetBounty", uint(1)).Return(existingBounty).Once()
mockDb.On("UserHasManageBountyRoles", "test-key", mockOrg.Uuid).Return(true).Once()
mockDb.On("CreateOrEditBounty", mock.MatchedBy(func(b db.Bounty) bool {
return b.Created == now
})).Return(updatedBounty, nil).Once()

body, _ := json.Marshal(updatedBounty)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
})

t.Run("should return error if failed to add new bounty", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.CreateOrEditBounty)
Expand Down

0 comments on commit fd91a43

Please sign in to comment.