-
Notifications
You must be signed in to change notification settings - Fork 0
Add KYC Service and enable getting user's persona accountId #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e0c4a80
first pass
ocasta181 653be58
fix some bugs, clear unneeded code
ocasta181 7198e9e
create identity on user creation, and update on email verification
ocasta181 2507bdb
add kyc service
ocasta181 5825d48
Update pkg/service/kyc.go
83ef00b
simplify KYC function names
ocasta181 27df6f7
unit tests passing
ocasta181 22df8aa
add route
frostbournesb 4fd4485
update annotations
ocasta181 452a27e
ensure the identity actually get's built
ocasta181 7bd8b6b
handle noRows in sql selects
ocasta181 839065b
use enum
ocasta181 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
libcommon "github.com/String-xyz/go-lib/v2/common" | ||
"github.com/String-xyz/go-lib/v2/database" | ||
baserepo "github.com/String-xyz/go-lib/v2/repository" | ||
"github.com/String-xyz/string-api/pkg/model" | ||
) | ||
|
||
type Identity interface { | ||
Create(ctx context.Context, insert model.Identity) (identity model.Identity, err error) | ||
GetById(ctx context.Context, id string) (model.Identity, error) | ||
List(ctx context.Context, limit int, offset int) ([]model.Identity, error) | ||
Update(ctx context.Context, id string, updates any) (identity model.Identity, err error) | ||
GetByUserId(ctx context.Context, userId string) (identity model.Identity, err error) | ||
GetByAccountId(ctx context.Context, accountId string) (identity model.Identity, err error) | ||
} | ||
|
||
type identity[T any] struct { | ||
baserepo.Base[T] | ||
} | ||
|
||
func NewIdentity(db database.Queryable) Identity { | ||
return &identity[model.Identity]{baserepo.Base[model.Identity]{Store: db, Table: "identity"}} | ||
} | ||
|
||
func (i identity[T]) Create(ctx context.Context, insert model.Identity) (identity model.Identity, err error) { | ||
query, args, err := i.Named(` | ||
INSERT INTO identity (user_id) | ||
VALUES(:user_id) RETURNING *`, insert) | ||
if err != nil { | ||
return identity, libcommon.StringError(err) | ||
} | ||
|
||
err = i.Store.QueryRowxContext(ctx, query, args...).StructScan(&identity) | ||
if err != nil { | ||
return identity, libcommon.StringError(err) | ||
} | ||
|
||
return identity, nil | ||
} | ||
|
||
func (i identity[T]) Update(ctx context.Context, id string, updates any) (identity model.Identity, err error) { | ||
names, keyToUpdate := libcommon.KeysAndValues(updates) | ||
if len(names) == 0 { | ||
return identity, libcommon.StringError(errors.New("no updates provided")) | ||
} | ||
|
||
keyToUpdate["id"] = id | ||
|
||
query := fmt.Sprintf("UPDATE %s SET %s WHERE id=:id RETURNING *", i.Table, strings.Join(names, ",")) | ||
namedQuery, args, err := i.Named(query, keyToUpdate) | ||
if err != nil { | ||
return identity, libcommon.StringError(err) | ||
} | ||
|
||
err = i.Store.QueryRowxContext(ctx, namedQuery, args...).StructScan(&identity) | ||
if err != nil { | ||
return identity, libcommon.StringError(err) | ||
} | ||
return identity, nil | ||
} | ||
|
||
func (i identity[T]) GetByUserId(ctx context.Context, userId string) (identity model.Identity, err error) { | ||
query := fmt.Sprintf("SELECT * FROM %s WHERE user_id=$1", i.Table) | ||
err = i.Store.QueryRowxContext(ctx, query, userId).StructScan(&identity) | ||
if err != nil && err == sql.ErrNoRows { | ||
return identity, libcommon.StringError(err) | ||
} | ||
return identity, nil | ||
} | ||
|
||
func (i identity[T]) GetByAccountId(ctx context.Context, accountId string) (identity model.Identity, err error) { | ||
query := fmt.Sprintf("SELECT * FROM %s WHERE account_id=$1", i.Table) | ||
err = i.Store.QueryRowxContext(ctx, query, accountId).StructScan(&identity) | ||
if err != nil && err == sql.ErrNoRows { | ||
return identity, libcommon.StringError(err) | ||
} | ||
return identity, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ type Repositories struct { | |
Transaction Transaction | ||
TxLeg TxLeg | ||
Location Location | ||
Identity Identity | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ type Services struct { | |
Unit21 Unit21 | ||
Card Card | ||
Webhook Webhook | ||
KYC KYC | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/String-xyz/string-api/pkg/model" | ||
"github.com/String-xyz/string-api/pkg/repository" | ||
) | ||
|
||
type KYC interface { | ||
MeetsRequirements(ctx context.Context, userId string, assetType string, cost float64) (met bool, err error) | ||
GetTransactionLevel(assetType string, cost float64) KYCLevel | ||
GetUserLevel(ctx context.Context, userId string) (level KYCLevel, err error) | ||
UpdateUserLevel(ctx context.Context, userId string) (level KYCLevel, err error) | ||
} | ||
|
||
type KYCLevel int | ||
|
||
const ( | ||
Level0 KYCLevel = iota | ||
Level1 | ||
Level2 | ||
Level3 | ||
) | ||
|
||
type kyc struct { | ||
repos repository.Repositories | ||
} | ||
|
||
func NewKYC(repos repository.Repositories) KYC { | ||
return &kyc{repos} | ||
} | ||
|
||
func (k kyc) MeetsRequirements(ctx context.Context, userId string, assetType string, cost float64) (met bool, err error) { | ||
transactionLevel := k.GetTransactionLevel(assetType, cost) | ||
|
||
userLevel, err := k.GetUserLevel(ctx, userId) | ||
if err != nil { | ||
return false, err | ||
} | ||
if userLevel >= transactionLevel { | ||
return true, nil | ||
} else { | ||
return false, nil | ||
} | ||
} | ||
|
||
func (k kyc) GetTransactionLevel(assetType string, cost float64) KYCLevel { | ||
if assetType == "NFT" { | ||
if cost < 1000.00 { | ||
return Level1 | ||
} else if cost < 5000.00 { | ||
return Level2 | ||
} else { | ||
return Level3 | ||
} | ||
} else { | ||
if cost < 5000.00 { | ||
return Level2 | ||
} else { | ||
return Level3 | ||
} | ||
} | ||
} | ||
|
||
func (k kyc) GetUserLevel(ctx context.Context, userId string) (level KYCLevel, err error) { | ||
level, err = k.UpdateUserLevel(ctx, userId) | ||
if err != nil { | ||
return level, err | ||
} | ||
|
||
return level, nil | ||
} | ||
|
||
func (k kyc) UpdateUserLevel(ctx context.Context, userId string) (level KYCLevel, err error) { | ||
identity, err := k.repos.Identity.GetByUserId(ctx, userId) | ||
if err != nil { | ||
return level, err | ||
} | ||
|
||
points := 0 | ||
if identity.EmailVerified != nil { | ||
points++ | ||
} | ||
if identity.PhoneVerified != nil { | ||
points++ | ||
} | ||
if identity.DocumentVerified != nil { | ||
points++ | ||
} | ||
if identity.SelfieVerified != nil { | ||
points++ | ||
} | ||
|
||
if points >= 4 { | ||
if identity.Level != int(Level2) { | ||
identity.Level = int(Level2) | ||
k.repos.Identity.Update(ctx, userId, model.IdentityUpdates{Level: &identity.Level}) | ||
} | ||
} else if points >= 1 && identity.EmailVerified != nil { | ||
if identity.Level != int(Level1) { | ||
identity.Level = int(Level1) | ||
k.repos.Identity.Update(ctx, userId, model.IdentityUpdates{Level: &identity.Level}) | ||
} | ||
} else if points <= 1 && identity.Level != int(Level0) { | ||
identity.Level = int(Level0) | ||
k.repos.Identity.Update(ctx, userId, model.IdentityUpdates{Level: &identity.Level}) | ||
} | ||
|
||
return KYCLevel(identity.Level), nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.