Compare commits

..

No commits in common. "aa0f4d292e0200f7c2d5d34542673ee53d2a028d" and "22070a464b7ceb15cefeac6b32844c0717cde944" have entirely different histories.

3 changed files with 5 additions and 40 deletions

View File

@ -2,7 +2,6 @@ package api
import ( import (
"fmt" "fmt"
"github.com/spf13/viper"
"strings" "strings"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
@ -14,25 +13,6 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func getBillingStatus(c *fiber.Ctx) error {
if err := sec.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("nex_user").(*sec.UserInfo)
currentBytes, err := services.GetLastDayUploadedBytes(user.ID)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
discountFileSize := viper.GetInt64("payment.discount")
return c.JSON(fiber.Map{
"current_bytes": currentBytes,
"discount_file_size": discountFileSize,
"included_ratio": float64(currentBytes) / float64(discountFileSize),
})
}
func openAttachment(c *fiber.Ctx) error { func openAttachment(c *fiber.Ctx) error {
id := c.Params("id") id := c.Params("id")
region := c.Query("region") region := c.Query("region")

View File

@ -9,7 +9,6 @@ func MapAPIs(app *fiber.App, baseURL string) {
api := app.Group(baseURL).Name("API") api := app.Group(baseURL).Name("API")
{ {
api.Get("/destinations", listDestination) api.Get("/destinations", listDestination)
api.Get("/billing", getBillingStatus)
boost := api.Group("/boosts").Name("Boosts API") boost := api.Group("/boosts").Name("Boosts API")
{ {

View File

@ -3,8 +3,6 @@ package services
import ( import (
"context" "context"
"fmt" "fmt"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"time" "time"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
@ -14,34 +12,22 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func GetLastDayUploadedBytes(user uint) (int64, error) {
deadline := time.Now().Add(-24 * time.Hour)
var totalSize int64
if err := database.C.
Model(&models.Attachment{}).
Where("account_id = ?", user).
Where("created_at <= ?", deadline).
Select("SUM(size)").
Scan(&totalSize).Error; err != nil {
return totalSize, err
}
return totalSize, nil
}
// PlaceOrder create a transaction if needed for user // PlaceOrder create a transaction if needed for user
// Pricing according here: https://kb.solsynth.dev/solar-network/wallet#file-uploads // Pricing according here: https://kb.solsynth.dev/solar-network/wallet#file-uploads
func PlaceOrder(user uint, filesize int64, withDiscount bool) error { func PlaceOrder(user uint, filesize int64, withDiscount bool) error {
currentBytes, _ := GetLastDayUploadedBytes(user)
discountFileSize := viper.GetInt64("payment.discount") discountFileSize := viper.GetInt64("payment.discount")
if currentBytes+filesize <= discountFileSize { if filesize <= discountFileSize && withDiscount {
// Discount included // Discount included
return nil return nil
} }
var amount float64 var amount float64
if withDiscount { if withDiscount {
amount = float64(filesize) / 1024 / 1024 * 1 billableSize := filesize - discountFileSize
amount = float64(billableSize) / 1024 / 1024 * 1
} else if filesize > discountFileSize {
amount = 50 + float64(filesize-discountFileSize)/1024/1024*5
} else { } else {
amount = float64(filesize) / 1024 / 1024 * 1 amount = float64(filesize) / 1024 / 1024 * 1
} }