The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 line
3.1 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. . "github.com/imosed/signet/data"
  6. "github.com/rs/zerolog/log"
  7. )
  8. func getQualifiedRewardFunds() []RewardFund {
  9. var standalone []RewardFund
  10. var currentFromQueues []RewardFund
  11. var allQueues []RewardFund
  12. Db.Table("reward_funds").
  13. Select("reward_funds.id",
  14. "reward_funds.created_at",
  15. "reward_funds.updated_at",
  16. "reward_funds.deleted_at",
  17. "reward_funds.asset",
  18. "reward_funds.fund_wallet",
  19. "reward_funds.selling_wallet",
  20. "reward_funds.issuer_wallet",
  21. "reward_funds.memo",
  22. "reward_funds.price",
  23. "reward_funds.amount_available",
  24. "reward_funds.min_contribution",
  25. "reward_funds.title",
  26. "reward_funds.description").
  27. Joins("left outer join queue_orders qo on reward_funds.id = qo.reward_fund_id").
  28. Where("qo.reward_fund_id is null").
  29. Scan(&standalone)
  30. tempTable := Db.Table("contributions c").
  31. Select("c.reward_fund_id",
  32. "queue_id",
  33. "sum(amount) amt").
  34. Joins("inner join queue_orders qo on c.reward_fund_id = qo.reward_fund_id").
  35. Group("c.reward_fund_id").
  36. Group("qo.queue_id")
  37. Db.Table("reward_funds").
  38. Select("distinct on (qo.queue_id) reward_funds.id",
  39. "reward_funds.created_at",
  40. "reward_funds.updated_at",
  41. "reward_funds.deleted_at",
  42. "reward_funds.asset",
  43. "reward_funds.fund_wallet",
  44. "reward_funds.selling_wallet",
  45. "reward_funds.issuer_wallet",
  46. "reward_funds.memo",
  47. "reward_funds.price",
  48. "reward_funds.amount_available",
  49. "reward_funds.min_contribution",
  50. "reward_funds.title",
  51. "reward_funds.description").
  52. Joins("inner join queue_orders qo on reward_funds.id = qo.reward_fund_id").
  53. Joins("left join contributions c on reward_funds.id = c.reward_fund_id").
  54. Joins("inner join (?) tt on reward_funds.id = tt.reward_fund_id",
  55. tempTable,
  56. Db.Where("tt.amount < reward_funds.amount_available or tt.amount is null")).
  57. Order("qo.queue_id").
  58. Order("qo.order").
  59. Scan(&currentFromQueues)
  60. allQueues = append(standalone, currentFromQueues...)
  61. return allQueues
  62. }
  63. func GetRewardFunds(w http.ResponseWriter, r *http.Request) {
  64. var req GetRewardFundsRequest
  65. err := json.NewDecoder(r.Body).Decode(&req)
  66. if err != nil {
  67. log.Error().Err(err).Msg("Could not decode body in GetRewardFunds call")
  68. return
  69. }
  70. var resp GetRewardFundsResponse
  71. var rewardFunds []RewardFund
  72. rewardFunds = getQualifiedRewardFunds()
  73. for _, f := range rewardFunds {
  74. resp.RewardFunds = append(resp.RewardFunds, FundInfo{
  75. ID: f.ID,
  76. CreatedAt: f.CreatedAt,
  77. Asset: f.Asset,
  78. FundWallet: f.FundWallet,
  79. IssuerWallet: f.IssuerWallet,
  80. Memo: f.Memo,
  81. AmountAvailable: f.AmountAvailable,
  82. MinContribution: f.MinContribution,
  83. TelegramLink: f.TelegramLink,
  84. Bonuses: f.Bonuses,
  85. })
  86. }
  87. err = json.NewEncoder(w).Encode(resp)
  88. if err != nil {
  89. log.Error().Err(err).Msg("Could not deliver response in GetRewardFunds call")
  90. }
  91. }
  92. type GetRewardFundsRequest struct {
  93. Offset int `json:"offset"`
  94. }
  95. type GetRewardFundsResponse struct {
  96. RewardFunds []FundInfo `json:"rewardFunds"`
  97. Total int64 `json:"total"`
  98. }