/
customer.go
99 lines (79 loc) · 2.48 KB
/
customer.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
package merchantapi
import (
"net/http"
"github.com/go-openapi/strfmt"
"github.com/labstack/echo/v4"
"github.com/oxygenpay/oxygen/internal/server/http/common"
"github.com/oxygenpay/oxygen/internal/server/http/middleware"
"github.com/oxygenpay/oxygen/internal/service/payment"
"github.com/oxygenpay/oxygen/internal/util"
"github.com/oxygenpay/oxygen/pkg/api-dashboard/v1/model"
"github.com/pkg/errors"
)
const (
paramCustomerID = "customerId"
)
func (h *Handler) ListCustomers(c echo.Context) error {
ctx := c.Request().Context()
mt := middleware.ResolveMerchant(c)
pagination, err := common.QueryPagination(c)
if err != nil {
return common.ValidationErrorResponse(c, err)
}
customers, nextCursor, err := h.payments.ListCustomers(ctx, mt.ID, payment.ListOptions{
Limit: pagination.Limit,
Cursor: pagination.Cursor,
ReverseOrder: pagination.ReverseSort,
})
switch {
case errors.Is(err, payment.ErrValidation):
return common.ValidationErrorResponse(c, "invalid query")
case err != nil:
return err
}
return c.JSON(http.StatusOK, &model.CustomersPagination{
Cursor: nextCursor,
Limit: int64(pagination.Limit),
Results: util.MapSlice(customers, customerToResponse),
})
}
func (h *Handler) GetCustomerDetails(c echo.Context) error {
ctx := c.Request().Context()
mt := middleware.ResolveMerchant(c)
id, err := common.UUID(c, paramCustomerID)
if err != nil {
return err
}
customerDetails, err := h.payments.GetCustomerDetailsByUUID(ctx, mt.ID, id)
switch {
case errors.Is(err, payment.ErrNotFound):
return common.NotFoundResponse(c, "customer not found")
case err != nil:
return err
}
return c.JSON(http.StatusOK, customerDetailsToResponse(customerDetails))
}
func customerToResponse(c *payment.Customer) *model.Customer {
return &model.Customer{
CreatedAt: strfmt.DateTime(c.CreatedAt),
Email: c.Email,
ID: c.UUID.String(),
}
}
func customerDetailsToResponse(ct *payment.CustomerDetails) *model.Customer {
customer := customerToResponse(&ct.Customer)
mapPayments := func(pt *payment.Payment) *model.CustomerPayment {
return &model.CustomerPayment{
ID: pt.MerchantOrderUUID.String(),
CreatedAt: strfmt.DateTime(pt.CreatedAt),
Currency: pt.Price.Ticker(),
Price: pt.Price.String(),
Status: pt.PublicStatus().String(),
}
}
customer.Details = &model.CustomerDetails{
SuccessfulPayments: ct.SuccessfulPayments,
Payments: util.MapSlice(ct.RecentPayments, mapPayments),
}
return customer
}