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

Added Bounty Providers Endpoint #1591

Merged
merged 1 commit into from
Mar 6, 2024
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
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type Database interface {
GetPersonByPubkey(pubkey string) Person
GetBountiesByDateRange(r PaymentDateRange, re *http.Request) []Bounty
GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Request) int64
GetBountiesProviders(r PaymentDateRange, re *http.Request) []Person
PersonUniqueNameFromName(name string) (string, error)
ProcessAlerts(p Person)
UserHasAccess(pubKeyFromAuth string, uuid string, role string) bool
Expand Down
55 changes: 54 additions & 1 deletion db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request)
} else {
orderQuery = " ORDER BY " + sortBy + "" + "DESC"
}
if limit > 1 {
if limit > 0 {
kevkevinpal marked this conversation as resolved.
Show resolved Hide resolved
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}

Expand Down Expand Up @@ -247,3 +247,56 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ
db.db.Raw(allQuery).Scan(&count)
return count
}

func (db database) GetBountiesProviders(r PaymentDateRange, re *http.Request) []Person {
offset, limit, _, _, _ := utils.GetPaginationParams(re)
keys := re.URL.Query()
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
providers := keys.Get("provider")

var statusConditions []string

limitQuery := ""

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")
}

var statusQuery string
if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
} else {
statusQuery = ""
}

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

if limit > 0 {
limitQuery = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
}

bountyOwners := []BountyOwners{}
bountyProviders := []Person{}

query := `SELECT DISTINCT owner_id FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'` + providerCondition
allQuery := query + " " + statusQuery + " " + limitQuery
db.db.Raw(allQuery).Scan(&bountyOwners)

for _, owner := range bountyOwners {
person := db.GetPersonByPubkey(owner.OwnerID)
bountyProviders = append(bountyProviders, person)
}
return bountyProviders
}
4 changes: 4 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ type Bounty struct {
CodingLanguages pq.StringArray `gorm:"type:text[];not null default:'[]'" json:"coding_languages"`
}

type BountyOwners struct {
OwnerID string `json:"owner_id"`
}

type BountyData struct {
Bounty
BountyId uint `json:"bounty_id"`
Expand Down
26 changes: 26 additions & 0 deletions handlers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ func (mh *metricHandler) MetricsBountiesCount(w http.ResponseWriter, r *http.Req
json.NewEncoder(w).Encode(MetricsBountiesCount)
}

func (mh *metricHandler) MetricsBountiesProviders(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)

if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

request := db.PaymentDateRange{}
body, err := io.ReadAll(r.Body)
r.Body.Close()

err = json.Unmarshal(body, &request)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
json.NewEncoder(w).Encode("Request body not accepted")
return
}

bountiesProviders := mh.db.GetBountiesProviders(request, r)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountiesProviders)
}

func MetricsCsv(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
Expand Down
49 changes: 49 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion routes/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func MetricsRoutes() chi.Router {
r := chi.NewRouter()
mh := handlers.NewMetricHandler(db.DB)
r.Group(func(r chi.Router) {
// Todo: change auth to superadmin context
r.Use(auth.PubKeyContextSuperAdmin)

r.Post("/payment", handlers.PaymentMetrics)
Expand All @@ -20,6 +19,7 @@ func MetricsRoutes() chi.Router {
r.Post("/bounty_stats", mh.BountyMetrics)
r.Post("/bounties", mh.MetricsBounties)
r.Post("/bounties/count", mh.MetricsBountiesCount)
r.Post("/bounties/providers", mh.MetricsBountiesProviders)
r.Post("/csv", handlers.MetricsCsv)
})
return r
Expand Down
Loading