8 "github.com/jloup/utils"
9 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
10 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets"
13 type MarketConfigQuery struct {
20 func (q MarketConfigQuery) ValidateParams() *Error {
21 if q.In.Market != "poloniex" {
22 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
28 func (q MarketConfigQuery) Run() (interface{}, *Error) {
29 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
31 return nil, NewInternalError(err)
35 configMap := make(map[string]string)
37 configMap["secret"] = ""
39 config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
41 return nil, NewInternalError(err)
46 if _, ok := config.Config["key"]; !ok {
47 config.Config["key"] = ""
50 if _, ok := config.Config["secret"]; !ok {
51 config.Config["secret"] = ""
54 return config.Config, nil
57 type TestMarketCredentialsQuery struct {
64 func (q TestMarketCredentialsQuery) ValidateParams() *Error {
65 if q.In.Market != "poloniex" {
66 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
72 func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
73 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
75 return nil, NewInternalError(err)
78 if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
79 return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
82 resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
83 err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"])
85 if utils.ErrIs(err, markets.InvalidCredentials) {
86 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
89 if utils.ErrIs(err, markets.IPRestricted) {
90 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
94 return NewInternalError(err)
100 if resultErr != nil {
101 return nil, NewInternalError(resultErr)
107 type UpdateMarketConfigQuery struct {
116 func (q UpdateMarketConfigQuery) ValidateParams() *Error {
117 if q.In.Market == "" {
118 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
121 q.In.Secret = strings.TrimSpace(q.In.Secret)
122 q.In.Key = strings.TrimSpace(q.In.Key)
127 func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
128 configMap := make(map[string]string)
130 configMap["key"] = q.In.Key
132 if q.In.Secret != "" {
133 configMap["secret"] = q.In.Secret
136 marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
138 return nil, NewInternalError(err)
141 resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
142 err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"])
144 if utils.ErrIs(err, markets.InvalidCredentials) {
145 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
148 if utils.ErrIs(err, markets.IPRestricted) {
149 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
153 return NewInternalError(err)
159 var newStatus db.MarketConfigStatus = db.MarketConfigEnabled
161 if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) {
162 newStatus = db.MarketConfigInvalidCredentials
163 } else if resultErr != nil {
164 return nil, NewInternalError(resultErr)
167 marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus)
169 return nil, NewInternalError(err)
175 type GetPortfolioQuery struct {
182 func (q GetPortfolioQuery) ValidateParams() *Error {
183 if q.In.Market != "poloniex" {
184 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
190 func (q GetPortfolioQuery) Run() (interface{}, *Error) {
191 marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
193 return nil, NewInternalError(err)
196 report, err := GetWeekPortfolio(*marketConfig)
197 if ErrorIs(err, NotFound) {
198 return nil, err.(*Error)
202 return nil, NewInternalError(err)
205 return report.Round(), nil