/
update_receiver_handler.go
113 lines (96 loc) · 3.63 KB
/
update_receiver_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
107
108
109
110
111
112
113
package httphandler
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/stellar/go/support/http/httpdecode"
"github.com/stellar/go/support/log"
"github.com/stellar/go/support/render/httpjson"
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/validators"
)
type UpdateReceiverHandler struct {
Models *data.Models
DBConnectionPool db.DBConnectionPool
}
func createVerificationInsert(updateReceiverInfo *validators.UpdateReceiverRequest, receiverID string) []data.ReceiverVerificationInsert {
receiverVerifications := []data.ReceiverVerificationInsert{}
if updateReceiverInfo.DateOfBirth != "" {
receiverVerifications = append(receiverVerifications, data.ReceiverVerificationInsert{
ReceiverID: receiverID,
VerificationField: data.VerificationFieldDateOfBirth,
VerificationValue: updateReceiverInfo.DateOfBirth,
})
}
if updateReceiverInfo.Pin != "" {
receiverVerifications = append(receiverVerifications, data.ReceiverVerificationInsert{
ReceiverID: receiverID,
VerificationField: data.VerificationFieldPin,
VerificationValue: updateReceiverInfo.Pin,
})
}
if updateReceiverInfo.NationalID != "" {
receiverVerifications = append(receiverVerifications, data.ReceiverVerificationInsert{
ReceiverID: receiverID,
VerificationField: data.VerificationFieldNationalID,
VerificationValue: updateReceiverInfo.NationalID,
})
}
return receiverVerifications
}
func (h UpdateReceiverHandler) UpdateReceiver(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
var reqBody validators.UpdateReceiverRequest
err := httpdecode.DecodeJSON(req, &reqBody)
if err != nil {
err = fmt.Errorf("decoding the request body: %w", err)
log.Ctx(ctx).Error(err)
httperror.BadRequest("", err, nil).Render(rw)
return
}
// validate request payload
validator := validators.NewUpdateReceiverValidator()
validator.ValidateReceiver(&reqBody)
if validator.HasErrors() {
log.Ctx(ctx).Errorf("request invalid: %s", validator.Errors)
httperror.BadRequest("request invalid", nil, validator.Errors).Render(rw)
return
}
receiverID := chi.URLParam(req, "id")
receiverVerifications := createVerificationInsert(&reqBody, receiverID)
receiver, err := db.RunInTransactionWithResult(ctx, h.DBConnectionPool, nil, func(dbTx db.DBTransaction) (response *data.Receiver, innerErr error) {
for _, rv := range receiverVerifications {
innerErr = h.Models.ReceiverVerification.UpsertVerificationValue(
req.Context(),
dbTx,
rv.ReceiverID,
rv.VerificationField,
rv.VerificationValue,
)
if innerErr != nil {
return nil, fmt.Errorf("error updating receiver verification %s: %w", rv.VerificationField, innerErr)
}
}
receiverUpdate := data.ReceiverUpdate{
Email: reqBody.Email,
ExternalId: reqBody.ExternalID,
}
if receiverUpdate.Email != "" || receiverUpdate.ExternalId != "" {
if innerErr = h.Models.Receiver.Update(ctx, dbTx, receiverID, receiverUpdate); innerErr != nil {
return nil, fmt.Errorf("error updating receiver with ID %s: %w", receiverID, innerErr)
}
}
receiver, innerErr := h.Models.Receiver.Get(ctx, dbTx, receiverID)
if innerErr != nil {
return nil, fmt.Errorf("error querying receiver with ID %s: %w", receiverID, innerErr)
}
return receiver, nil
})
if err != nil {
httperror.InternalError(ctx, "", err, nil).Render(rw)
return
}
httpjson.Render(rw, receiver, httpjson.JSON)
}