/
paylinkctl.go
164 lines (140 loc) · 5.49 KB
/
paylinkctl.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package paylinkctl
import (
"context"
"encoding/json"
"errors"
"fmt"
aulogging "github.com/StephanHCB/go-autumn-logging"
"github.com/eurofurence/reg-payment-cncrd-adapter/internal/api/v1/cncrdapi"
"github.com/eurofurence/reg-payment-cncrd-adapter/internal/repository/attendeeservice"
"github.com/eurofurence/reg-payment-cncrd-adapter/internal/repository/concardis"
"github.com/eurofurence/reg-payment-cncrd-adapter/internal/service/paymentlinksrv"
"github.com/eurofurence/reg-payment-cncrd-adapter/internal/web/util/ctlutil"
"github.com/eurofurence/reg-payment-cncrd-adapter/internal/web/util/ctxvalues"
"github.com/go-chi/chi/v5"
"github.com/go-http-utils/headers"
"net/http"
"net/url"
"strconv"
)
var paymentLinkService paymentlinksrv.PaymentLinkService
func Create(server chi.Router, paymentLinkSrv paymentlinksrv.PaymentLinkService) {
paymentLinkService = paymentLinkSrv
server.Post("/api/rest/v1/paylinks", createPaylinkHandler)
server.Get("/api/rest/v1/paylinks/{id}", getPaylinkHandler)
server.Delete("/api/rest/v1/paylinks/{id}", deletePaylinkHandler)
}
func createPaylinkHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if !ctxvalues.HasApiToken(ctx) {
ctlutil.UnauthenticatedError(ctx, w, r, "you must be logged in for this operation", "anonymous access attempt")
return
}
request, err := parseBodyToPaymentLinkRequestDto(ctx, w, r)
if err != nil {
return
}
errs := paymentLinkService.ValidatePaymentLinkRequest(ctx, request)
if errs != nil {
paylinkRequestInvalidErrorHandler(ctx, w, r, errs)
return
}
dto, id, err := paymentLinkService.CreatePaymentLink(ctx, request)
if err != nil {
if errors.Is(err, concardis.DownstreamError) {
downstreamErrorHandler(ctx, w, r, "paylink", err)
} else if errors.Is(err, attendeeservice.DownstreamError) {
downstreamErrorHandler(ctx, w, r, "attsrv", err)
} else {
ctlutil.UnexpectedError(ctx, w, r, err)
}
return
}
w.Header().Set(headers.Location, fmt.Sprintf("/api/rest/v1/paylinks/%d", id))
w.WriteHeader(http.StatusCreated)
ctlutil.WriteJson(ctx, w, dto)
}
func getPaylinkHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if !ctxvalues.HasApiToken(ctx) {
ctlutil.UnauthenticatedError(ctx, w, r, "you must be logged in for this operation", "anonymous access attempt")
return
}
id, err := idFromVars(ctx, w, r)
if err != nil {
return
}
dto, err := paymentLinkService.GetPaymentLink(ctx, id)
if err != nil {
if errors.Is(err, concardis.DownstreamError) {
downstreamErrorHandler(ctx, w, r, "paylink", err)
} else if errors.Is(err, concardis.NoSuchID404Error) {
paylinkNotFoundErrorHandler(ctx, w, r, id)
} else {
ctlutil.UnexpectedError(ctx, w, r, err)
}
return
}
ctlutil.WriteJson(ctx, w, dto)
}
func deletePaylinkHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if !ctxvalues.HasApiToken(ctx) {
ctlutil.UnauthenticatedError(ctx, w, r, "you must be logged in for this operation", "anonymous access attempt")
return
}
id, err := idFromVars(ctx, w, r)
if err != nil {
return
}
err = paymentLinkService.DeletePaymentLink(ctx, id)
if err != nil {
if errors.Is(err, concardis.DownstreamError) {
downstreamErrorHandler(ctx, w, r, "paylink", err)
} else if errors.Is(err, concardis.NoSuchID404Error) {
paylinkNotFoundErrorHandler(ctx, w, r, id)
} else {
ctlutil.UnexpectedError(ctx, w, r, err)
}
return
}
w.WriteHeader(http.StatusNoContent)
}
func parseBodyToPaymentLinkRequestDto(ctx context.Context, w http.ResponseWriter, r *http.Request) (cncrdapi.PaymentLinkRequestDto, error) {
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
dto := cncrdapi.PaymentLinkRequestDto{}
err := decoder.Decode(&dto)
if err != nil {
paylinkRequestParseErrorHandler(ctx, w, r, err)
}
return dto, err
}
func idFromVars(ctx context.Context, w http.ResponseWriter, r *http.Request) (uint, error) {
idStr := chi.URLParam(r, "id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
invalidPaylinkIdErrorHandler(ctx, w, r, idStr)
}
return uint(id), err
}
func paylinkRequestParseErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("paylink body could not be parsed: %s", err.Error())
ctlutil.ErrorHandler(ctx, w, r, "paylink.parse.error", http.StatusBadRequest, nil)
}
func paylinkRequestInvalidErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, validationErrors url.Values) {
// validation already logged each individual error
ctlutil.ErrorHandler(ctx, w, r, "paylink.data.invalid", http.StatusBadRequest, validationErrors)
}
func downstreamErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, sysname string, err error) {
aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("%s downstream error: %s", sysname, err.Error())
ctlutil.ErrorHandler(ctx, w, r, fmt.Sprintf("%s.downstream.error", sysname), http.StatusBadGateway, nil)
}
func invalidPaylinkIdErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, id string) {
aulogging.Logger.Ctx(ctx).Warn().Printf("received invalid paylink id '%s'", url.QueryEscape(id))
ctlutil.ErrorHandler(ctx, w, r, "paylink.id.invalid", http.StatusBadRequest, url.Values{})
}
func paylinkNotFoundErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, id uint) {
aulogging.Logger.Ctx(ctx).Warn().Printf("paylink id %d not found", id)
ctlutil.ErrorHandler(ctx, w, r, "paylink.id.notfound", http.StatusNotFound, url.Values{})
}