Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status filters to query to call bounty statuses for created bounties when viewing profile #1575

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
63 changes: 63 additions & 0 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading