5
5
"encoding/json"
6
6
"fmt"
7
7
"math/big"
8
- "strconv"
9
8
10
9
"github.com/ethereum/go-ethereum/log"
11
10
"github.com/gin-gonic/gin"
@@ -19,15 +18,13 @@ import (
19
18
// AdminController handles admin API requests
20
19
type AdminController struct {
21
20
cfg * config.Config
22
- db * gorm.DB
23
21
policyOrm * orm.Policy
24
22
}
25
23
26
24
// NewAdminController creates a new AdminController
27
25
func NewAdminController (cfg * config.Config , db * gorm.DB ) * AdminController {
28
26
return & AdminController {
29
27
cfg : cfg ,
30
- db : db ,
31
28
policyOrm : orm .NewPolicy (db ),
32
29
}
33
30
}
@@ -63,7 +60,7 @@ func (ac *AdminController) handleListPolicies(c *gin.Context, req types.Paymaste
63
60
var result []types.PolicyResponse
64
61
for _ , policy := range policies {
65
62
result = append (result , types.PolicyResponse {
66
- PolicyID : strconv . FormatInt ( policy .PolicyID , 10 ) ,
63
+ PolicyID : policy .PolicyID ,
67
64
PolicyName : policy .PolicyName ,
68
65
Limits : policy .Limits ,
69
66
CreatedAt : policy .CreatedAt .Format ("2006-01-02T15:04:05Z" ),
@@ -78,25 +75,26 @@ func (ac *AdminController) handleListPolicies(c *gin.Context, req types.Paymaste
78
75
// handleGetPolicyByID retrieves a specific policy by ID
79
76
func (ac * AdminController ) handleGetPolicyByID (c * gin.Context , req types.PaymasterJSONRPCRequest , apiKey string ) {
80
77
var params struct {
81
- PolicyID string `json:"policy_id"`
78
+ PolicyID * int64 `json:"policy_id"`
82
79
}
83
80
84
81
if err := json .Unmarshal (req .Params , & params ); err != nil {
85
82
types .SendError (c , req .ID , types .InvalidParamsCode , "Invalid params structure" )
86
83
return
87
84
}
88
85
89
- if params .PolicyID == "" {
86
+ if params .PolicyID == nil {
90
87
types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id is required" )
91
88
return
92
89
}
93
90
94
- policyID , err := strconv .ParseInt (params .PolicyID , 10 , 64 )
95
- if err != nil {
96
- types .SendError (c , req .ID , types .InvalidParamsCode , "Invalid policy_id format" )
91
+ if * params .PolicyID < 0 {
92
+ types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id must be positive" )
97
93
return
98
94
}
99
95
96
+ policyID := * params .PolicyID
97
+
100
98
policy , err := ac .policyOrm .GetByAPIKeyAndPolicyID (c .Request .Context (), apiKey , policyID )
101
99
if err != nil {
102
100
if err == gorm .ErrRecordNotFound {
@@ -109,7 +107,7 @@ func (ac *AdminController) handleGetPolicyByID(c *gin.Context, req types.Paymast
109
107
}
110
108
111
109
result := types.PolicyResponse {
112
- PolicyID : strconv . FormatInt ( policy .PolicyID , 10 ) ,
110
+ PolicyID : policy .PolicyID ,
113
111
PolicyName : policy .PolicyName ,
114
112
Limits : policy .Limits ,
115
113
CreatedAt : policy .CreatedAt .Format ("2006-01-02T15:04:05Z" ),
@@ -123,7 +121,7 @@ func (ac *AdminController) handleGetPolicyByID(c *gin.Context, req types.Paymast
123
121
// handleCreatePolicy creates a new policy
124
122
func (ac * AdminController ) handleCreatePolicy (c * gin.Context , req types.PaymasterJSONRPCRequest , apiKey string ) {
125
123
var params struct {
126
- PolicyID string `json:"policy_id"`
124
+ PolicyID * int64 `json:"policy_id"`
127
125
PolicyName string `json:"policy_name"`
128
126
Limits orm.PolicyLimits `json:"limits"`
129
127
}
@@ -133,24 +131,25 @@ func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.Paymaste
133
131
return
134
132
}
135
133
136
- if params .PolicyID == "" {
134
+ if params .PolicyID == nil {
137
135
types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id is required" )
138
136
return
139
137
}
140
138
141
- if params .PolicyName == "" {
142
- types .SendError (c , req .ID , types .InvalidParamsCode , "policy_name is required " )
139
+ if * params .PolicyID < 0 {
140
+ types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id must be positive " )
143
141
return
144
142
}
145
143
146
- policyID , err := strconv .ParseInt (params .PolicyID , 10 , 64 )
147
- if err != nil {
148
- types .SendError (c , req .ID , types .InvalidParamsCode , "Invalid policy_id format" )
144
+ policyID := * params .PolicyID
145
+
146
+ if params .PolicyName == "" {
147
+ types .SendError (c , req .ID , types .InvalidParamsCode , "policy_name is required" )
149
148
return
150
149
}
151
150
152
151
// Validate limits
153
- if err = ac .validatePolicyLimits (& params .Limits ); err != nil {
152
+ if err : = ac .validatePolicyLimits (& params .Limits ); err != nil {
154
153
types .SendError (c , req .ID , types .PolicyValidationErrorCode , "Invalid limits: " + err .Error ())
155
154
return
156
155
}
@@ -162,8 +161,7 @@ func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.Paymaste
162
161
Limits : params .Limits ,
163
162
}
164
163
165
- err = ac .policyOrm .Create (c .Request .Context (), newPolicy )
166
- if err != nil {
164
+ if err := ac .policyOrm .Create (c .Request .Context (), newPolicy ); err != nil {
167
165
log .Error ("Failed to create policy" , "error" , err , "apiKey" , apiKey )
168
166
types .SendError (c , req .ID , types .InternalServerError , "Failed to create policy" )
169
167
return
@@ -181,7 +179,7 @@ func (ac *AdminController) handleCreatePolicy(c *gin.Context, req types.Paymaste
181
179
// handleUpdatePolicy updates an existing policy
182
180
func (ac * AdminController ) handleUpdatePolicy (c * gin.Context , req types.PaymasterJSONRPCRequest , apiKey string ) {
183
181
var params struct {
184
- PolicyID string `json:"policy_id"`
182
+ PolicyID * int64 `json:"policy_id"`
185
183
PolicyName * string `json:"policy_name,omitempty"`
186
184
Limits * orm.PolicyLimits `json:"limits,omitempty"`
187
185
}
@@ -191,28 +189,28 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
191
189
return
192
190
}
193
191
194
- if params .PolicyID == "" {
192
+ if params .PolicyID == nil {
195
193
types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id is required" )
196
194
return
197
195
}
198
196
199
- policyID , err := strconv .ParseInt (params .PolicyID , 10 , 64 )
200
- if err != nil {
201
- types .SendError (c , req .ID , types .InvalidParamsCode , "Invalid policy_id format" )
197
+ if * params .PolicyID < 0 {
198
+ types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id must be positive" )
202
199
return
203
200
}
204
201
202
+ policyID := * params .PolicyID
203
+
205
204
// Validate limits if provided
206
205
if params .Limits != nil {
207
- if err = ac .validatePolicyLimits (params .Limits ); err != nil {
206
+ if err : = ac .validatePolicyLimits (params .Limits ); err != nil {
208
207
types .SendError (c , req .ID , types .PolicyValidationErrorCode , "Invalid limits: " + err .Error ())
209
208
return
210
209
}
211
210
}
212
211
213
212
// Check if policy exists
214
- _ , err = ac .policyOrm .GetByAPIKeyAndPolicyID (c .Request .Context (), apiKey , policyID )
215
- if err != nil {
213
+ if _ , err := ac .policyOrm .GetByAPIKeyAndPolicyID (c .Request .Context (), apiKey , policyID ); err != nil {
216
214
if err == gorm .ErrRecordNotFound {
217
215
types .SendError (c , req .ID , types .PolicyNotFoundCode , "Policy not found" )
218
216
return
@@ -229,7 +227,7 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
229
227
}
230
228
if params .Limits != nil {
231
229
var limitsJSON []byte
232
- limitsJSON , err = json .Marshal (* params .Limits )
230
+ limitsJSON , err : = json .Marshal (* params .Limits )
233
231
if err != nil {
234
232
types .SendError (c , req .ID , types .InternalErrorCode , "Failed to serialize limits" )
235
233
return
@@ -243,8 +241,7 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
243
241
}
244
242
245
243
// Perform update
246
- err = ac .policyOrm .Update (c .Request .Context (), apiKey , policyID , updates )
247
- if err != nil {
244
+ if err := ac .policyOrm .Update (c .Request .Context (), apiKey , policyID , updates ); err != nil {
248
245
log .Error ("Failed to update policy" , "error" , err , "apiKey" , apiKey , "policyID" , policyID )
249
246
types .SendError (c , req .ID , types .InternalServerError , "Failed to update policy" )
250
247
return
@@ -262,28 +259,28 @@ func (ac *AdminController) handleUpdatePolicy(c *gin.Context, req types.Paymaste
262
259
// handleDeletePolicy deletes a policy
263
260
func (ac * AdminController ) handleDeletePolicy (c * gin.Context , req types.PaymasterJSONRPCRequest , apiKey string ) {
264
261
var params struct {
265
- PolicyID string `json:"policy_id"`
262
+ PolicyID * int64 `json:"policy_id"`
266
263
}
267
264
268
265
if err := json .Unmarshal (req .Params , & params ); err != nil {
269
266
types .SendError (c , req .ID , types .InvalidParamsCode , "Invalid params structure" )
270
267
return
271
268
}
272
269
273
- if params .PolicyID == "" {
270
+ if params .PolicyID == nil {
274
271
types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id is required" )
275
272
return
276
273
}
277
274
278
- policyID , err := strconv .ParseInt (params .PolicyID , 10 , 64 )
279
- if err != nil {
280
- types .SendError (c , req .ID , types .InvalidParamsCode , "Invalid policy_id format" )
275
+ if * params .PolicyID < 0 {
276
+ types .SendError (c , req .ID , types .InvalidParamsCode , "policy_id must be positive" )
281
277
return
282
278
}
283
279
280
+ policyID := * params .PolicyID
281
+
284
282
// Check if policy exists
285
- _ , err = ac .policyOrm .GetByAPIKeyAndPolicyID (c .Request .Context (), apiKey , policyID )
286
- if err != nil {
283
+ if _ , err := ac .policyOrm .GetByAPIKeyAndPolicyID (c .Request .Context (), apiKey , policyID ); err != nil {
287
284
if err == gorm .ErrRecordNotFound {
288
285
types .SendError (c , req .ID , types .PolicyNotFoundCode , "Policy not found" )
289
286
return
@@ -294,8 +291,7 @@ func (ac *AdminController) handleDeletePolicy(c *gin.Context, req types.Paymaste
294
291
}
295
292
296
293
// Perform deletion (soft delete)
297
- err = ac .policyOrm .Delete (c .Request .Context (), apiKey , policyID )
298
- if err != nil {
294
+ if err := ac .policyOrm .Delete (c .Request .Context (), apiKey , policyID ); err != nil {
299
295
log .Error ("Failed to delete policy" , "error" , err , "apiKey" , apiKey , "policyID" , policyID )
300
296
types .SendError (c , req .ID , types .InternalServerError , "Failed to delete policy" )
301
297
return
0 commit comments