The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

111 rader
3.0 KiB

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