Skip to content

Commit c7d4a22

Browse files
committed
add voucher duration to the voucher model and set it on voucher creation
1 parent aab9a04 commit c7d4a22

4 files changed

Lines changed: 41 additions & 27 deletions

File tree

server/app/user_handler.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ type EmailInput struct {
6666

6767
// ApplyForVoucherInput struct for user to apply for voucher
6868
type ApplyForVoucherInput struct {
69-
VMs int `json:"vms" binding:"required" validate:"min=0"`
70-
PublicIPs int `json:"public_ips" binding:"required" validate:"min=0"`
71-
Reason string `json:"reason" binding:"required" validate:"nonzero"`
69+
VMs int `json:"vms" binding:"required" validate:"min=0"`
70+
PublicIPs int `json:"public_ips" binding:"required" validate:"min=0"`
71+
Reason string `json:"reason" binding:"required" validate:"nonzero"`
72+
VoucherDuration int `json:"voucher_duration" binding:"required"`
7273
}
7374

7475
// AddVoucherInput struct for voucher applied by user
@@ -80,7 +81,6 @@ type AddVoucherInput struct {
8081
func (a *App) SignUpHandler(req *http.Request) (interface{}, Response) {
8182
var signUp SignUpInput
8283
err := json.NewDecoder(req.Body).Decode(&signUp)
83-
8484
if err != nil {
8585
log.Error().Err(err).Send()
8686
return nil, BadRequest(errors.New("failed to read sign up data"))
@@ -573,14 +573,19 @@ func (a *App) ApplyForVoucherHandler(req *http.Request) (interface{}, Response)
573573
return nil, BadRequest(errors.New("invalid voucher data"))
574574
}
575575

576+
if input.VoucherDuration > a.config.VouchersMaxDuration {
577+
return nil, BadRequest(fmt.Errorf("invalid voucher duration, max duration is %d", a.config.VouchersMaxDuration))
578+
}
579+
576580
// generate voucher for user but can't use it until admin approves it
577581
v := internal.GenerateRandomVoucher(5)
578582
voucher := models.Voucher{
579-
Voucher: v,
580-
UserID: userID,
581-
VMs: input.VMs,
582-
Reason: input.Reason,
583-
PublicIPs: input.PublicIPs,
583+
Voucher: v,
584+
UserID: userID,
585+
VMs: input.VMs,
586+
Reason: input.Reason,
587+
PublicIPs: input.PublicIPs,
588+
VoucherDuration: input.VoucherDuration,
584589
}
585590

586591
err = a.db.CreateVoucher(&voucher)

server/app/voucher_handler.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package app
44
import (
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"net/http"
89
"strconv"
910

@@ -17,9 +18,10 @@ import (
1718

1819
// GenerateVoucherInput struct for data needed when user generate vouchers
1920
type GenerateVoucherInput struct {
20-
Length int `json:"length" binding:"required" validate:"min=3,max=20"`
21-
VMs int `json:"vms" binding:"required"`
22-
PublicIPs int `json:"public_ips" binding:"required"`
21+
Length int `json:"length" binding:"required" validate:"min=3,max=20"`
22+
VMs int `json:"vms" binding:"required"`
23+
PublicIPs int `json:"public_ips" binding:"required"`
24+
VoucherDuration int `json:"voucher_duration" binding:"required"`
2325
}
2426

2527
// UpdateVoucherInput struct for data needed when user update voucher
@@ -43,11 +45,16 @@ func (a *App) GenerateVoucherHandler(req *http.Request) (interface{}, Response)
4345
}
4446
voucher := internal.GenerateRandomVoucher(input.Length)
4547

48+
if input.VoucherDuration > a.config.VouchersMaxDuration {
49+
return nil, BadRequest(fmt.Errorf("invalid voucher duration, max duration is %d", a.config.VouchersMaxDuration))
50+
}
51+
4652
v := models.Voucher{
47-
Voucher: voucher,
48-
VMs: input.VMs,
49-
PublicIPs: input.PublicIPs,
50-
Approved: true,
53+
Voucher: voucher,
54+
VMs: input.VMs,
55+
PublicIPs: input.PublicIPs,
56+
VoucherDuration: input.VoucherDuration,
57+
Approved: true,
5158
}
5259

5360
err = a.db.CreateVoucher(&v)

server/internal/config_parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Configuration struct {
2121
NotifyAdminsIntervalHours int `json:"notifyAdminsIntervalHours"`
2222
AdminSSHKey string `json:"adminSSHKey"`
2323
BalanceThreshold int `json:"balanceThreshold"`
24+
VouchersMaxDuration int `json:"vouchersDuration"`
2425
}
2526

2627
// Server struct to hold server's information

server/models/voucher.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import "time"
55

66
// Voucher struct holds data of vouchers
77
type Voucher struct {
8-
ID int `json:"id" gorm:"primaryKey"`
9-
UserID string `json:"user_id" binding:"required"`
10-
Voucher string `json:"voucher" gorm:"unique"`
11-
VMs int `json:"vms" binding:"required"`
12-
PublicIPs int `json:"public_ips" binding:"required"`
13-
Reason string `json:"reason" binding:"required"`
14-
Used bool `json:"used" binding:"required"`
15-
Approved bool `json:"approved" binding:"required"`
16-
Rejected bool `json:"rejected" binding:"required"`
17-
CreatedAt time.Time `json:"created_at"`
18-
UpdatedAt time.Time `json:"updated_at"`
8+
ID int `json:"id" gorm:"primaryKey"`
9+
UserID string `json:"user_id" binding:"required"`
10+
Voucher string `json:"voucher" gorm:"unique"`
11+
VMs int `json:"vms" binding:"required"`
12+
PublicIPs int `json:"public_ips" binding:"required"`
13+
Reason string `json:"reason" binding:"required"`
14+
Used bool `json:"used" binding:"required"`
15+
Approved bool `json:"approved" binding:"required"`
16+
Rejected bool `json:"rejected" binding:"required"`
17+
VoucherDuration int `json:"voucher_duration" binding:"required"`
18+
CreatedAt time.Time `json:"created_at"`
19+
UpdatedAt time.Time `json:"updated_at"`
1920
}

0 commit comments

Comments
 (0)