From f4583fff4cfab7df57734e87a18a496fcd11662f Mon Sep 17 00:00:00 2001 From: elraphty Date: Fri, 1 Mar 2024 14:47:15 +0100 Subject: [PATCH] added status to user assigned bounties --- db/db.go | 29 ++++++++++++++++++++++++++--- handlers/bounty_test.go | 4 +++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/db/db.go b/db/db.go index 9f64d7fb5..a3f18221b 100644 --- a/db/db.go +++ b/db/db.go @@ -731,14 +731,37 @@ func (db database) GetAssignedBounties(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 } else { - orderQuery = " ORDER BY paid ASC" + orderQuery = " ORDER BY created DESC" } if offset >= 0 && limit != 0 { limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset) @@ -747,7 +770,7 @@ func (db database) GetAssignedBounties(r *http.Request) ([]Bounty, error) { ms := []Bounty{} query := `SELECT * FROM public.bounty WHERE assignee = '` + pubkey + `' AND show != false` - allQuery := query + " " + orderQuery + " " + limitQuery + allQuery := query + " " + statusQuery + " " + orderQuery + " " + limitQuery err := db.db.Raw(allQuery).Find(&ms).Error return ms, err } @@ -787,7 +810,7 @@ func (db database) GetCreatedBounties(r *http.Request) ([]Bounty, error) { if sortBy != "" && direction != "" { orderQuery = "ORDER BY " + sortBy + " " + direction } else { - orderQuery = "ORDER BY paid ASC" + orderQuery = "ORDER BY created DESC" } if offset >= 0 && limit != 0 { diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 0ad1a846d..f8679c072 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -971,13 +971,15 @@ func GetPersonAssigned(t *testing.T) { expectedBounties := []db.Bounty{ {ID: 1, Assignee: "user1"}, {ID: 2, Assignee: "user1"}, + {ID: 3, OwnerID: "user2", Assignee: "user1"}, + {ID: 4, OwnerID: "user2", Assignee: "user1", Paid: true}, } mockDb.On("GetAssignedBounties", 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", "/wanteds/assigned/uuid", nil) + req, err := http.NewRequest("GET", "/wanteds/assigned/uuid?Assigned=true&Paid=true&offset=0&limit=4", nil) req = req.WithContext(ctx) if err != nil { t.Fatal(err)