/
admin_history_handler.go
106 lines (94 loc) · 2.52 KB
/
admin_history_handler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package controller
import (
"net/http"
"strconv"
"sync"
"github.com/gorilla/mux"
"github.com/ic3network/mccs-alpha/internal/app/service"
"github.com/ic3network/mccs-alpha/internal/app/types"
"github.com/ic3network/mccs-alpha/internal/pkg/l"
"github.com/ic3network/mccs-alpha/internal/pkg/template"
"github.com/ic3network/mccs-alpha/internal/pkg/util"
"go.uber.org/zap"
)
type adminHistoryHandler struct {
once *sync.Once
}
var AdminHistoryHandler = newAdminHistoryHandler()
func newAdminHistoryHandler() *adminHistoryHandler {
return &adminHistoryHandler{
once: new(sync.Once),
}
}
func (h *adminHistoryHandler) RegisterRoutes(
public *mux.Router,
private *mux.Router,
adminPublic *mux.Router,
adminPrivate *mux.Router,
) {
h.once.Do(func() {
adminPrivate.Path("/history/{id}").HandlerFunc(h.historyPage()).Methods("GET")
})
}
func (h *adminHistoryHandler) historyPage() func(http.ResponseWriter, *http.Request) {
t := template.NewView("admin/history")
type formData struct {
DateFrom string
DateTo string
Page int
}
type response struct {
FormData formData
TotalPages int
Balance float64
Transactions []*types.Transaction
Email string
BusinessID string
}
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bID := vars["id"]
q := r.URL.Query()
page, err := strconv.Atoi(q.Get("page"))
if err != nil {
l.Logger.Error("controller.AdminHistory.HistoryPage failed", zap.Error(err))
t.Error(w, r, nil, err)
return
}
f := formData{
DateFrom: q.Get("date-from"),
DateTo: q.Get("date-to"),
Page: page,
}
user, err := UserHandler.FindByBusinessID(bID)
if err != nil {
l.Logger.Error("controller.AdminHistory.HistoryPage failed", zap.Error(err))
t.Error(w, r, nil, err)
return
}
res := response{FormData: f, BusinessID: bID, Email: user.Email}
// Get the account balance.
account, err := service.Account.FindByBusinessID(bID)
if err != nil {
l.Logger.Error("controller.AdminHistory.HistoryPage failed", zap.Error(err))
t.Error(w, r, nil, err)
return
}
res.Balance = account.Balance
// Get the recent transactions.
transactions, totalPages, err := service.Transaction.FindInRange(
account.ID,
util.ParseTime(f.DateFrom),
util.ParseTime(f.DateTo),
page,
)
if err != nil {
l.Logger.Error("controller.AdminHistory.HistoryPage failed", zap.Error(err))
t.Error(w, r, nil, err)
return
}
res.Transactions = transactions
res.TotalPages = totalPages
t.Render(w, r, res, nil)
}
}