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.

getrewardfund.go 4.9 KiB

2 år sedan
2 år sedan
2 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package endpoints
  2. import (
  3. "encoding/json"
  4. . "github.com/imosed/signet/data"
  5. "gorm.io/gorm/clause"
  6. "net/http"
  7. "time"
  8. )
  9. type Total struct {
  10. AmountHeld float64 `json:"amountHeld"`
  11. }
  12. type GetRewardFundRequest struct {
  13. Id uint `json:"id"`
  14. ConsolidateContributions bool `json:"consolidateContributions"`
  15. }
  16. type GetRewardFundResponse struct {
  17. FundInfo FundInfo `json:"fundInfo"`
  18. Contributions Contributions `json:"contributions"`
  19. Total Total `json:"total"`
  20. }
  21. func GetRewardFund(resp http.ResponseWriter, req *http.Request) {
  22. var fund RewardFund
  23. var contribs Contributions
  24. var amountHeld Total
  25. var requestedFund GetRewardFundRequest
  26. dec := json.NewDecoder(req.Body)
  27. err := dec.Decode(&requestedFund)
  28. if err != nil {
  29. panic("Could not read requested fund")
  30. }
  31. found := Db.Preload(clause.Associations).Find(&fund, requestedFund.Id).RowsAffected
  32. if found == 0 {
  33. resp.WriteHeader(404)
  34. return
  35. }
  36. Db.Table("contributions").Select("distinct on (created_at::TIMESTAMP::DATE) created_at").Where("reward_fund_id = ?", fund.ID).Scan(&contribs.Dates)
  37. baseQuery := Db.Table("contributions").Where("reward_fund_id = ?", fund.ID).Limit(12) // TODO: change value
  38. baseCount := Db.Table("contributions").Where("reward_fund_id = ?", fund.ID)
  39. Db.Table("contributions").Select("sum(amount) amount_held").Where("reward_fund_id = ?", fund.ID).Scan(&amountHeld)
  40. if requestedFund.ConsolidateContributions {
  41. baseQuery.Select("wallet, sum(amount) amount").Group("wallet").Order("wallet").Scan(&contribs.List)
  42. baseCount.Group("wallet").Count(&contribs.Total)
  43. } else {
  44. baseQuery.Select("wallet, amount, created_at").Order("created_at desc").Scan(&contribs.List)
  45. baseCount.Count(&contribs.Total)
  46. }
  47. r := GetRewardFundResponse{
  48. FundInfo: FundInfo{
  49. ID: fund.ID,
  50. CreatedAt: fund.CreatedAt,
  51. Asset: fund.Asset,
  52. FundWallet: fund.FundWallet,
  53. IssuerWallet: fund.IssuerWallet,
  54. Memo: fund.Memo,
  55. Price: fund.Price,
  56. AmountAvailable: fund.AmountAvailable,
  57. MinContribution: fund.MinContribution,
  58. Title: fund.Title,
  59. Description: fund.Description,
  60. Bonuses: fund.Bonuses,
  61. },
  62. Contributions: contribs,
  63. Total: amountHeld,
  64. }
  65. err = json.NewEncoder(resp).Encode(r)
  66. if err != nil {
  67. panic("Could not deliver requested fund")
  68. }
  69. }
  70. type GetContributionsRequest struct {
  71. Id uint `json:"id"`
  72. Offset int `json:"offset"`
  73. ForDate string `json:"forDate"`
  74. ConsolidateContributions bool `json:"consolidateContributions"`
  75. }
  76. type Contributions struct {
  77. List []Contribution `json:"list"`
  78. Dates []string `json:"dates"`
  79. Total int64 `json:"total"`
  80. }
  81. type GetContributionsResponse = Contributions
  82. func GetContributions(w http.ResponseWriter, r *http.Request) {
  83. var req GetContributionsRequest
  84. err := json.NewDecoder(r.Body).Decode(&req)
  85. if err != nil {
  86. panic("Could not decode body")
  87. }
  88. var resp GetContributionsResponse
  89. baseQuery := Db.Table("contributions").Where("reward_fund_id = ?", req.Id).Offset(req.Offset).Limit(12) // TODO: change value
  90. baseCount := Db.Table("contributions").Where("reward_fund_id = ?", req.Id)
  91. Db.Table("contributions").Select("distinct on (created_at::TIMESTAMP::DATE) created_at").Where("reward_fund_id = ?", req.Id).Scan(&resp.Dates)
  92. if req.ForDate == "" {
  93. if req.ConsolidateContributions {
  94. baseQuery.Select("wallet, sum(amount) amount").Group("wallet").Scan(&resp.List)
  95. baseCount.Group("wallet").Count(&resp.Total)
  96. } else {
  97. baseQuery.Select("wallet, amount, created_at").Order("created_at desc").Scan(&resp.List)
  98. baseCount.Count(&resp.Total)
  99. }
  100. } else {
  101. var t time.Time
  102. t, err = time.Parse("2006-Jan-02", req.ForDate)
  103. t = t.Add(time.Hour * 24)
  104. if err != nil {
  105. panic("Could not interpret time")
  106. }
  107. if req.ConsolidateContributions {
  108. baseQuery.Select("wallet, sum(amount) amount").Where("created_at < ?", t).Group("wallet").Scan(&resp.List)
  109. baseCount.Where("created_at < ?", t).Group("wallet").Count(&resp.Total)
  110. } else {
  111. baseQuery.Select("wallet, amount, created_at").Where("created_at < ?", t).Order("created_at desc").Scan(&resp.List)
  112. baseCount.Where("created_at < ?", t).Count(&resp.Total)
  113. }
  114. }
  115. err = json.NewEncoder(w).Encode(resp)
  116. if err != nil {
  117. panic("Could not deliver response")
  118. }
  119. }
  120. type FundInfo struct {
  121. ID uint `json:"id"`
  122. CreatedAt time.Time `json:"createdAt"`
  123. Asset string `json:"asset"`
  124. FundWallet string `json:"fundWallet"`
  125. IssuerWallet string `json:"issuerWallet"`
  126. Memo string `json:"memo"`
  127. Price float64 `json:"price"`
  128. AmountAvailable float64 `json:"amountAvailable"`
  129. MinContribution float64 `json:"minContribution"`
  130. Title string `json:"title"`
  131. Description string `json:"description"`
  132. Bonuses []Bonus `json:"bonuses"`
  133. }