From 2ee3348b629109c7b9f86aa53ce166a1384d394c Mon Sep 17 00:00:00 2001 From: Jared Date: Sun, 8 Jan 2023 02:18:08 -0500 Subject: [PATCH] Got queue stuff worked out --- endpoints/getrewardfunds.go | 70 +++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/endpoints/getrewardfunds.go b/endpoints/getrewardfunds.go index 65364b4..d61939b 100644 --- a/endpoints/getrewardfunds.go +++ b/endpoints/getrewardfunds.go @@ -2,11 +2,71 @@ package endpoints import ( "encoding/json" - . "github.com/imosed/signet/data" - "gorm.io/gorm/clause" "net/http" + + . "github.com/imosed/signet/data" ) +func getQualifiedRewardFunds() []RewardFund { + var standalone []RewardFund + var currentFromQueues []RewardFund + var allQueues []RewardFund + + Db.Table("reward_funds"). + Select("reward_funds.id", + "reward_funds.created_at", + "reward_funds.updated_at", + "reward_funds.deleted_at", + "reward_funds.asset", + "reward_funds.fund_wallet", + "reward_funds.selling_wallet", + "reward_funds.issuer_wallet", + "reward_funds.memo", + "reward_funds.price", + "reward_funds.amount_available", + "reward_funds.min_contribution", + "reward_funds.title", + "reward_funds.description"). + Joins("left outer join queue_orders qo on reward_funds.id = qo.reward_fund_id"). + Where("qo.reward_fund_id is null"). + Scan(&standalone) + + tempTable := Db.Table("contributions c"). + Select("c.reward_fund_id", + "queue_id", + "sum(amount) amt"). + Joins("inner join queue_orders qo on c.reward_fund_id = qo.reward_fund_id"). + Group("c.reward_fund_id"). + Group("qo.queue_id") + + Db.Table("reward_funds"). + Select("distinct on (qo.queue_id) reward_funds.id", + "reward_funds.created_at", + "reward_funds.updated_at", + "reward_funds.deleted_at", + "reward_funds.asset", + "reward_funds.fund_wallet", + "reward_funds.selling_wallet", + "reward_funds.issuer_wallet", + "reward_funds.memo", + "reward_funds.price", + "reward_funds.amount_available", + "reward_funds.min_contribution", + "reward_funds.title", + "reward_funds.description"). + Joins("inner join queue_orders qo on reward_funds.id = qo.reward_fund_id"). + Joins("left join contributions c on reward_funds.id = c.reward_fund_id"). + Joins("inner join (?) tt on reward_funds.id = tt.reward_fund_id", + tempTable, + Db.Where("tt.amount < reward_funds.amount_available or tt.amount is null")). + Order("qo.queue_id"). + Order("qo.order"). + Scan(¤tFromQueues) + + allQueues = append(standalone, currentFromQueues...) + return allQueues +} + func GetRewardFunds(w http.ResponseWriter, r *http.Request) { var req GetRewardFundsRequest err := json.NewDecoder(r.Body).Decode(&req) @@ -16,11 +76,7 @@ func GetRewardFunds(w http.ResponseWriter, r *http.Request) { var resp GetRewardFundsResponse var rewardFunds []RewardFund - Db.Table("reward_funds").Count(&resp.Total) - Db.Preload(clause.Associations).Table("reward_funds"). - Select("id", "created_at", "asset", "fund_wallet", "issuer_wallet", "memo", "min_contribution", "amount_available", "title"). - Order("created_at desc"). - Find(&rewardFunds) + rewardFunds = getQualifiedRewardFunds() for _, f := range rewardFunds { resp.RewardFunds = append(resp.RewardFunds, FundInfo{