Skip to content

Commit

Permalink
add voucher duration to the voucher model and set it on voucher creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Dec 4, 2023
1 parent aab9a04 commit 60893dd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
23 changes: 14 additions & 9 deletions server/app/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ type EmailInput struct {

// ApplyForVoucherInput struct for user to apply for voucher
type ApplyForVoucherInput struct {
VMs int `json:"vms" binding:"required" validate:"min=0"`
PublicIPs int `json:"public_ips" binding:"required" validate:"min=0"`
Reason string `json:"reason" binding:"required" validate:"nonzero"`
VMs int `json:"vms" binding:"required" validate:"min=0"`
PublicIPs int `json:"public_ips" binding:"required" validate:"min=0"`
Reason string `json:"reason" binding:"required" validate:"nonzero"`
VoucherDuration int `json:"voucher_duration" binding:"required"`
}

// AddVoucherInput struct for voucher applied by user
Expand All @@ -80,7 +81,6 @@ type AddVoucherInput struct {
func (a *App) SignUpHandler(req *http.Request) (interface{}, Response) {
var signUp SignUpInput
err := json.NewDecoder(req.Body).Decode(&signUp)

if err != nil {
log.Error().Err(err).Send()
return nil, BadRequest(errors.New("failed to read sign up data"))
Expand Down Expand Up @@ -573,14 +573,19 @@ func (a *App) ApplyForVoucherHandler(req *http.Request) (interface{}, Response)
return nil, BadRequest(errors.New("invalid voucher data"))
}

if input.VoucherDuration > a.config.VouchersMaxDuration {
return nil, BadRequest(fmt.Errorf("invalid voucher duration, max duration is %d", a.config.VouchersMaxDuration))
}

// generate voucher for user but can't use it until admin approves it
v := internal.GenerateRandomVoucher(5)
voucher := models.Voucher{
Voucher: v,
UserID: userID,
VMs: input.VMs,
Reason: input.Reason,
PublicIPs: input.PublicIPs,
Voucher: v,
UserID: userID,
VMs: input.VMs,
Reason: input.Reason,
PublicIPs: input.PublicIPs,
VoucherDuration: input.VoucherDuration,
}

err = a.db.CreateVoucher(&voucher)
Expand Down
21 changes: 14 additions & 7 deletions server/app/voucher_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package app
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"

Expand All @@ -17,9 +18,10 @@ import (

// GenerateVoucherInput struct for data needed when user generate vouchers
type GenerateVoucherInput struct {
Length int `json:"length" binding:"required" validate:"min=3,max=20"`
VMs int `json:"vms" binding:"required"`
PublicIPs int `json:"public_ips" binding:"required"`
Length int `json:"length" binding:"required" validate:"min=3,max=20"`
VMs int `json:"vms" binding:"required"`
PublicIPs int `json:"public_ips" binding:"required"`
VoucherDuration int `json:"voucher_duration" binding:"required"`
}

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

if input.VoucherDuration > a.config.VouchersMaxDuration {
return nil, BadRequest(fmt.Errorf("invalid voucher duration, max duration is %d", a.config.VouchersMaxDuration))
}

v := models.Voucher{
Voucher: voucher,
VMs: input.VMs,
PublicIPs: input.PublicIPs,
Approved: true,
Voucher: voucher,
VMs: input.VMs,
PublicIPs: input.PublicIPs,
VoucherDuration: input.VoucherDuration,
Approved: true,
}

err = a.db.CreateVoucher(&v)
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 @@ -21,6 +21,7 @@ type Configuration struct {
NotifyAdminsIntervalHours int `json:"notifyAdminsIntervalHours"`
AdminSSHKey string `json:"adminSSHKey"`
BalanceThreshold int `json:"balanceThreshold"`
VouchersMaxDuration int `json:"voucherMaxDuration"`
}

// Server struct to hold server's information
Expand Down
23 changes: 12 additions & 11 deletions server/models/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import "time"

// Voucher struct holds data of vouchers
type Voucher struct {
ID int `json:"id" gorm:"primaryKey"`
UserID string `json:"user_id" binding:"required"`
Voucher string `json:"voucher" gorm:"unique"`
VMs int `json:"vms" binding:"required"`
PublicIPs int `json:"public_ips" binding:"required"`
Reason string `json:"reason" binding:"required"`
Used bool `json:"used" binding:"required"`
Approved bool `json:"approved" binding:"required"`
Rejected bool `json:"rejected" binding:"required"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ID int `json:"id" gorm:"primaryKey"`
UserID string `json:"user_id" binding:"required"`
Voucher string `json:"voucher" gorm:"unique"`
VMs int `json:"vms" binding:"required"`
PublicIPs int `json:"public_ips" binding:"required"`
Reason string `json:"reason" binding:"required"`
Used bool `json:"used" binding:"required"`
Approved bool `json:"approved" binding:"required"`
Rejected bool `json:"rejected" binding:"required"`
VoucherDuration int `json:"voucher_duration" binding:"required"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

0 comments on commit 60893dd

Please sign in to comment.