Skip to content

Commit

Permalink
Added statuses query
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulWahab3181 committed Feb 29, 2024
1 parent 8a7957b commit 1100092
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
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

0 comments on commit 1100092

Please sign in to comment.