Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion server/app/admin_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -244,6 +245,26 @@ func (a *App) GetDlsCountHandler(req *http.Request) (interface{}, Response) {
}, Ok()
}

// GetPricesHandler return vms and public ip prices
// Example endpoint: Get vms and public ip prices
// @Summary Get vms and public ip prices
// @Description Get vms and public ip prices
// @Tags Admin
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} internal.Prices
// @Failure 400 {object} Response
// @Failure 401 {object} Response
// @Failure 500 {object} Response
// @Router /prices [get]
func (a *App) GetPricesHandler(req *http.Request) (interface{}, Response) {
return ResponseMsg{
Message: "Prices are found",
Data: a.config.PricesPerMonth,
}, Ok()
}

// GetBalanceHandler return account balance information
// Example endpoint: Get main TF account balance
// @Summary Get main TF account balance
Expand All @@ -266,7 +287,7 @@ func (a *App) GetBalanceHandler(req *http.Request) (interface{}, Response) {

return ResponseMsg{
Message: "Balance is found",
Data: balance,
Data: math.Round(balance*100) / 100,
}, Ok()
}

Expand Down
2 changes: 2 additions & 0 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (a *App) registerHandlers() {
voucherRouter := adminRouter.PathPrefix("/voucher").Subrouter()
maintenanceRouter := adminRouter.PathPrefix("/maintenance").Subrouter()
balanceRouter := adminRouter.PathPrefix("/balance").Subrouter()
pricesRouter := adminRouter.PathPrefix("/prices").Subrouter()
deploymentsRouter := adminRouter.PathPrefix("/deployments").Subrouter()
nextLaunchRouter := adminRouter.PathPrefix("/nextlaunch").Subrouter()

Expand Down Expand Up @@ -188,6 +189,7 @@ func (a *App) registerHandlers() {
adminRouter.HandleFunc("/set_admin", WrapFunc(a.SetAdminHandler)).Methods("PUT", "OPTIONS")
adminRouter.HandleFunc("/set_prices", WrapFunc(a.SetPricesHandler)).Methods("PUT", "OPTIONS")
balanceRouter.HandleFunc("", WrapFunc(a.GetBalanceHandler)).Methods("GET", "OPTIONS")
pricesRouter.HandleFunc("", WrapFunc(a.GetPricesHandler)).Methods("GET", "OPTIONS")
maintenanceRouter.HandleFunc("", WrapFunc(a.UpdateMaintenanceHandler)).Methods("PUT", "OPTIONS")
deploymentsRouter.HandleFunc("", WrapFunc(a.DeleteAllDeploymentsHandler)).Methods("DELETE", "OPTIONS")
deploymentsRouter.HandleFunc("/vm/{id}", WrapFunc(a.DeleteVMDeploymentHandler)).Methods("DELETE", "OPTIONS")
Expand Down
33 changes: 31 additions & 2 deletions server/app/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ func (a *App) VerifySignUpCodeHandler(req *http.Request) (interface{}, Response)
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

notification := models.Notification{UserID: user.ID.String(),
Msg: "Welcome! Your account has been created successfully",
}
err = a.db.CreateNotification(&notification)
if err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "Account is created successfully.",
}, Created()
Expand Down Expand Up @@ -1041,15 +1050,22 @@ func (a *App) DeleteUserHandler(req *http.Request) (interface{}, Response) {
}
}

