Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyliu committed Dec 4, 2023
1 parent 7df3634 commit 5a37aee
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 12 deletions.
37 changes: 34 additions & 3 deletions internal/data/receiver_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ReceiverVerificationInsert struct {
ReceiverID string `db:"receiver_id"`
VerificationField VerificationField `db:"verification_field"`
VerificationValue string `db:"hashed_value"`
UpdatedAt *time.Time `db:"updated_at"`
}

const MaxAttemptsAllowed = 15
Expand Down Expand Up @@ -91,6 +92,30 @@ func (m ReceiverVerificationModel) GetAllByReceiverId(ctx context.Context, sqlEx
return receiverVerifications, nil
}

func (m *ReceiverVerificationModel) GetLatestByReceiverId(ctx context.Context, sqlExec db.SQLExecuter, receiverId string) (*ReceiverVerification, error) {
receiverVerifications := []ReceiverVerification{}
query := `
SELECT
*
FROM
receiver_verifications
WHERE
receiver_id = $1
ORDER BY
updated_at DESC
`

err := sqlExec.SelectContext(ctx, &receiverVerifications, query, receiverId)
if err != nil {
return nil, fmt.Errorf("error querying receiver verifications: %w", err)
}
if len(receiverVerifications) == 0 {
return nil, fmt.Errorf("cannot query any receiver verifications for receiver id %s", receiverId)
}

return &receiverVerifications[0], nil
}

