Skip to content

Commit

Permalink
Merge pull request #1512 from AbdulWahab3181/fix/get-bounties-date-ra…
Browse files Browse the repository at this point in the history
…nge-queries

Enable correct filtering when status is selected in superadmin
  • Loading branch information
elraphty authored Feb 14, 2024
2 parents 1cc053c + 523e67f commit d37c4e0
Showing 1 changed file with 32 additions and 42 deletions.
74 changes: 32 additions & 42 deletions db/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"net/http"
"strings"

"github.com/stakwork/sphinx-tribes/utils"
)
Expand Down Expand Up @@ -161,31 +162,26 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request)
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")

openQuery := ""
assignedQuery := ""
paidQuery := ""
orderQuery := ""
limitQuery := ""

if open != "" && open == "true" {
openQuery = "AND assignee = '' AND paid != true"
assignedQuery = ""
var statusConditions []string

if open == "true" {
statusConditions = append(statusConditions, "assignee = '' AND paid != true")
}
if assingned == "true" {
statusConditions = append(statusConditions, "assignee != '' AND paid = false")
}
if assingned != "" && assingned == "true" {
if open != "" && open == "true" {
assignedQuery = "OR assignee != '' AND paid != true"
} else {
assignedQuery = "AND assignee != '' AND paid != true"
}
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if paid != "" && paid == "true" {
if open != "" && open == "true" || assingned != "" && assingned == "true" {
paidQuery = "OR paid = true"
} else if open != "" && open == "true" && assingned == "" && assingned != "true" {
assignedQuery = ""
} else {
paidQuery = "AND paid = true"
}

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

if sortBy != "" && direction != "" {
Expand All @@ -198,7 +194,7 @@ func (db database) GetBountiesByDateRange(r PaymentDateRange, re *http.Request)
}

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

b := []Bounty{}
db.db.Raw(allQuery).Find(&b)
Expand All @@ -211,35 +207,29 @@ func (db database) GetBountiesByDateRangeCount(r PaymentDateRange, re *http.Requ
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")

openQuery := ""
assignedQuery := ""
paidQuery := ""
var statusConditions []string

if open != "" && open == "true" {
openQuery = "AND assignee = '' AND paid != true"
assignedQuery = ""
if open == "true" {
statusConditions = append(statusConditions, "assignee = '' AND paid != true")
}
if assingned != "" && assingned == "true" {
if open != "" && open == "true" {
assignedQuery = "OR assignee != '' AND paid = false"
} else {
assignedQuery = "AND assignee != '' AND paid = false"
}
if assingned == "true" {
statusConditions = append(statusConditions, "assignee != '' AND paid = false")
}
if paid != "" && paid == "true" {
if open != "" && open == "true" || assingned != "" && assingned == "true" {
paidQuery = "OR paid = true"
} else if open != "" && open == "true" && assingned == "" && assingned != "true" {
assignedQuery = ""
} else {
paidQuery = "AND paid = true"
}
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}

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

var count int64

query := `SELECT COUNT(*) FROM public.bounty WHERE created >= '` + r.StartDate + `' AND created <= '` + r.EndDate + `'`
allQuery := query + " " + openQuery + " " + assignedQuery + " " + paidQuery
allQuery := query + " " + statusQuery
db.db.Raw(allQuery).Scan(&count)
return count
}

0 comments on commit d37c4e0

Please sign in to comment.