Skip to content

Commit

Permalink
feat: better admin user subscription management ui
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaomaiTX committed Jul 6, 2024
1 parent 140bed5 commit 6e8c33f
Show file tree
Hide file tree
Showing 23 changed files with 5,217 additions and 3,804 deletions.
53 changes: 31 additions & 22 deletions admin/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package admin

import (
"chat/utils"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
)

type GenerateInvitationForm struct {
Expand Down Expand Up @@ -44,27 +46,26 @@ type BanForm struct {
}

type QuotaOperationForm struct {
Id int64 `json:"id" binding:"required"`
Quota float32 `json:"quota" binding:"required"`
Override bool `json:"override"`
}
Id int64 `json:"id" binding:"required"`
Quota *float32 `json:"quota" binding:"required"`
Override bool `json:"override"`}

type SubscriptionOperationForm struct {
Id int64 `json:"id"`
Month int64 `json:"month"`
Id int64 `json:"id" binding:"required"`
Expired string `json:"expired" binding:"required"`
}

type SubscriptionLevelForm struct {
Id int64 `json:"id"`
Level int64 `json:"level"`
Id int64 `json:"id" binding:"required"`
Level *int64 `json:"level" binding:"required"`
}

type ReleaseUsageForm struct {
Id int64 `json:"id"`
Id int64 `json:"id" binding:"required"`
}

type UpdateRootPasswordForm struct {
Password string `json:"password"`
Password string `json:"password" binding:"required"`
}

func UpdateMarketAPI(c *gin.Context) {
Expand Down Expand Up @@ -337,7 +338,7 @@ func UserQuotaAPI(c *gin.Context) {
return
}

err := quotaMigration(db, form.Id, form.Quota, form.Override)
err := quotaMigration(db, form.Id, *form.Quota, form.Override)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
Expand All @@ -363,18 +364,26 @@ func UserSubscriptionAPI(c *gin.Context) {
return
}

err := subscriptionMigration(db, form.Id, form.Month)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"message": err.Error(),
})
return
}
// convert to time
if _, err := time.Parse("2006-01-02 15:04:05", form.Expired); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
"message": err.Error(),
})
return
}

if err := subscriptionMigration(db, form.Id, form.Expired); err != nil {
c.JSON(http.StatusOK, gin.H{
"status": true,
"status": false,
"message": err.Error(),
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": true,
})
}

func SubscriptionLevelAPI(c *gin.Context) {
Expand All @@ -389,7 +398,7 @@ func SubscriptionLevelAPI(c *gin.Context) {
return
}

err := subscriptionLevelMigration(db, form.Id, form.Level)
err := subscriptionLevelMigration(db, form.Id, *form.Level)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,
Expand Down
3 changes: 2 additions & 1 deletion admin/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin
import (
"chat/globals"
"fmt"

"github.com/spf13/viper"
)

Expand All @@ -12,7 +13,7 @@ type MarketModel struct {
Name string `json:"name" mapstructure:"name" required:"true"`
Description string `json:"description" mapstructure:"description"`
Default bool `json:"default" mapstructure:"default"`
HighContext bool `json:"high_context" mapstructure:"high_context"`
HighContext bool `json:"high_context" mapstructure:"highcontext"`
Avatar string `json:"avatar" mapstructure:"avatar"`
Tag ModelTag `json:"tag" mapstructure:"tag"`
}
Expand Down
1 change: 1 addition & 0 deletions admin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type UserData struct {
IsAdmin bool `json:"is_admin"`
Quota float32 `json:"quota"`
UsedQuota float32 `json:"used_quota"`
ExpiredAt string `json:"expired_at"`
IsSubscribed bool `json:"is_subscribed"`
TotalMonth int64 `json:"total_month"`
Enterprise bool `json:"enterprise"`
Expand Down
18 changes: 7 additions & 11 deletions admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"context"
"database/sql"
"fmt"
"github.com/go-redis/redis/v8"
"math"
"strings"
"time"

"github.com/go-redis/redis/v8"
)

// AuthLike is to solve the problem of import cycle
Expand Down Expand Up @@ -97,6 +98,7 @@ func getUsersForm(db *sql.DB, page int64, search string) PaginationForm {
stamp := utils.ConvertTime(expired)
if stamp != nil {
user.IsSubscribed = stamp.After(time.Now())
user.ExpiredAt = stamp.Format("2006-01-02 15:04:05")
}
user.Enterprise = isEnterprise.Valid && isEnterprise.Bool
user.IsBanned = isBanned.Valid && isBanned.Bool
Expand Down Expand Up @@ -171,17 +173,11 @@ func quotaMigration(db *sql.DB, id int64, quota float32, override bool) error {
return err
}

func subscriptionMigration(db *sql.DB, id int64, month int64) error {
// if month is negative, then decrease month
// if month is positive, then increase month

expireAt := time.Now().AddDate(0, int(month), 0)

func subscriptionMigration(db *sql.DB, id int64, expired string) error {
_, err := globals.ExecDb(db, `
INSERT INTO subscription (user_id, total_month, expired_at) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE total_month = total_month + ?, expired_at = DATE_ADD(expired_at, INTERVAL ? MONTH)
`, id, month, expireAt, month, month)

INSERT INTO subscription (user_id, expired_at) VALUES (?, ?)
ON DUPLICATE KEY UPDATE expired_at = ?
`, id, expired, expired)
return err
}

Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"next-themes": "^0.2.1",
"react": "^18.2.0",
"react-beautiful-dnd": "^13.1.1",
"react-day-picker": "^8.10.0",
"react-dom": "^18.2.0",
"react-i18next": "^13.2.2",
"react-markdown": "^8.0.7",
Expand Down
Loading

0 comments on commit 6e8c33f

Please sign in to comment.