-
Notifications
You must be signed in to change notification settings - Fork 5
/
history.go
62 lines (48 loc) · 1.76 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package handlers
import (
"encoding/json"
"net/http"
"github.com/bhuvansingla/iitk-coin/account"
"github.com/bhuvansingla/iitk-coin/auth"
"github.com/bhuvansingla/iitk-coin/errors"
)
type WalletHistoryResponse struct {
History []interface{} `json:"history"`
RollNo string `json:"rollNo"`
}
func WalletHistory(w http.ResponseWriter, r *http.Request) error {
if r.Method != "GET" {
return errors.NewHTTPError(nil, http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
}
queriedRollNo := r.URL.Query().Get("rollNo")
if err := account.ValidateRollNo(queriedRollNo); err != nil {
return errors.NewHTTPError(err, http.StatusBadRequest, "invalid rollNo")
}
requestorRollNo, err := auth.GetRollNoFromRequest(r)
if err != nil {
return errors.NewHTTPError(err, http.StatusBadRequest, "invalid cookie")
}
requestorRole, err := account.GetAccountRoleByRollNo(requestorRollNo)
if err != nil {
return err
}
if !(requestorRole == account.GeneralSecretary || requestorRole == account.AssociateHead || requestorRollNo == queriedRollNo) {
return errors.NewHTTPError(nil, http.StatusUnauthorized, "you are not authorized to read this account history")
}
userExists, err := account.UserExists(queriedRollNo)
if err != nil {
errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if !userExists {
return errors.NewHTTPError(err, http.StatusBadRequest, "account does not exist")
}
history, err := account.GetWalletHistoryByRollNo(queriedRollNo)
if err != nil {
return errors.NewHTTPError(err, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
json.NewEncoder(w).Encode(&WalletHistoryResponse{
History: history,
RollNo: queriedRollNo,
})
return nil
}