From 1100092586a4ec7092e103f37556d8a96b6be80c Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Thu, 29 Feb 2024 16:49:43 +0500 Subject: [PATCH] Added statuses query --- db/db.go | 25 +++++++++++++++- handlers/bounty_test.go | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/db/db.go b/db/db.go index e03d0cd27..9f64d7fb5 100644 --- a/db/db.go +++ b/db/db.go @@ -757,9 +757,32 @@ func (db database) GetCreatedBounties(r *http.Request) ([]Bounty, error) { uuid := chi.URLParam(r, "uuid") person := db.GetPersonByUuid(uuid) pubkey := person.OwnerPubKey + keys := r.URL.Query() + + open := keys.Get("Open") + assingned := keys.Get("Assigned") + paid := keys.Get("Paid") orderQuery := "" limitQuery := "" + var statusQuery string + var statusConditions []string + + if open == "true" { + statusConditions = append(statusConditions, "assignee = '' AND paid != true") + } + if assingned == "true" { + statusConditions = append(statusConditions, "assignee != '' AND paid = false") + } + if paid == "true" { + statusConditions = append(statusConditions, "paid = true") + } + + if len(statusConditions) > 0 { + statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")" + } else { + statusQuery = "" + } if sortBy != "" && direction != "" { orderQuery = "ORDER BY " + sortBy + " " + direction @@ -774,7 +797,7 @@ func (db database) GetCreatedBounties(r *http.Request) ([]Bounty, error) { ms := []Bounty{} query := `SELECT * FROM public.bounty WHERE owner_id = '` + pubkey + `'` - allQuery := query + " " + orderQuery + " " + limitQuery + allQuery := query + " " + statusQuery + " " + orderQuery + " " + limitQuery err := db.db.Raw(allQuery).Find(&ms).Error return ms, err diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 2a04813f9..c50d72cd0 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -712,6 +712,69 @@ func TestGetPersonCreatedBounties(t *testing.T) { assert.Empty(t, responseData) assert.Len(t, responseData, 0) }) + + t.Run("should filter bounties by status and apply pagination", func(t *testing.T) { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { + var bountyResponses []db.BountyResponse + + for _, bounty := range bounties { + owner := db.Person{ + ID: 1, + } + assignee := db.Person{ + ID: 1, + } + organization := db.OrganizationShort{ + Uuid: "uuid", + } + + bountyResponse := db.BountyResponse{ + Bounty: bounty, + Assignee: assignee, + Owner: owner, + Organization: organization, + } + bountyResponses = append(bountyResponses, bountyResponse) + } + + return bountyResponses + } + bHandler.generateBountyResponse = mockGenerateBountyResponse + + expectedBounties := []db.Bounty{ + {ID: 1, OwnerID: "user1", Assignee: "assignee1"}, + {ID: 2, OwnerID: "user1", Assignee: "assignee2", Paid: true}, + {ID: 3, OwnerID: "user1", Assignee: "", Paid: true}, + } + + mockDb.On("GetCreatedBounties", mock.Anything).Return(expectedBounties, nil).Once() + mockDb.On("GetPersonByPubkey", mock.Anything).Return(db.Person{}, nil) + mockDb.On("GetOrganizationByUuid", mock.Anything).Return(db.Organization{}, nil) + + rr := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/people/wanteds/created/uuid?Open=true&Assigned=true&Paid=true&offset=0&limit=2", nil) + req = req.WithContext(ctx) + if err != nil { + t.Fatal(err) + } + + bHandler.GetPersonCreatedBounties(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var responseData []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + + assert.Len(t, responseData, 3) + + // Assert that bounties are filtered correctly + assert.Equal(t, expectedBounties[0].ID, responseData[0].Bounty.ID) + assert.Equal(t, expectedBounties[1].ID, responseData[1].Bounty.ID) + assert.Equal(t, expectedBounties[2].ID, responseData[2].Bounty.ID) + }) } func TestGetNextBountyByCreated(t *testing.T) {