// Insert inserts a new receiver verification
func (m ReceiverVerificationModel) Insert(ctx context.Context, sqlExec db.SQLExecuter, verificationInsert ReceiverVerificationInsert) (string, error) {
err := verificationInsert.Validate()
Expand All @@ -106,11 +131,17 @@ func (m ReceiverVerificationModel) Insert(ctx context.Context, sqlExec db.SQLExe
INSERT INTO receiver_verifications (
receiver_id,
verification_field,
hashed_value
) VALUES ($1, $2, $3)
hashed_value%s
) VALUES ($1, $2, $3%s)
`

_, err = sqlExec.ExecContext(ctx, query, verificationInsert.ReceiverID, verificationInsert.VerificationField, hashedValue)
if verificationInsert.UpdatedAt != nil {
query = fmt.Sprintf(query, ",\nupdated_at\n", ", $4")
_, err = sqlExec.ExecContext(ctx, query, verificationInsert.ReceiverID, verificationInsert.VerificationField, hashedValue, *verificationInsert.UpdatedAt)
} else {
query = fmt.Sprintf(query, "", "")
_, err = sqlExec.ExecContext(ctx, query, verificationInsert.ReceiverID, verificationInsert.VerificationField, hashedValue)
}

if err != nil {
return "", fmt.Errorf("error inserting receiver verification: %w", err)
Expand Down
54 changes: 54 additions & 0 deletions internal/data/receiver_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -125,6 +126,59 @@ func Test_ReceiverVerificationModel_GetAllByReceiverId(t *testing.T) {
})
}

func Test_ReceiverVerificationModel_GetReceiverVerificationByReceiverId(t *testing.T) {
dbt := dbtest.Open(t)
defer dbt.Close()

dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
require.NoError(t, err)
defer dbConnectionPool.Close()

ctx := context.Background()

receiver := CreateReceiverFixture(t, ctx, dbConnectionPool, &Receiver{})

t.Run("returns error when the receiver has no verifications registered", func(t *testing.T) {
receiverVerificationModel := ReceiverVerificationModel{}
_, err := receiverVerificationModel.GetLatestByReceiverId(ctx, dbConnectionPool, receiver.ID)
require.Error(t, err, fmt.Errorf("cannot query any receiver verifications for receiver id %s", receiver.ID))
})

t.Run("returns the latest receiver verification for a list of receiver verifications", func(t *testing.T) {
earlierTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
CreateReceiverVerificationFixture(t, ctx, dbConnectionPool, ReceiverVerificationInsert{
ReceiverID: receiver.ID,
VerificationField: VerificationFieldDateOfBirth,
VerificationValue: "1990-01-01",
UpdatedAt: &earlierTime,
})
CreateReceiverVerificationFixture(t, ctx, dbConnectionPool, ReceiverVerificationInsert{
ReceiverID: receiver.ID,
VerificationField: VerificationFieldPin,
VerificationValue: "1234",
UpdatedAt: &earlierTime,
})
verification3 := CreateReceiverVerificationFixture(t, ctx, dbConnectionPool, ReceiverVerificationInsert{
ReceiverID: receiver.ID,
VerificationField: VerificationFieldNationalID,
VerificationValue: "5678",
})

receiverVerificationModel := ReceiverVerificationModel{}
actualVerification, err := receiverVerificationModel.GetLatestByReceiverId(ctx, dbConnectionPool, receiver.ID)
require.NoError(t, err)

assert.Equal(t,
ReceiverVerification{
ReceiverID: receiver.ID,
VerificationField: VerificationFieldNationalID,
HashedValue: verification3.HashedValue,
CreatedAt: verification3.CreatedAt,
UpdatedAt: verification3.UpdatedAt,
}, *actualVerification)
})
}

func Test_ReceiverVerificationModel_Insert(t *testing.T) {
dbt := dbtest.Open(t)
defer dbt.Close()
Expand Down
29 changes: 27 additions & 2 deletions internal/htmltemplate/tmpl/receiver_register.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,39 @@
/>
</div>
<div class="Form__item">
<label for="verification">Date of birth</label>
{{ if .VerificationField eq "DATE_OF_BIRTH" }}
<label for="verification">Date of birth</label>
<input
type="date"
id="date-of-birth"
name="date-of-birth"
required
/>
{{ else if .VerificationField eq "PIN" }}
<label for="verification">Pin</label>
<input
type="string"
id="pin"
name="pin"
required
/>
{{ else if .VerificationField eq "NATIONAL_ID" }}
<label for="verification">National ID</label>
<input
type="string"
id="national-id"
name="national-id"
required
/>
{{ end }}
<!--<label for="verification">Date of birth</label>
<input
type="date"
id="verification"
name="verification"
required
/>
</div>
</div-->

<div class="g-recaptcha" data-sitekey="{{.ReCAPTCHASiteKey}}"></div>

Expand Down
26 changes: 19 additions & 7 deletions internal/serve/httphandler/receiver_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import (
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/anchorplatform"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
"github.com/stellar/stellar-disbursement-platform-backend/internal/db"
htmlTpl "github.com/stellar/stellar-disbursement-platform-backend/internal/htmltemplate"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror"
)

type ReceiverRegistrationHandler struct {
ReceiverWalletModel *data.ReceiverWalletModel
ReCAPTCHASiteKey string
ReceiverWalletModel *data.ReceiverWalletModel
ReceiverVerificationModel *data.ReceiverVerificationModel
DBConnectionPool db.DBConnectionPool
ReCAPTCHASiteKey string
}

type ReceiverRegistrationData struct {
StellarAccount string
JWTToken string
Title string
Message string
ReCAPTCHASiteKey string
StellarAccount string
JWTToken string
Title string
Message string
ReCAPTCHASiteKey string
VerificationField data.VerificationField
}

// ServeHTTP will serve the SEP-24 deposit page needed to register users.
Expand Down Expand Up @@ -70,6 +74,14 @@ func (h ReceiverRegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
htmlTemplateName = "receiver_registered_successfully.tmpl"
tmplData.Title = "Registration Complete 🎉"
tmplData.Message = "Your Stellar wallet has been registered successfully!"
} else {
latestReceiverVerification, err := h.ReceiverVerificationModel.GetLatestByReceiverId(ctx, h.DBConnectionPool, rw.Receiver.ID)

Check failure on line 78 in internal/serve/httphandler/receiver_registration.go

View workflow job for this annotation

GitHub Actions / check

shadow: declaration of "err" shadows declaration at line 51 (govet)
if err != nil {
httperror.InternalError(ctx, "Cannot find receiver verifications for receiver wallet", err, nil).Render(w)
return
}

tmplData.VerificationField = latestReceiverVerification.VerificationField
}

registerPage, err := htmlTpl.ExecuteHTMLTemplate(htmlTemplateName, tmplData)
Expand Down

0 comments on commit 5a37aee

Please sign in to comment.