Skip to content

Commit

Permalink
Merge pull request #1552 from FaisalIqbal211/GHI-1547
Browse files Browse the repository at this point in the history
[SuperAdmin] Add optional word clause to query for providers
  • Loading branch information
elraphty authored Feb 26, 2024
2 parents 0c4e506 + 3de4da0 commit 4479847
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
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,provider2"

// 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()
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

0 comments on commit 4479847

Please sign in to comment.