X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=api%2Fmarket_config.go;h=e7b2341760f0d4a64df680767de8b0ea2afeb5ff;hb=299b6b6d9fb879c06e675ef240f361348629ff6c;hp=81a92d1e6d35fb476b9ddd79ff97e6f487347122;hpb=3b8833854f83f75e3d16c1fdb869937f690e48ea;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/api/market_config.go b/api/market_config.go index 81a92d1..e7b2341 100644 --- a/api/market_config.go +++ b/api/market_config.go @@ -75,11 +75,11 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { return nil, NewInternalError(err) } - if config.Config["key"] == "" || config.Config["secret"] == "" { + 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' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error { + 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) { @@ -98,7 +98,7 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { }) if resultErr != nil { - return nil, resultErr + return nil, NewInternalError(resultErr) } return nil, nil @@ -133,7 +133,38 @@ 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) }