]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/market_config.go
Set newStatus to enabled if no result are returned from credentials test.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / market_config.go
CommitLineData
7a9e5112 1package api
2
3import (
4 "fmt"
2f91f20a 5 "strings"
6 "time"
7a9e5112 7
1d68446a 8 "git.immae.eu/Cryptoportfolio/Front.git/db"
9 "git.immae.eu/Cryptoportfolio/Front.git/markets"
4495b984 10 "github.com/jloup/utils"
7a9e5112 11)
12
13type MarketConfigQuery struct {
14 In struct {
15 User db.User
16 Market string
17 }
18}
19
20func (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)}
23 }
24
25 return nil
26}
27
28func (q MarketConfigQuery) Run() (interface{}, *Error) {
29 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
30 if err != nil {
31 return nil, NewInternalError(err)
32 }
33
34 if config == nil {
35 configMap := make(map[string]string)
36 configMap["key"] = ""
37 configMap["secret"] = ""
38
39 config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
40 if err != nil {
41 return nil, NewInternalError(err)
42 }
43
44 }
45
16e43cc7 46 if _, ok := config.Config["key"]; !ok {
47 config.Config["key"] = ""
48 }
49
50 if _, ok := config.Config["secret"]; !ok {
51 config.Config["secret"] = ""
52 }
53
7a9e5112 54 return config.Config, nil
55}
56
24e47979 57type TestMarketCredentialsQuery struct {
2f91f20a 58 In struct {
24e47979 59 User db.User
60 Market string
2f91f20a 61 }
62}
63
24e47979 64func (q TestMarketCredentialsQuery) ValidateParams() *Error {
2f91f20a 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)}
67 }
68
2f91f20a 69 return nil
70}
71
24e47979 72func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
2f91f20a 73 config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
74 if err != nil {
75 return nil, NewInternalError(err)
76 }
77
299b6b6d 78 if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
c6aa553f 79 return nil, &Error{MarketCredentialsNotConfigured, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
2f91f20a 80 }
81
299b6b6d 82 resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
c086df91 83 err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"], EXTERNAL_SERVICE_TIMEOUT_SECONDS)
2f91f20a 84
85 if utils.ErrIs(err, markets.InvalidCredentials) {
86 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
87 }
88
50c6eea6 89 if utils.ErrIs(err, markets.IPRestricted) {
90 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
2f91f20a 91 }
92
2f91f20a 93 if err != nil {
94 return NewInternalError(err)
95 }
96
2f91f20a 97 return nil
98 })
99
4495b984 100 var newStatus db.MarketConfigStatus = config.Status
101
102 if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
103 newStatus = db.MarketConfigInvalidCredentials
104 } else if resultErr != nil {
105 return nil, NewInternalError(resultErr)
106 } else if resultErr != nil {
299b6b6d 107 return nil, NewInternalError(resultErr)
73bad1e9 108 } else if resultErr == nil {
109 newStatus = db.MarketConfigEnabled
2f91f20a 110 }
111
4495b984 112 if newStatus != config.Status {
113 config, err = db.SetMarketConfigStatus(*config, newStatus)
114 if err != nil {
115 return nil, NewInternalError(err)
116 }
117 }
118
24e47979 119 return nil, nil
2f91f20a 120}
121
7a9e5112 122type UpdateMarketConfigQuery struct {
123 In struct {
124 User db.User
125 Market string
126 Key string
127 Secret string
128 }
129}
130
131func (q UpdateMarketConfigQuery) ValidateParams() *Error {
132 if q.In.Market == "" {
133 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
134 }
135
2f91f20a 136 q.In.Secret = strings.TrimSpace(q.In.Secret)
137 q.In.Key = strings.TrimSpace(q.In.Key)
138
7a9e5112 139 return nil
140}
141
142func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
143 configMap := make(map[string]string)
144 if q.In.Key != "" {
145 configMap["key"] = q.In.Key
146 }
147 if q.In.Secret != "" {
148 configMap["secret"] = q.In.Secret
149 }
150
299b6b6d 151 marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
152 if err != nil {
153 return nil, NewInternalError(err)
154 }
155
156 resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
c086df91 157 err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"], EXTERNAL_SERVICE_TIMEOUT_SECONDS)
299b6b6d 158
159 if utils.ErrIs(err, markets.InvalidCredentials) {
160 return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
161 }
162
163 if utils.ErrIs(err, markets.IPRestricted) {
164 return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
165 }
166
167 if err != nil {
168 return NewInternalError(err)
169 }
170
171 return nil
172 })
173
174 var newStatus db.MarketConfigStatus = db.MarketConfigEnabled
175
4495b984 176 if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
299b6b6d 177 newStatus = db.MarketConfigInvalidCredentials
178 } else if resultErr != nil {
179 return nil, NewInternalError(resultErr)
180 }
181
182 marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus)
7a9e5112 183 if err != nil {
184 return nil, NewInternalError(err)
185 }
186
187 return nil, nil
188}
24e47979 189
190type GetPortfolioQuery struct {
191 In struct {
192 User db.User
193 Market string
194 }
195}
196
197func (q GetPortfolioQuery) ValidateParams() *Error {
198 if q.In.Market != "poloniex" {
199 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
200 }
201
202 return nil
203}
204
205func (q GetPortfolioQuery) Run() (interface{}, *Error) {
206 marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market)
207 if err != nil {
208 return nil, NewInternalError(err)
209 }
210
211 report, err := GetWeekPortfolio(*marketConfig)
212 if ErrorIs(err, NotFound) {
213 return nil, err.(*Error)
214 }
215
216 if err != nil {
217 return nil, NewInternalError(err)
218 }
219
220 return report.Round(), nil
221}