Skip to content

Commit

Permalink
kyc/social: change primary key (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-dionysos authored Jan 21, 2024
1 parent deb1407 commit 697d389
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
19 changes: 19 additions & 0 deletions kyc/social/.testdata/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: ice License 1.0

development: true
logger:
encoder: console
level: debug

users: &users
wintr/connectors/storage/v2:
runDDL: true
primaryURL: postgresql://root:pass@localhost:5432/ice
credentials:
user: root
password: pass
replicaURLs:
- postgresql://root:pass@localhost:5432/ice

kyc/social:
<<: *users
8 changes: 7 additions & 1 deletion kyc/social/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ insert into unsuccessful_social_kyc_alerts (last_alert_at, kyc_step,social)
(current_timestamp,9, 'twitter'),
(current_timestamp,10, 'twitter')
ON CONFLICT (kyc_step, social)
DO NOTHING;
DO NOTHING;

ALTER TABLE socials DROP CONSTRAINT socials_pkey, ADD PRIMARY KEY (social, user_handle);
ALTER TABLE socials DROP CONSTRAINT IF EXISTS socials_social_user_handle_key;

CREATE INDEX IF NOT EXISTS socials_lookup_userid_idx ON socials (user_id);
CREATE INDEX IF NOT EXISTS socials_lookup_userid_per_social ON socials (user_id, social);
16 changes: 2 additions & 14 deletions kyc/social/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,9 @@ func (r *repository) saveUnsuccessfulAttempt(ctx context.Context, now *time.Time
}

func (r *repository) saveSocial(ctx context.Context, socialType Type, userID, userHandle string) error {
sql := `INSERT INTO socials(user_id,social,user_handle) VALUES ($1,$2,$3)`
_, err := storage.Exec(ctx, r.db, sql, userID, socialType, userHandle)
if err != nil && storage.IsErr(err, storage.ErrDuplicate, "pk") {
sql = `SELECT true AS bogus WHERE EXISTS (SELECT 1 FROM socials WHERE user_id = $1 AND social = $2 AND lower(user_handle) = $3)`
if _, err2 := storage.ExecOne[struct{ Bogus bool }](ctx, r.db, sql, userID, socialType, userHandle); err2 == nil {
return nil
} else if !storage.IsErr(err2, storage.ErrNotFound) {
err = errors.Wrapf(err2, "failed to check if user used the same userhandle previously; userID:%v, social:%v, userHandle:%v",
userID, socialType, userHandle)
}
}
_, err := storage.Exec(ctx, r.db, `INSERT INTO socials(user_id,social,user_handle) VALUES ($1,$2,$3)`, userID, socialType, userHandle)

return errors.Wrapf(err, "failed to `%v`; userID:%v, social:%v, userHandle:%v", sql, userID, socialType, userHandle)
return errors.Wrapf(err, "failed to save social:%v, userID:%v, userHandle:%v", socialType, userID, userHandle)
}

func detectReason(err error) string {
Expand All @@ -362,8 +352,6 @@ func detectReason(err error) string {
case storage.IsErr(err, storage.ErrDuplicate):
if tErr := terror.As(err); tErr != nil {
if unwrapped := tErr.Unwrap(); storage.IsErr(unwrapped, storage.ErrDuplicate, "pk") {
return fmt.Sprintf("duplicate socials '%v'", tErr.Data["user_handle"])
} else if storage.IsErr(unwrapped, storage.ErrDuplicate) {
return fmt.Sprintf("duplicate userhandle '%v'", tErr.Data["user_handle"])
}
}
Expand Down
52 changes: 52 additions & 0 deletions kyc/social/social_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: ice License 1.0

package social

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/ice-blockchain/eskimo/users"
"github.com/ice-blockchain/wintr/connectors/storage/v2"
"github.com/ice-blockchain/wintr/terror"
)

func helperRemoveSocials(t *testing.T, db storage.Execer, userID string) {
t.Helper()

_, err := storage.Exec(context.TODO(), db, "DELETE FROM socials where user_id = $1", userID)
require.NoError(t, err)
}

func TestSocialSave(t *testing.T) {
const userName = `icenetwork`

ctx := context.Background()
usersRepo := users.New(ctx, nil)
require.NotNil(t, usersRepo)

db := storage.MustConnect(ctx, ddl, applicationYamlKey)
repo := &repository{db: db}

helperRemoveSocials(t, db, userName)

t.Run("OK", func(t *testing.T) {
err := repo.saveSocial(ctx, TwitterType, userName, "foo")
require.NoError(t, err)

err = repo.saveSocial(ctx, TwitterType, userName, "bar")
require.NoError(t, err)
})

t.Run("Duplicate", func(t *testing.T) {
err := repo.saveSocial(ctx, TwitterType, userName, "foo")
require.ErrorIs(t, err, storage.ErrDuplicate)

reason := detectReason(terror.New(err, map[string]any{"user_handle": "foo"}))
require.Equal(t, `duplicate userhandle 'foo'`, reason)
})

require.NoError(t, db.Close())
}

0 comments on commit 697d389

Please sign in to comment.