Skip to content

Commit

Permalink
Merge pull request #1911 from stakwork/feat/add_pending_failed_paymen…
Browse files Browse the repository at this point in the history
…ts_search

PR: Filter bounties with failed and pending payments
  • Loading branch information
elraphty authored Nov 1, 2024
2 parents b92bde7 + 731d5b2 commit 00d1ad1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 60 deletions.
163 changes: 103 additions & 60 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,38 +447,6 @@ func makeExtrasListQuery(columnName string) string {
`
}

func makePersonExtrasListQuery(columnName string) string {
// this is safe because columnName is not provided by the user, its hard-coded in db.go
return `SELECT
json_build_object('owner_pubkey', owner_pub_key, 'owner_alias', owner_alias, 'img', img, 'unique_name', unique_name, 'id', id, '` + columnName + `', extras->'` + columnName + `', 'github_issues', github_issues) #>> '{}' as person,
arr.item_object as body
FROM people,
jsonb_array_elements(extras->'` + columnName + `') with ordinality
arr(item_object, position)
WHERE arr.item_object->'assignee'->>'owner_pubkey' = ?
AND LOWER(arr.item_object->>'title') LIKE ?
AND CASE
WHEN arr.item_object->>'show' = 'false' THEN false
ELSE true
END`
}

func addNewerThanXDaysToExtrasRawQuery(query string, days int) string {
secondsInDay := 86400
newerThan := secondsInDay * days
t := strconv.Itoa(newerThan)
return query + ` AND CAST(arr.item_object->>'created' AS INT) > (extract(epoch from now()) - ` + t + `) `
}

func addNewerThanTimestampToExtrasRawQuery(query string, timestamp int) string {
t := strconv.Itoa(timestamp)
return query + ` AND CAST(arr.item_object->>'created' AS INT) > ` + t
}

func addOrderToExtrasRawQuery(query string) string {
return query + `ORDER BY cast(arr.item_object->>'created' as integer) DESC`
}

func addNotMineToExtrasRawQuery(query string, pubkey string) string {
return query + ` AND people.owner_pub_key != ` + pubkey + ` `
}
Expand Down Expand Up @@ -525,44 +493,41 @@ func (db database) GetBountiesCount(r *http.Request) int64 {
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
completed := keys.Get("Completed")
pending := keys.Get("Pending")
failed := keys.Get("Failed")

openQuery := ""
assignedQuery := ""
paidQuery := ""
completedQuery := ""
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 completed != "" && completed == "true" {
if open != "" && open == "true" {
completedQuery = "OR assignee != '' AND completed = true AND paid = false"
} else {
completedQuery = "AND assignee != '' AND completed = true AND paid = false"
}
if completed == "true" {
statusConditions = append(statusConditions, "assignee != '' AND completed = true 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")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

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

var count int64

query := "SELECT COUNT(*) FROM bounty WHERE show != false"
allQuery := query + " " + openQuery + " " + assignedQuery + " " + completedQuery + " " + paidQuery
allQuery := query + " " + statusQuery
db.db.Raw(allQuery).Scan(&count)
return count
}
Expand All @@ -572,17 +537,23 @@ func (db database) GetFilterStatusCount() FilterStattuCount {
var assignedCount int64
var completedCount int64
var paidCount int64
var pendingCount int64
var failedCount int64

db.db.Model(&Bounty{}).Where("show != false").Where("assignee = ''").Where("paid != true").Count(&openCount)
db.db.Model(&Bounty{}).Where("show != false").Where("assignee != ''").Where("paid != true").Count(&assignedCount)
db.db.Model(&Bounty{}).Where("show != false").Where("assignee != ''").Where("completed = true").Where("paid != true").Count(&completedCount)
db.db.Model(&Bounty{}).Where("show != false").Where("assignee != ''").Where("paid = true").Count(&paidCount)
db.db.Model(&Bounty{}).Where("show != false").Where("assignee != ''").Where("payment_pending = true").Count(&pendingCount)
db.db.Model(&Bounty{}).Where("show != false").Where("assignee != ''").Where("payment_failed = true").Count(&failedCount)

ms := FilterStattuCount{
Open: openCount,
Assigned: assignedCount,
Completed: completedCount,
Paid: paidCount,
Pending: pendingCount,
Failed: failedCount,
}

return ms
Expand All @@ -596,6 +567,8 @@ func (db database) GetWorkspaceBounties(r *http.Request, workspace_uuid string)
assingned := keys.Get("Assigned")
completed := keys.Get("Completed")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)
Expand Down Expand Up @@ -636,6 +609,12 @@ func (db database) GetWorkspaceBounties(r *http.Request, workspace_uuid string)
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

var statusQuery string
if len(statusConditions) > 0 {
Expand Down Expand Up @@ -683,6 +662,8 @@ func (db database) GetWorkspaceBountiesCount(r *http.Request, workspace_uuid str
assingned := keys.Get("Assigned")
completed := keys.Get("Completed")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)
Expand All @@ -708,6 +689,12 @@ func (db database) GetWorkspaceBountiesCount(r *http.Request, workspace_uuid str
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

var statusQuery string
if len(statusConditions) > 0 {
Expand Down Expand Up @@ -763,6 +750,8 @@ func (db database) GetAssignedBounties(r *http.Request) ([]NewBounty, error) {
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")

orderQuery := ""
limitQuery := ""
Expand All @@ -778,6 +767,12 @@ func (db database) GetAssignedBounties(r *http.Request) ([]NewBounty, error) {
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
Expand Down Expand Up @@ -816,6 +811,8 @@ func (db database) GetCreatedBounties(r *http.Request) ([]NewBounty, error) {
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")

orderQuery := ""
limitQuery := ""
Expand All @@ -831,6 +828,12 @@ func (db database) GetCreatedBounties(r *http.Request) ([]NewBounty, error) {
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
Expand Down Expand Up @@ -872,6 +875,8 @@ func (db database) GetNextBountyByCreated(r *http.Request) (uint, error) {
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)
Expand All @@ -894,6 +899,12 @@ func (db database) GetNextBountyByCreated(r *http.Request) (uint, error) {
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
Expand Down Expand Up @@ -934,6 +945,8 @@ func (db database) GetPreviousBountyByCreated(r *http.Request) (uint, error) {
assingned := keys.Get("Assigned")
completed := keys.Get("Completed")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)
Expand All @@ -959,6 +972,12 @@ func (db database) GetPreviousBountyByCreated(r *http.Request) (uint, error) {
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
Expand Down Expand Up @@ -1000,6 +1019,8 @@ func (db database) GetNextWorkspaceBountyByCreated(r *http.Request) (uint, error
assingned := keys.Get("Assigned")
completed := keys.Get("Completed")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)
Expand All @@ -1025,6 +1046,12 @@ func (db database) GetNextWorkspaceBountyByCreated(r *http.Request) (uint, error
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
Expand Down Expand Up @@ -1066,6 +1093,8 @@ func (db database) GetPreviousWorkspaceBountyByCreated(r *http.Request) (uint, e
assingned := keys.Get("Assigned")
completed := keys.Get("Completed")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
languages := keys.Get("languages")
languageArray := strings.Split(languages, ",")
languageLength := len(languageArray)
Expand All @@ -1091,6 +1120,12 @@ func (db database) GetPreviousWorkspaceBountyByCreated(r *http.Request) (uint, e
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

if len(statusConditions) > 0 {
statusQuery = " AND (" + strings.Join(statusConditions, " OR ") + ")"
Expand Down Expand Up @@ -1146,6 +1181,8 @@ func (db database) GetAllBounties(r *http.Request) []NewBounty {
assingned := keys.Get("Assigned")
completed := keys.Get("Completed")
paid := keys.Get("Paid")
pending := keys.Get("Pending")
failed := keys.Get("Failed")
orgUuid := keys.Get("org_uuid")
workspaceUuid := keys.Get("workspace_uuid")
languages := keys.Get("languages")
Expand Down Expand Up @@ -1202,6 +1239,12 @@ func (db database) GetAllBounties(r *http.Request) []NewBounty {
if paid == "true" {
statusConditions = append(statusConditions, "paid = true")
}
if pending == "true" {
statusConditions = append(statusConditions, "payment_pending = true")
}
if failed == "true" {
statusConditions = append(statusConditions, "payment_failed = true")
}

var statusQuery string
if len(statusConditions) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,8 @@ type FilterStattuCount struct {
Assigned int64 `json:"assigned"`
Completed int64 `json:"completed"`
Paid int64 `json:"paid"`
Pending int64 `json:"pending"`
Failed int64 `json:"failed"`
}

func (Person) TableName() string {
Expand Down

0 comments on commit 00d1ad1

Please sign in to comment.