diff --git a/db/metrics.go b/db/metrics.go index 8e75479e6..9a33bdbbe 100644 --- a/db/metrics.go +++ b/db/metrics.go @@ -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 := "" @@ -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{} @@ -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 @@ -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 diff --git a/handlers/metrics_test.go b/handlers/metrics_test.go index 85eeb4dee..7fc0de3f4 100644 --- a/handlers/metrics_test.go +++ b/handlers/metrics_test.go @@ -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) { @@ -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) { @@ -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) {