This repository was archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrouter.go
85 lines (73 loc) · 2.17 KB
/
router.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
package reports
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
moovhttp "github.com/moov-io/base/http"
"github.com/moov-io/base/log"
"github.com/moov-io/customers/pkg/accounts"
"github.com/moov-io/customers/pkg/client"
"github.com/moov-io/customers/pkg/customers"
"github.com/moov-io/customers/pkg/route"
)
func AddRoutes(
logger log.Logger,
r *mux.Router,
customerRepo customers.CustomerRepository,
accountRepo accounts.Repository,
) {
logger = logger.Set("package", log.String("reports"))
r.Methods("GET").Path("/reports/accounts").HandlerFunc(getCustomerAccounts(logger, customerRepo, accountRepo))
}
type CustomerAccount struct {
Customer *client.Customer `json:"customer"`
Account *client.Account `json:"account"`
}
func getCustomerAccounts(
logger log.Logger,
customerRepo customers.CustomerRepository,
accountRepo accounts.Repository,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w = route.Responder(logger, w, r)
accountIDsInput := r.URL.Query().Get("accountIDs")
accountIDsInput = strings.TrimSpace(accountIDsInput)
accountIDs := strings.Split(accountIDsInput, ",")
organization := route.GetOrganization(w, r)
if organization == "" {
return
}
limit := 25
if len(accountIDs) > limit {
moovhttp.Problem(w, fmt.Errorf("exceeded limit of %d accountIDs, found %d", limit, len(accountIDs)))
return
}
allAccounts, err := accountRepo.GetCustomerAccountsByIDs(accountIDs)
if err != nil {
logger.LogErrorf("error getting customers' accounts: %v", err)
moovhttp.Problem(w, err)
return
}
results := make([]*client.ReportAccountResponse, 0)
for _, acc := range allAccounts {
cust, err := customerRepo.GetCustomer(acc.CustomerID, organization)
if err != nil {
logger.LogErrorf("error getting customer: %v", err)
moovhttp.Problem(w, err)
return
}
results = append(results, &client.ReportAccountResponse{
Customer: *cust,
Account: *acc,
})
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(results); err != nil {
moovhttp.Problem(w, err)
return
}
}
}