Skip to content

Commit

Permalink
make voucher balance fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Feb 12, 2025
1 parent dcfeca3 commit c75afcd
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 20 deletions.
3 changes: 2 additions & 1 deletion server/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func SetUp(t testing.TB) *App {
"medium_vm": 20,
"large_vm": 30
},
"stripe_secret": "sk_test"
"stripe_secret": "sk_test",
"voucher_balance": 10
}
`, dbPath)

Expand Down
5 changes: 2 additions & 3 deletions server/app/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ type EmailInput struct {

// ApplyForVoucherInput struct for user to apply for voucher
type ApplyForVoucherInput struct {
Balance uint64 `json:"balance" binding:"required" validate:"min=0"`
Reason string `json:"reason" binding:"required" validate:"nonzero"`
Reason string `json:"reason" binding:"required" validate:"nonzero"`
}

// AddVoucherInput struct for voucher applied by user
Expand Down Expand Up @@ -711,7 +710,7 @@ func (a *App) ApplyForVoucherHandler(req *http.Request) (interface{}, Response)
voucher := models.Voucher{
Voucher: v,
UserID: userID,
Balance: input.Balance,
Balance: a.config.VoucherBalance,
Reason: input.Reason,
}

Expand Down
2 changes: 0 additions & 2 deletions server/app/user_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,6 @@ func TestApplyForVoucherHandler(t *testing.T) {
assert.NoError(t, err)

voucherBody := []byte(`{
"vms":10,
"public_ips":1,
"reason":"strongReason"
}`)

Expand Down
5 changes: 0 additions & 5 deletions server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2712,14 +2712,9 @@ const docTemplate = `{
"app.ApplyForVoucherInput": {
"type": "object",
"required": [
"balance",
"reason"
],
"properties": {
"balance": {
"type": "integer",
"minimum": 0
},
"reason": {
"type": "string"
}
Expand Down
4 changes: 0 additions & 4 deletions server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ definitions:
type: object
app.ApplyForVoucherInput:
properties:
balance:
minimum: 0
type: integer
reason:
type: string
required:
- balance
- reason
type: object
app.ChangePasswordInput:
Expand Down
1 change: 1 addition & 0 deletions server/internal/config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Configuration struct {
PricesPerMonth Prices `json:"prices"`
Currency string `json:"currency" validate:"nonzero"`
StripeSecret string `json:"stripe_secret" validate:"nonzero"`
VoucherBalance uint64 `json:"voucher_balance" validate:"nonzero"`
}

// Server struct to hold server's information
Expand Down
3 changes: 2 additions & 1 deletion server/internal/config_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ var rightConfig = `
"medium_vm": 20,
"large_vm": 30
},
"stripe_secret": "sk_test"
"stripe_secret": "sk_test",
"voucher_balance": 10
}
`

Expand Down
8 changes: 4 additions & 4 deletions server/models/deployments_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func (d *DB) CountAllDeployments() (DeploymentsCount, error) {
dlsCount := k8sCount + vmsCount

var vmIPsCount int64
result = d.db.Table("vms").Where("public_ip = true").Count(&vmIPsCount)
result = d.db.Table("vms").Where("public = true").Count(&vmIPsCount)
if result.Error != nil {
return DeploymentsCount{}, result.Error
}

var k8sIPsCount int64
result = d.db.Table("masters").Where("public_ip = true").Count(&k8sIPsCount)
result = d.db.Table("masters").Where("public = true").Count(&k8sIPsCount)
if result.Error != nil {
return DeploymentsCount{}, result.Error
}
Expand Down Expand Up @@ -61,14 +61,14 @@ func (d *DB) CountUserDeployments(userID string) (DeploymentsCount, error) {
dlsCount := k8sCount + vmsCount

var vmIPsCount int64
result = d.db.Table("vms").Where("public_ip = true").Where("user_id = ?", userID).Count(&vmIPsCount)
result = d.db.Table("vms").Where("public = true").Where("user_id = ?", userID).Count(&vmIPsCount)
if result.Error != nil {
return DeploymentsCount{}, result.Error
}

var k8sIPsCount int64
result = d.db.Table("k8s_clusters").Joins("JOIN masters ON k8s_clusters.id = masters.cluster_id").
Where("public_ip = true").Where("user_id = ?", userID).Count(&k8sIPsCount)
Where("public = true").Where("user_id = ?", userID).Count(&k8sIPsCount)
if result.Error != nil {
return DeploymentsCount{}, result.Error
}
Expand Down

0 comments on commit c75afcd

Please sign in to comment.