Skip to content

Task/andrin/str 457 #126

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 31 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3cc49ba
Merge pull request #70 from String-xyz/develop
saito-sv Jan 10, 2023
1760116
Merge pull request #83 from String-xyz/develop
saito-sv Jan 11, 2023
e49194e
Merge pull request #85 from String-xyz/develop
saito-sv Jan 12, 2023
e45add8
Merge pull request #90 from String-xyz/develop
saito-sv Jan 12, 2023
68a363c
Merge pull request #94 from String-xyz/develop
saito-sv Jan 14, 2023
ff5fa02
Merge pull request #96 from String-xyz/develop
saito-sv Jan 16, 2023
41cca7e
Merge pull request #116 from String-xyz/develop
saito-sv Feb 14, 2023
fed6f03
Merge pull request #121 from String-xyz/develop
saito-sv Feb 14, 2023
2298ee4
get instrument types sending properly again
ocasta181 Feb 23, 2023
9ada898
remove CustomData and empty LocationData
ocasta181 Feb 23, 2023
5366843
Turns out they lied...
ocasta181 Feb 23, 2023
e5d543d
replace ioutil.ReadAll with io.ReadAll (#122)
Feb 15, 2023
8e0348f
fix go build on CI (#123)
saito-sv Feb 21, 2023
fa72e18
use zero logs instead of log (#124)
saito-sv Feb 21, 2023
c1d0618
merge conflicts
ocasta181 Feb 23, 2023
42f6d31
get instrument types sending properly again
ocasta181 Feb 23, 2023
e44da5c
remove CustomData and empty LocationData
ocasta181 Feb 23, 2023
a400a25
Turns out they lied...
ocasta181 Feb 23, 2023
c58d751
rebase
ocasta181 Feb 23, 2023
662cfd3
remove new lines
ocasta181 Feb 23, 2023
b35c081
pull from master
ocasta181 Feb 23, 2023
afed4d6
replace ioutil.ReadAll with io.ReadAll (#122)
Feb 15, 2023
85648b1
get instrument types sending properly again
ocasta181 Feb 23, 2023
0235995
Turns out they lied...
ocasta181 Feb 23, 2023
886e8f1
pull from master
ocasta181 Feb 23, 2023
4322b71
Merge branch 'task/andrin/str-457' of github.com:String-xyz/string-ap…
ocasta181 Feb 23, 2023
7496a8f
merge
ocasta181 Feb 23, 2023
1d83844
allow nil device IPAddress
ocasta181 Feb 24, 2023
b7b451e
await unit21CreateInstrument
Feb 24, 2023
fbe79ee
refactor unit21 for dependency injection; create wallet instrument on…
ocasta181 Feb 24, 2023
0062875
merge conflicts
ocasta181 Feb 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewRepos(config APIConfig) repository.Repositories {
* Not every service needs access to all of the repos, so we can pass in only the ones it needs. This will make it easier to test
*/
func NewServices(config APIConfig, repos repository.Repositories) service.Services {
unit21 := service.NewUnit21(repos)
httpClient := service.NewHTTPClient(service.HTTPConfig{Timeout: time.Duration(30) * time.Second})
client := service.NewFingerprintClient(httpClient)
fingerprint := service.NewFingerprint(client)
Expand All @@ -52,8 +53,8 @@ func NewServices(config APIConfig, repos repository.Repositories) service.Servic
platformRepos := repository.Repositories{Auth: repos.Auth, Platform: repos.Platform}
platform := service.NewPlatform(platformRepos)

transaction := service.NewTransaction(repos, config.Redis)
user := service.NewUser(repos, auth, fingerprint, device)
transaction := service.NewTransaction(repos, config.Redis, unit21)
user := service.NewUser(repos, auth, fingerprint, device, unit21)

return service.Services{
Auth: auth,
Expand Down
12 changes: 2 additions & 10 deletions pkg/internal/unit21/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/String-xyz/string-api/pkg/internal/common"
"github.com/String-xyz/string-api/pkg/model"
"github.com/String-xyz/string-api/pkg/repository"
"github.com/rs/zerolog/log"
)

Expand All @@ -17,18 +16,11 @@ type Action interface {
eventSubtype string) (unit21Id string, err error)
}

type ActionRepo struct {
User repository.User
Device repository.Device
Location repository.Location
}

type action struct {
repo ActionRepo
}

func NewAction(r ActionRepo) Action {
return &action{repo: r}
func NewAction() Action {
return &action{}
}

func (a action) Create(
Expand Down
3 changes: 1 addition & 2 deletions pkg/internal/unit21/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func u21Post(url string, jsonBody any) (body []byte, err error) {
apiKey := os.Getenv("UNIT21_API_KEY")

reqBodyBytes, err := json.Marshal(jsonBody)

if err != nil {
log.Err(err).Msg("Could not encode into bytes")
return nil, common.StringError(err)
Expand Down Expand Up @@ -97,7 +96,7 @@ func u21Post(url string, jsonBody any) (body []byte, err error) {
return nil, common.StringError(err)
}

log.Info().Str("body", string(body)).Msgf("Strinb of body grom response")
log.Info().Str("body", string(body)).Msgf("String of body from response")

if res.StatusCode != 200 {
log.Err(err).Str("url", url).Int("statusCode", res.StatusCode).Msg("Request failed to update")
Expand Down
62 changes: 35 additions & 27 deletions pkg/internal/unit21/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ type Instrument interface {
Update(instrument model.Instrument) (unit21Id string, err error)
}

type InstrumentRepo struct {
type InstrumentRepos struct {
User repository.User
Device repository.Device
Location repository.Location
}

type instrument struct {
repo InstrumentRepo
action Action
repos InstrumentRepos
}

func NewInstrument(r InstrumentRepo) Instrument {
return &instrument{repo: r}
func NewInstrument(r InstrumentRepos, a Action) Instrument {
return &instrument{repos: r, action: a}
}

func (i instrument) Create(instrument model.Instrument) (unit21Id string, err error) {
Expand Down Expand Up @@ -70,6 +71,14 @@ func (i instrument) Create(instrument model.Instrument) (unit21Id string, err er
}

log.Info().Str("Unit21Id", u21Response.Unit21Id).Send()

// Log create instrument action w/ Unit21
_, err = i.action.Create(instrument, "Creation", u21Response.Unit21Id, "Creation")
if err != nil {
log.Err(err).Msg("Error creating a new instrument action in Unit21")
return u21Response.Unit21Id, common.StringError(err)
}

return u21Response.Unit21Id, nil
}

Expand Down Expand Up @@ -125,7 +134,7 @@ func (i instrument) getSource(userId string) (source string, err error) {
log.Warn().Msg("No userId defined")
return
}
user, err := i.repo.User.GetById(userId)
user, err := i.repos.User.GetById(userId)
if err != nil {
log.Err(err).Msg("Failed go get user contacts")
return "", common.StringError(err)
Expand All @@ -143,7 +152,7 @@ func (i instrument) getEntities(userId string) (entity instrumentEntity, err err
return
}

user, err := i.repo.User.GetById(userId)
user, err := i.repos.User.GetById(userId)
if err != nil {
log.Err(err).Msg("Failed go get user contacts")
err = common.StringError(err)
Expand All @@ -164,7 +173,7 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum
return
}

devices, err := i.repo.Device.ListByUserId(userId, 100, 0)
devices, err := i.repos.Device.ListByUserId(userId, 100, 0)
if err != nil {
log.Err(err).Msg("Failed to get user devices")
err = common.StringError(err)
Expand All @@ -177,35 +186,36 @@ func (i instrument) getInstrumentDigitalData(userId string) (digitalData instrum
return
}

func (i instrument) getLocationData(locationId string) (locationData instrumentLocationData, err error) {
func (i instrument) getLocationData(locationId string) (locationData *instrumentLocationData, err error) {
if locationId == "" {
log.Warn().Msg("No locationId defined")
return
}

location, err := i.repo.Location.GetById(locationId)
location, err := i.repos.Location.GetById(locationId)
if err != nil {
log.Err(err).Msg("Failed go get instrument location")
err = common.StringError(err)
return
}

locationData = instrumentLocationData{
Type: location.Type,
BuildingNumber: location.BuildingNumber,
UnitNumber: location.UnitNumber,
StreetName: location.StreetName,
City: location.City,
State: location.State,
PostalCode: location.PostalCode,
Country: location.Country,
VerifiedOn: int(location.CreatedAt.Unix()),
if location.CreatedAt.Unix() != 0 {
locationData = &instrumentLocationData{
Type: location.Type,
BuildingNumber: location.BuildingNumber,
UnitNumber: location.UnitNumber,
StreetName: location.StreetName,
City: location.City,
State: location.State,
PostalCode: location.PostalCode,
Country: location.Country,
VerifiedOn: int(location.CreatedAt.Unix()),
}
}

return locationData, nil
}

func mapToUnit21Instrument(instrument model.Instrument, source string, entityData instrumentEntity, digitalData instrumentDigitalData, locationData instrumentLocationData) *u21Instrument {
func mapToUnit21Instrument(instrument model.Instrument, source string, entityData instrumentEntity, digitalData instrumentDigitalData, locationData *instrumentLocationData) *u21Instrument {
var instrumentTagArr []string
if instrument.Tags != nil {
for key, value := range instrument.Tags {
Expand All @@ -225,12 +235,10 @@ func mapToUnit21Instrument(instrument model.Instrument, source string, entityDat
RegisteredAt: int(instrument.CreatedAt.Unix()),
ParentInstrumentId: "",
Entities: entityArray,
CustomData: &instrumentCustomData{
None: nil,
},
DigitalData: &digitalData,
LocationData: &locationData,
Tags: instrumentTagArr,
CustomData: nil, //TODO: include platform in customData

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If CustomData is a pointer, theres no need to explicitly pass nil. Zero value of a pointer is nil

DigitalData: &digitalData,
LocationData: locationData,
Tags: instrumentTagArr,
// Options: &options,
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/internal/unit21/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ type Transaction interface {
Update(transaction model.Transaction) (unit21Id string, err error)
}

type TransactionRepo struct {
TxLeg repository.TxLeg
type TransactionRepos struct {
User repository.User
TxLeg repository.TxLeg
Asset repository.Asset
}

type transaction struct {
repo TransactionRepo
repos TransactionRepos
}

func NewTransaction(r TransactionRepo) Transaction {
return &transaction{repo: r}
func NewTransaction(r TransactionRepos) Transaction {
return &transaction{repos: r}
}

func (t transaction) Evaluate(transaction model.Transaction) (pass bool, err error) {
Expand Down Expand Up @@ -118,28 +118,28 @@ func (t transaction) Update(transaction model.Transaction) (unit21Id string, err
}

func (t transaction) getTransactionData(transaction model.Transaction) (txData transactionData, err error) {
senderData, err := t.repo.TxLeg.GetById(transaction.OriginTxLegID)
senderData, err := t.repos.TxLeg.GetById(transaction.OriginTxLegID)
if err != nil {
log.Err(err).Msg("Failed go get origin transaction leg")
err = common.StringError(err)
return
}

receiverData, err := t.repo.TxLeg.GetById(transaction.DestinationTxLegID)
receiverData, err := t.repos.TxLeg.GetById(transaction.DestinationTxLegID)
if err != nil {
log.Err(err).Msg("Failed go get origin transaction leg")
err = common.StringError(err)
return
}

senderAsset, err := t.repo.Asset.GetById(senderData.AssetID)
senderAsset, err := t.repos.Asset.GetById(senderData.AssetID)
if err != nil {
log.Err(err).Msg("Failed go get transaction sender asset")
err = common.StringError(err)
return
}

receiverAsset, err := t.repo.Asset.GetById(receiverData.AssetID)
receiverAsset, err := t.repos.Asset.GetById(receiverData.AssetID)
if err != nil {
log.Err(err).Msg("Failed go get transaction receiver asset")
err = common.StringError(err)
Expand Down
12 changes: 0 additions & 12 deletions pkg/model/common.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/model/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Device struct {
Type string `json:"type" db:"type"`
Description string `json:"description" db:"description"`
Fingerprint string `json:"fingerprint" db:"fingerprint"`
IpAddresses pq.StringArray `json:"ipAddresses" db:"ip_addresses"`
IpAddresses pq.StringArray `json:"ipAddresses,omitempty" db:"ip_addresses"`
UserID string `json:"userId" db:"user_id"`
}

Expand Down
1 change: 1 addition & 0 deletions pkg/service/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ type Services struct {
User User
Verification Verification
Device Device
Unit21 Unit21
}
12 changes: 8 additions & 4 deletions pkg/service/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@ func NewDevice(repos repository.Repositories, f Fingerprint) Device {
return &device{repos, f}
}

func (d device) createDevice(userID string, visitor model.FPVisitor, description string) (model.Device, error) {
func (d device) createDevice(userID string, visitor FPVisitor, description string) (model.Device, error) {
addresses := pq.StringArray{}
if visitor.IPAddress.String != "" {
addresses = pq.StringArray{visitor.IPAddress.String}
}

return d.repos.Device.Create(model.Device{
UserID: userID,
Fingerprint: visitor.VisitorID,
Type: visitor.Type,
IpAddresses: pq.StringArray{visitor.IPAddress},
IpAddresses: addresses,
Description: description,
LastUsedAt: time.Now(),
})
}

func (d device) CreateUnknownDevice(userID string) (model.Device, error) {
visitor := model.FPVisitor{
visitor := FPVisitor{
VisitorID: "unknown",
Type: "unknown",
IPAddress: "unknown",
UserAgent: "unknown",
}
device, err := d.createDevice(userID, visitor, "an unknown device")
Expand Down
15 changes: 12 additions & 3 deletions pkg/service/fingerprint.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package service

import (
"database/sql"
"errors"

"github.com/String-xyz/string-api/pkg/internal/common"
"github.com/String-xyz/string-api/pkg/model"
)

type FPClient common.FingerprintClient
type HTTPConfig common.HTTPConfig
type HTTPClient common.HTTPClient
type FPVisitor = model.FPVisitor
type FPVisitor struct {
VisitorID string
Country string
State string
IPAddress sql.NullString
Timestamp int64
Confidence float64
Type string
UserAgent string
}

func NewHTTPClient(config HTTPConfig) HTTPClient {
return common.NewHTTPClient(common.HTTPConfig(config))
Expand Down Expand Up @@ -59,7 +68,7 @@ func (f fingerprint) hydrateVisitor(visitor common.FPVisitor) (FPVisitor, error)
VisitorID: visitor.ID,
Country: visit.IPLocation.Coutry.Code,
State: state,
IPAddress: visit.IP,
IPAddress: sql.NullString{String: visit.IP},
Timestamp: visit.Timestamp,
Confidence: visit.IPLocation.Confidence.Score,
Type: visit.BrowserDetails.Device,
Expand Down
Loading