]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - api/market_config.go
Add column 'status' to market_configs.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / market_config.go
index 3fd10ae000d19fba89f6e86b4911a1dde21bf294..e7b2341760f0d4a64df680767de8b0ea2afeb5ff 100644 (file)
@@ -2,8 +2,12 @@ package api
 
 import (
        "fmt"
+       "strings"
+       "time"
 
+       "github.com/jloup/utils"
        "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
+       "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets"
 )
 
 type MarketConfigQuery struct {
@@ -39,9 +43,67 @@ func (q MarketConfigQuery) Run() (interface{}, *Error) {
 
        }
 
+       if _, ok := config.Config["key"]; !ok {
+               config.Config["key"] = ""
+       }
+
+       if _, ok := config.Config["secret"]; !ok {
+               config.Config["secret"] = ""
+       }
+
        return config.Config, nil
 }
 
+type TestMarketCredentialsQuery struct {
+       In struct {
+               User   db.User
+               Market string
+       }
+}
+
+func (q TestMarketCredentialsQuery) ValidateParams() *Error {
+       if q.In.Market != "poloniex" {
+               return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
+       }
+
+       return nil
+}
+
+func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
+       config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
+       if err != nil {
+               return nil, NewInternalError(err)
+       }
+
+       if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
+               return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
+       }
+
+       resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
+               err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"])
+
+               if utils.ErrIs(err, markets.InvalidCredentials) {
+                       return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
+               }
+
+               if utils.ErrIs(err, markets.IPRestricted) {
+                       return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
+               }
+
+               if err != nil {
+                       return NewInternalError(err)
+               }
+
+               return nil
+       })
+
+       if resultErr != nil {
+               return nil, NewInternalError(resultErr)
+       }
+
+       return nil, nil
+}
+
 type UpdateMarketConfigQuery struct {
        In struct {
                User   db.User
@@ -56,6 +118,9 @@ func (q UpdateMarketConfigQuery) ValidateParams() *Error {
                return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
        }
 
+       q.In.Secret = strings.TrimSpace(q.In.Secret)
+       q.In.Key = strings.TrimSpace(q.In.Key)
+
        return nil
 }
 
@@ -68,10 +133,74 @@ func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
                configMap["secret"] = q.In.Secret
        }
 
-       _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
+       marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
+       if err != nil {
+               return nil, NewInternalError(err)
+       }
+
+       resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
+               err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"])
+
+               if utils.ErrIs(err, markets.InvalidCredentials) {
+                       return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
+               }
+
+               if utils.ErrIs(err, markets.IPRestricted) {
+                       return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
+               }
+
+               if err != nil {
+                       return NewInternalError(err)
+               }
+
+               return nil
+       })
+
+       var newStatus db.MarketConfigStatus = db.MarketConfigEnabled
+
+       if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) {
+               newStatus = db.MarketConfigInvalidCredentials
+       } else if resultErr != nil {
+               return nil, NewInternalError(resultErr)
+       }
+
+       marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus)
        if err != nil {
                return nil, NewInternalError(err)
        }
 
        return nil, nil
 }
+
+type GetPortfolioQuery struct {
+       In struct {
+               User   db.User
+               Market string
+       }
+}
+
+func (q GetPortfolioQuery) ValidateParams() *Error {
+       if q.In.Market != "poloniex" {
+               return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
+       }
+
+       return nil
+}
+
+func (q GetPortfolioQuery) Run() (interface{}, *Error) {
+       marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
+       if err != nil {
+               return nil, NewInternalError(err)
+       }
+
+       report, err := GetWeekPortfolio(*marketConfig)
+       if ErrorIs(err, NotFound) {
+               return nil, err.(*Error)
+       }
+
+       if err != nil {
+               return nil, NewInternalError(err)
+       }
+
+       return report.Round(), nil
+}