Skip to content

Commit 6e8c33f

Browse files
committed
feat: better admin user subscription management ui
1 parent 140bed5 commit 6e8c33f

23 files changed

+5217
-3804
lines changed

admin/controller.go

+31-22
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package admin
22

33
import (
44
"chat/utils"
5-
"github.com/gin-gonic/gin"
65
"net/http"
76
"strconv"
87
"strings"
8+
"time"
9+
10+
"github.com/gin-gonic/gin"
911
)
1012

1113
type GenerateInvitationForm struct {
@@ -44,27 +46,26 @@ type BanForm struct {
4446
}
4547

4648
type QuotaOperationForm struct {
47-
Id int64 `json:"id" binding:"required"`
48-
Quota float32 `json:"quota" binding:"required"`
49-
Override bool `json:"override"`
50-
}
49+
Id int64 `json:"id" binding:"required"`
50+
Quota *float32 `json:"quota" binding:"required"`
51+
Override bool `json:"override"`}
5152

5253
type SubscriptionOperationForm struct {
53-
Id int64 `json:"id"`
54-
Month int64 `json:"month"`
54+
Id int64 `json:"id" binding:"required"`
55+
Expired string `json:"expired" binding:"required"`
5556
}
5657

5758
type SubscriptionLevelForm struct {
58-
Id int64 `json:"id"`
59-
Level int64 `json:"level"`
59+
Id int64 `json:"id" binding:"required"`
60+
Level *int64 `json:"level" binding:"required"`
6061
}
6162

6263
type ReleaseUsageForm struct {
63-
Id int64 `json:"id"`
64+
Id int64 `json:"id" binding:"required"`
6465
}
6566

6667
type UpdateRootPasswordForm struct {
67-
Password string `json:"password"`
68+
Password string `json:"password" binding:"required"`
6869
}
6970

7071
func UpdateMarketAPI(c *gin.Context) {
@@ -337,7 +338,7 @@ func UserQuotaAPI(c *gin.Context) {
337338
return
338339
}
339340

340-
err := quotaMigration(db, form.Id, form.Quota, form.Override)
341+
err := quotaMigration(db, form.Id, *form.Quota, form.Override)
341342
if err != nil {
342343
c.JSON(http.StatusOK, gin.H{
343344
"status": false,
@@ -363,18 +364,26 @@ func UserSubscriptionAPI(c *gin.Context) {
363364
return
364365
}
365366

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

376+
if err := subscriptionMigration(db, form.Id, form.Expired); err != nil {
375377
c.JSON(http.StatusOK, gin.H{
376-
"status": true,
378+
"status": false,
379+
"message": err.Error(),
377380
})
381+
return
382+
}
383+
384+
c.JSON(http.StatusOK, gin.H{
385+
"status": true,
386+
})
378387
}
379388

380389
func SubscriptionLevelAPI(c *gin.Context) {
@@ -389,7 +398,7 @@ func SubscriptionLevelAPI(c *gin.Context) {
389398
return
390399
}
391400

392-
err := subscriptionLevelMigration(db, form.Id, form.Level)
401+
err := subscriptionLevelMigration(db, form.Id, *form.Level)
393402
if err != nil {
394403
c.JSON(http.StatusOK, gin.H{
395404
"status": false,

admin/market.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package admin
33
import (
44
"chat/globals"
55
"fmt"
6+
67
"github.com/spf13/viper"
78
)
89

@@ -12,7 +13,7 @@ type MarketModel struct {
1213
Name string `json:"name" mapstructure:"name" required:"true"`
1314
Description string `json:"description" mapstructure:"description"`
1415
Default bool `json:"default" mapstructure:"default"`
15-
HighContext bool `json:"high_context" mapstructure:"high_context"`
16+
HighContext bool `json:"high_context" mapstructure:"highcontext"`
1617
Avatar string `json:"avatar" mapstructure:"avatar"`
1718
Tag ModelTag `json:"tag" mapstructure:"tag"`
1819
}

admin/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type UserData struct {
7777
IsAdmin bool `json:"is_admin"`
7878
Quota float32 `json:"quota"`
7979
UsedQuota float32 `json:"used_quota"`
80+
ExpiredAt string `json:"expired_at"`
8081
IsSubscribed bool `json:"is_subscribed"`
8182
TotalMonth int64 `json:"total_month"`
8283
Enterprise bool `json:"enterprise"`

admin/user.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"context"
88
"database/sql"
99
"fmt"
10-
"github.com/go-redis/redis/v8"
1110
"math"
1211
"strings"
1312
"time"
13+
14+
"github.com/go-redis/redis/v8"
1415
)
1516

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

174-
func subscriptionMigration(db *sql.DB, id int64, month int64) error {
175-
// if month is negative, then decrease month
176-
// if month is positive, then increase month
177-
178-
expireAt := time.Now().AddDate(0, int(month), 0)
179-
176+
func subscriptionMigration(db *sql.DB, id int64, expired string) error {
180177
_, err := globals.ExecDb(db, `
181-
INSERT INTO subscription (user_id, total_month, expired_at) VALUES (?, ?, ?)
182-
ON DUPLICATE KEY UPDATE total_month = total_month + ?, expired_at = DATE_ADD(expired_at, INTERVAL ? MONTH)
183-
`, id, month, expireAt, month, month)
184-
178+
INSERT INTO subscription (user_id, expired_at) VALUES (?, ?)
179+
ON DUPLICATE KEY UPDATE expired_at = ?
180+
`, id, expired, expired)
185181
return err
186182
}
187183

app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"next-themes": "^0.2.1",
5353
"react": "^18.2.0",
5454
"react-beautiful-dnd": "^13.1.1",
55+
"react-day-picker": "^8.10.0",
5556
"react-dom": "^18.2.0",
5657
"react-i18next": "^13.2.2",
5758
"react-markdown": "^8.0.7",

0 commit comments

Comments
 (0)