2025-01-29 19:12:54 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2025-02-20 21:08:44 +08:00
|
|
|
"time"
|
|
|
|
|
2025-02-18 23:12:51 +08:00
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
|
2025-01-29 19:12:54 +08:00
|
|
|
|
|
|
|
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
|
|
|
|
wproto "git.solsynth.dev/hypernet/wallet/pkg/proto"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/samber/lo"
|
2025-01-29 19:14:37 +08:00
|
|
|
"github.com/spf13/viper"
|
2025-01-29 19:12:54 +08:00
|
|
|
)
|
|
|
|
|
2025-02-18 23:12:51 +08:00
|
|
|
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).
|
2025-02-20 21:08:44 +08:00
|
|
|
Where("created_at >= ?", deadline).
|
2025-02-18 23:12:51 +08:00
|
|
|
Select("SUM(size)").
|
|
|
|
Scan(&totalSize).Error; err != nil {
|
|
|
|
return totalSize, err
|
|
|
|
}
|
|
|
|
return totalSize, nil
|
|
|
|
}
|
|
|
|
|
2025-01-29 19:12:54 +08:00
|
|
|
// PlaceOrder create a transaction if needed for user
|
|
|
|
// Pricing according here: https://kb.solsynth.dev/solar-network/wallet#file-uploads
|
|
|
|
func PlaceOrder(user uint, filesize int64, withDiscount bool) error {
|
2025-02-18 23:12:51 +08:00
|
|
|
currentBytes, _ := GetLastDayUploadedBytes(user)
|
2025-01-29 19:14:37 +08:00
|
|
|
discountFileSize := viper.GetInt64("payment.discount")
|
|
|
|
|
2025-02-18 23:12:51 +08:00
|
|
|
if currentBytes+filesize <= discountFileSize {
|
2025-01-29 19:12:54 +08:00
|
|
|
// Discount included
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var amount float64
|
|
|
|
if withDiscount {
|
2025-02-18 23:12:51 +08:00
|
|
|
amount = float64(filesize) / 1024 / 1024 * 1
|
2025-01-29 19:12:54 +08:00
|
|
|
} else {
|
|
|
|
amount = float64(filesize) / 1024 / 1024 * 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !withDiscount {
|
|
|
|
amount += 10 // Service fee
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := gap.Nx.GetClientGrpcConn("wa")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to connect wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wc := wproto.NewPaymentServiceClient(conn)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
|
|
|
defer cancel()
|
|
|
|
resp, err := wc.MakeTransactionWithAccount(ctx, &wproto.MakeTransactionWithAccountRequest{
|
|
|
|
PayerAccountId: lo.ToPtr(uint64(user)),
|
|
|
|
Amount: amount,
|
|
|
|
Remark: "File Uploading Fee",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().
|
|
|
|
Uint64("transaction", resp.Id).Float64("amount", amount).Bool("discount", withDiscount).
|
|
|
|
Msg("Order placed for charge file uploading fee...")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|