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

[SuperAdmin] Add optional word clause to query for providers #1552

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request)
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
providers := keys.Get("provider")

orderQuery := ""
limitQuery := ""
Expand Down Expand Up @@ -193,7 +194,13 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request)
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}

query := `SELECT * FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'`
providerCondition := ""
if len(providers) > 0 {
providerSlice := strings.Split(providers, ",")
providerCondition = " AND owner_id IN ('" + strings.Join(providerSlice, "','") + "')"
}

query := `SELECT * FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'` + providerCondition
allQuery := query + " " + statusQuery + " " + orderQuery + " " + limitQuery

b := []Bounty{}
Expand All @@ -206,6 +213,7 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
providers := keys.Get("provider")

var statusConditions []string

Expand All @@ -226,9 +234,15 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ
statusQuery = ""
}

providerCondition := ""
if len(providers) > 0 {
providerSlice := strings.Split(providers, ",")
providerCondition = " AND owner_id IN ('" + strings.Join(providerSlice, "','") + "')"
}

var count int64

query := `SELECT COUNT(*) FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'`
query := `SELECT COUNT(*) FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'` + providerCondition
allQuery := query + " " + statusQuery
db.db.Raw(allQuery).Scan(&count)
return count
Expand Down
88 changes: 87 additions & 1 deletion handlers/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"testing"

"fmt"
"time"

"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"
"github.com/stretchr/testify/assert"
"time"
)

func TestBountyMetrics(t *testing.T) {
Expand Down Expand Up @@ -173,6 +174,64 @@ func TestMetricsBounties(t *testing.T) {
assert.Equal(t, res[0].BountyDescription, "test bounty")
assert.Equal(t, res[0].BountyCreated, int64(1112))
})

t.Run("should fetch bounties from db for selected providers", func(t *testing.T) {
db.RedisError = errors.New("redis not initialized")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(mh.MetricsBounties)
dateRange := db.PaymentDateRange{
StartDate: "1111",
EndDate: "2222",
}
body, _ := json.Marshal(dateRange)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/boutnies", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}

// Provide multiple provider IDs in the request query parameters
req.URL.RawQuery = "provider=provider1&provider=provider2&provider=provider3"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FaisalIqbal211 you have not updated the test to accept, a comma-separated provider string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elraphty Addressed


// Mock bounties data for multiple providers
bounties := []db.Bounty{
{
ID: 1,
OwnerID: "provider1",
Price: 100,
Title: "bounty 1",
Description: "test bounty",
Created: 1112,
},
{
ID: 2,
OwnerID: "provider2",
Price: 100,
Title: "bounty 2",
Description: "test bounty",
Created: 1112,
},
}
// Mock the database call to return bounties for the selected providers
mockDb.On("GetBountiesByDateRange", dateRange, req).Return(bounties).Once()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to mock this amount of Providers, the you don't have this amount of DB calls in the request handler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

mockDb.On("GetPersonByPubkey", "provider1").Return(db.Person{ID: 1}).Once()
mockDb.On("GetPersonByPubkey", "").Return(db.Person{}).Once()
mockDb.On("GetOrganizationByUuid", "").Return(db.Organization{}).Once()
mockDb.On("GetPersonByPubkey", "provider2").Return(db.Person{ID: 2}).Once()
mockDb.On("GetPersonByPubkey", "").Return(db.Person{}).Once()
mockDb.On("GetOrganizationByUuid", "").Return(db.Organization{}).Once()

handler.ServeHTTP(rr, req)

var res []db.BountyData
_ = json.Unmarshal(rr.Body.Bytes(), &res)

assert.Equal(t, http.StatusOK, rr.Code)

// Assert that the response contains bounties only from the selected providers
for _, bounty := range res {
assert.Contains(t, []string{"provider1", "provider2"}, bounty.OwnerID)
}
})
}

func TestMetricsBountiesCount(t *testing.T) {
Expand Down Expand Up @@ -233,6 +292,33 @@ func TestMetricsBountiesCount(t *testing.T) {
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, res, int64(100))
})

t.Run("should fetch bounties count within specified date range for selected providers", func(t *testing.T) {
db.RedisError = errors.New("redis not initialized")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(mh.MetricsBountiesCount)
dateRange := db.PaymentDateRange{
StartDate: "1111",
EndDate: "2222",
}
body, _ := json.Marshal(dateRange)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/boutnies/count", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}

// Provide provider IDs in the request query parameters
req.URL.RawQuery = "provider=provider1"

mockDb.On("GetBountiesByDateRangeCount", dateRange, req).Return(int64(50)).Once()
handler.ServeHTTP(rr, req)

var res int64
_ = json.Unmarshal(rr.Body.Bytes(), &res)

assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, res, int64(50))
})
}

func TestConvertMetricsToCSV(t *testing.T) {
Expand Down
Loading