// 6. Delete vouchers
err = a.db.DeleteUserVouchers(userID)
if err != nil && err != gorm.ErrRecordNotFound {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

// 7. Remove cards
err = a.db.DeleteAllCards(userID)
if err != nil && err != gorm.ErrRecordNotFound {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

// 6. TODO: should invoices be deleted?
// 8. TODO: should invoices be deleted?

// 7. Remove cards
err = a.db.DeleteUser(userID)
if err == gorm.ErrRecordNotFound {
return nil, NotFound(errors.New("user is not found"))
Expand All @@ -1059,6 +1075,19 @@ func (a *App) DeleteUserHandler(req *http.Request) (interface{}, Response) {
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

subject, body := internal.AdminMailContent(
"Your account has been deleted",
"We are writing to confirm that your account has been successfully deleted as per your request.\n\n"+
"All your personal data, account information, and associated content have been permanently removed from our system",
a.config.Server.Host, user.Name(),
)

err = internal.SendMail(a.config.MailSender.Email, a.config.MailSender.SendGridKey, user.Email, subject, body)
if err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "User is deleted successfully",
}, Ok()
Expand Down
60 changes: 60 additions & 0 deletions server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,46 @@ const docTemplate = `{
}
}
},
"/prices": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Get vms and public ip prices",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admin"
],
"summary": "Get vms and public ip prices",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/internal.Prices"
}
},
"400": {
"description": "Bad Request",
"schema": {}
},
"401": {
"description": "Unauthorized",
"schema": {}
},
"500": {
"description": "Internal Server Error",
"schema": {}
}
}
}
},
"/region": {
"get": {
"security": [
Expand Down Expand Up @@ -3154,6 +3194,23 @@ const docTemplate = `{
"voucherAndBalanceAndCard"
]
},
"internal.Prices": {
"type": "object",
"properties": {
"large_vm": {
"type": "number"
},
"medium_vm": {
"type": "number"
},
"public_ip": {
"type": "number"
},
"small_vm": {
"type": "number"
}
}
},
"models.Card": {
"type": "object",
"required": [
Expand Down Expand Up @@ -3394,6 +3451,9 @@ const docTemplate = `{
"user_id"
],
"properties": {
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
Expand Down
39 changes: 39 additions & 0 deletions server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ definitions:
- voucherAndCard
- balanceAndCard
- voucherAndBalanceAndCard
internal.Prices:
properties:
large_vm:
type: number
medium_vm:
type: number
public_ip:
type: number
small_vm:
type: number
type: object
models.Card:
properties:
brand:
Expand Down Expand Up @@ -491,6 +502,8 @@ definitions:
type: object
models.Notification:
properties:
created_at:
type: string
id:
type: integer
msg:
Expand Down Expand Up @@ -1401,6 +1414,32 @@ paths:
summary: Set user's notifications as seen
tags:
- Notification
/prices:
get:
consumes:
- application/json
description: Get vms and public ip prices
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/internal.Prices'
"400":
description: Bad Request
schema: {}
"401":
description: Unauthorized
schema: {}
"500":
description: Internal Server Error
schema: {}
security:
- BearerAuth: []
summary: Get vms and public ip prices
tags:
- Admin
/region:
get:
consumes:
Expand Down
10 changes: 3 additions & 7 deletions server/internal/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,9 @@
Welcome, -name-!
</h1>
<p style="margin: 0">
Your account has been created successfully. We are so glad to
have you here. You will receive a voucher to give you access
to your needed resources as requested.
</p>
<br /><br />
<p style="margin: 0">
The request will be processed within 12 hours.
Your account has been created successfully,
and you're now ready to explore deployments on our platform. We are so glad to
have you here.
</p>
</td>
</tr>
Expand Down
5 changes: 4 additions & 1 deletion server/models/notification.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package models for database models
package models

import "time"

const (
// VMsType deployment
VMsType = "vms"
Expand All @@ -15,7 +17,8 @@ type Notification struct {
Msg string `json:"msg" binding:"required"`
Seen bool `json:"seen" binding:"required"`
// to allow redirecting from notifications to the right pages
Type string `json:"type" binding:"required"`
Type string `json:"type" binding:"required"`
CreatedAt time.Time `json:"created_at"`
}

// ListNotifications returns a list of notifications for a user.
Expand Down
5 changes: 5 additions & 0 deletions server/models/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ func (d *DB) GetNotUsedVoucherByUserID(id string) (Voucher, error) {
var res Voucher
return res, d.db.Last(&res, "user_id = ? AND used = false", id).Error
}

func (d *DB) DeleteUserVouchers(userID string) error {
var vouchers []Voucher
return d.db.Clauses(clause.Returning{}).Where("user_id = ?", userID).Delete(&vouchers).Error
}
Loading