diff --git a/handlers/bounty.go b/handlers/bounty.go index 3c3f186ae..60d9f501c 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -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, } } @@ -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) @@ -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) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 83f6bb7db..86e856ce4 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -11,6 +11,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" "github.com/go-chi/chi" "github.com/stakwork/sphinx-tribes/auth" @@ -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)