Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
more feedback

rename

lint
  • Loading branch information
ziyliu committed Nov 28, 2023
1 parent 54a5174 commit dbce4d7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
23 changes: 11 additions & 12 deletions internal/serve/httphandler/disbursement_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -54,21 +53,21 @@ func (d DisbursementHandler) PostDisbursement(w http.ResponseWriter, r *http.Req
return
}

iv := validators.NewDisbursementInstructionsValidator(disbursementRequest.VerificationField)
iv.Check(disbursementRequest.Name != "", "name", "name is required")
iv.Check(disbursementRequest.CountryCode != "", "country_code", "country_code is required")
iv.Check(disbursementRequest.WalletID != "", "wallet_id", "wallet_id is required")
iv.Check(disbursementRequest.AssetID != "", "asset_id", "asset_id is required")
v := validators.NewDisbursementRequestValidator(disbursementRequest.VerificationField)
v.Check(disbursementRequest.Name != "", "name", "name is required")
v.Check(disbursementRequest.CountryCode != "", "country_code", "country_code is required")
v.Check(disbursementRequest.WalletID != "", "wallet_id", "wallet_id is required")
v.Check(disbursementRequest.AssetID != "", "asset_id", "asset_id is required")

if iv.HasErrors() {
httperror.BadRequest("Request invalid", err, iv.Errors).Render(w)
if v.HasErrors() {
httperror.BadRequest("Request invalid", err, v.Errors).Render(w)
return
}

iv.ValidateAndGetVerificationType(strings.TrimSpace(string(disbursementRequest.VerificationField)))
verificationField := v.ValidateAndGetVerificationType()

if iv.HasErrors() {
httperror.BadRequest("Verification field invalid", err, iv.Errors).Render(w)
if v.HasErrors() {
httperror.BadRequest("Verification field invalid", err, v.Errors).Render(w)
return
}

Expand Down Expand Up @@ -117,7 +116,7 @@ func (d DisbursementHandler) PostDisbursement(w http.ResponseWriter, r *http.Req
Wallet: wallet,
Asset: asset,
Country: country,
VerificationField: disbursementRequest.VerificationField,
VerificationField: verificationField,
}

newId, err := d.Models.Disbursements.Insert(ctx, &disbursement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func (iv *DisbursementInstructionsValidator) ValidateInstruction(instruction *da
}

// validateAndGetVerificationType validates if the verification type field is a valid value.
func (iv *DisbursementInstructionsValidator) ValidateAndGetVerificationType(verificationType string) data.VerificationField {
vt := data.VerificationField(strings.ToUpper(verificationType))
func (iv *DisbursementInstructionsValidator) ValidateAndGetVerificationType(verificationField string) data.VerificationField {
vf := data.VerificationField(strings.ToUpper(verificationField))

switch vt {
switch vf {
case data.VerificationFieldDateOfBirth, data.VerificationFieldPin, data.VerificationFieldNationalID:
return vt
return vf
default:
iv.Check(false, "verification_field", "invalid parameter. valid values are: DATE_OF_BIRTH, PIN, NATIONAL_ID_NUMBER")
return ""
Expand Down
26 changes: 26 additions & 0 deletions internal/serve/validators/disbursement_request_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package validators

import "github.com/stellar/stellar-disbursement-platform-backend/internal/data"

type DisbursementRequestValidator struct {
verificationField data.VerificationField
*Validator
}

func NewDisbursementRequestValidator(verificationField data.VerificationField) *DisbursementRequestValidator {
return &DisbursementRequestValidator{
verificationField: verificationField,
Validator: NewValidator(),
}
}

// ValidateAndGetVerificationType validates if the verification type field is a valid value.
func (dv *DisbursementRequestValidator) ValidateAndGetVerificationType() data.VerificationField {
switch dv.verificationField {
case data.VerificationFieldDateOfBirth, data.VerificationFieldPin, data.VerificationFieldNationalID:
return dv.verificationField
default:
dv.Check(false, "verification_field", "invalid parameter. valid values are: DATE_OF_BIRTH, PIN, NATIONAL_ID_NUMBER")
return ""
}
}
32 changes: 32 additions & 0 deletions internal/serve/validators/disbursement_request_validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package validators

import (
"testing"

"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stretchr/testify/assert"
)

func Test_DisbursementRequestValidator_ValidateAndGetVerificationType(t *testing.T) {
t.Run("Valid verification type", func(t *testing.T) {
validField := []data.VerificationField{
data.VerificationFieldDateOfBirth,
data.VerificationFieldPin,
data.VerificationFieldNationalID,
}
for _, field := range validField {
validator := NewDisbursementRequestValidator(field)
assert.Equal(t, field, validator.ValidateAndGetVerificationType())
}
})

t.Run("Invalid verification type", func(t *testing.T) {
field := data.VerificationField("field")
validator := NewDisbursementRequestValidator(field)

actual := validator.ValidateAndGetVerificationType()
assert.Empty(t, actual)
assert.Equal(t, 1, len(validator.Errors))
assert.Equal(t, "invalid parameter. valid values are: DATE_OF_BIRTH, PIN, NATIONAL_ID_NUMBER", validator.Errors["verification_field"])
})
}

0 comments on commit dbce4d7

Please sign in to comment.