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 Time-Based Filtering for Workspace Bounty Cards #2320

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
10 changes: 9 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,14 @@ func (db database) GetWorkspaceBountyCardsData(r *http.Request) []NewBounty {
limitQuery := ""
searchQuery := ""
workspaceQuery := ""
timeFilterQuery := ""

timeFilterQuery = `
AND (
(NOT paid AND EXTRACT(EPOCH FROM updated::timestamp) > EXTRACT(EPOCH FROM (NOW() - INTERVAL '4 weeks')))
OR (paid AND EXTRACT(EPOCH FROM updated::timestamp) > EXTRACT(EPOCH FROM (NOW() - INTERVAL '2 weeks')))
OR updated IS NULL -- Preserve existing records without updated timestamp
)`

if sortBy != "" && direction != "" {
orderQuery = "ORDER BY " + sortBy + " " + direction
Expand All @@ -2007,7 +2015,7 @@ func (db database) GetWorkspaceBountyCardsData(r *http.Request) []NewBounty {
}

query := "SELECT * FROM public.bounty"
allQuery := query + " " + workspaceQuery + " " + searchQuery + " " + orderQuery + " " + limitQuery
allQuery := query + " " + workspaceQuery + timeFilterQuery + " " + searchQuery + " " + orderQuery + " " + limitQuery

ms := []NewBounty{}
db.db.Raw(allQuery).Scan(&ms)
Expand Down
145 changes: 145 additions & 0 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3029,6 +3029,9 @@ func TestGetWorkspaceBountyCards(t *testing.T) {
Price: 2000,
}

fiveWeeksAgo := now.Add(-5 * 7 * 24 * time.Hour)
threeWeeksAgo := now.Add(-3 * 7 * 24 * time.Hour)

t.Run("should only get public bounty", func(t *testing.T) {
db.TestDB.DeleteAllBounties()
_, err := db.TestDB.CreateOrEditBounty(publicBounty)
Expand Down Expand Up @@ -3070,4 +3073,146 @@ func TestGetWorkspaceBountyCards(t *testing.T) {
assert.Equal(t, 1, len(response))
assert.Equal(t, "Private Bounty", response[0].Title)
})

t.Run("should include recent unpaid bounty", func(t *testing.T) {
db.TestDB.DeleteAllBounties()

recentUnpaidBounty := db.NewBounty{
ID: 1,
Type: "coding",
Title: "Recent Unpaid",
Description: "Test Description",
WorkspaceUuid: workspace.Uuid,
PhaseUuid: phase.Uuid,
Assignee: assignee.OwnerPubKey,
Show: true,
Created: now.Unix(),
OwnerID: "test-owner",
Price: 1000,
Updated: &now,
Paid: false,
}
_, err := db.TestDB.CreateOrEditBounty(recentUnpaidBounty)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyCards)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/gobounties/bounty-cards?workspace_uuid=%s", workspace.Uuid), nil)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)

var response []db.BountyCard
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, 1, len(response))
assert.Equal(t, "Recent Unpaid", response[0].Title)
})

t.Run("should include recent paid bounty", func(t *testing.T) {
db.TestDB.DeleteAllBounties()

recentPaidBounty := db.NewBounty{
ID: 1,
Type: "coding",
Title: "Recent Paid",
Description: "Test Description",
WorkspaceUuid: workspace.Uuid,
PhaseUuid: phase.Uuid,
Assignee: assignee.OwnerPubKey,
Show: true,
Created: now.Unix(),
OwnerID: "test-owner",
Price: 1000,
Updated: &now,
Paid: true,
}
_, err := db.TestDB.CreateOrEditBounty(recentPaidBounty)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyCards)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/gobounties/bounty-cards?workspace_uuid=%s", workspace.Uuid), nil)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)

var response []db.BountyCard
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, 1, len(response))
assert.Equal(t, "Recent Paid", response[0].Title)
})

t.Run("should exclude old unpaid bounty", func(t *testing.T) {
db.TestDB.DeleteAllBounties()

oldUnpaidBounty := db.NewBounty{
ID: 1,
Type: "coding",
Title: "Old Unpaid",
Description: "Test Description",
WorkspaceUuid: workspace.Uuid,
PhaseUuid: phase.Uuid,
Assignee: assignee.OwnerPubKey,
Show: true,
Created: fiveWeeksAgo.Unix(),
OwnerID: "test-owner",
Price: 1000,
Updated: &fiveWeeksAgo,
Paid: false,
}
_, err := db.TestDB.CreateOrEditBounty(oldUnpaidBounty)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyCards)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/gobounties/bounty-cards?workspace_uuid=%s", workspace.Uuid), nil)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)

var response []db.BountyCard
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, 0, len(response))
})

t.Run("should exclude old paid bounty", func(t *testing.T) {
db.TestDB.DeleteAllBounties()

oldPaidBounty := db.NewBounty{
ID: 1,
Type: "coding",
Title: "Old Paid",
Description: "Test Description",
WorkspaceUuid: workspace.Uuid,
PhaseUuid: phase.Uuid,
Assignee: assignee.OwnerPubKey,
Show: true,
Created: threeWeeksAgo.Unix(),
OwnerID: "test-owner",
Price: 1000,
Updated: &threeWeeksAgo,
Paid: true,
}
_, err := db.TestDB.CreateOrEditBounty(oldPaidBounty)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBountyCards)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/gobounties/bounty-cards?workspace_uuid=%s", workspace.Uuid), nil)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)

var response []db.BountyCard
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, 0, len(response))
})
}
Loading