feat: expand superadmin edits and minio docs

This commit is contained in:
2026-01-17 19:32:46 +08:00
parent 3af5b20bcc
commit 984a404b5f
25 changed files with 2688 additions and 36 deletions

View File

@@ -2,6 +2,7 @@ package services
import (
"database/sql"
"encoding/json"
"errors"
"testing"
"time"
@@ -9,8 +10,10 @@ import (
"quyun/v2/app/commands/testx"
"quyun/v2/app/errorx"
super_dto "quyun/v2/app/http/super/v1/dto"
v1_dto "quyun/v2/app/http/v1/dto"
"quyun/v2/app/requests"
"quyun/v2/database"
"quyun/v2/database/fields"
"quyun/v2/database/models"
"quyun/v2/pkg/consts"
@@ -140,10 +143,12 @@ func (s *SuperTestSuite) Test_CreateTenant() {
models.UserQuery.WithContext(ctx).Create(u)
Convey("should create tenant", func() {
startAt := time.Now()
form := &super_dto.TenantCreateForm{
Name: "Super Tenant",
Code: "st1",
AdminUserID: u.ID,
Duration: 7,
}
err := Super.CreateTenant(ctx, form)
So(err, ShouldBeNil)
@@ -153,6 +158,13 @@ func (s *SuperTestSuite) Test_CreateTenant() {
So(t.Name, ShouldEqual, "Super Tenant")
So(t.UserID, ShouldEqual, u.ID)
So(t.Status, ShouldEqual, consts.TenantStatusVerified)
So(t.ExpiredAt.After(startAt), ShouldBeTrue)
tu, _ := models.TenantUserQuery.WithContext(ctx).
Where(models.TenantUserQuery.TenantID.Eq(t.ID), models.TenantUserQuery.UserID.Eq(u.ID)).
First()
So(tu, ShouldNotBeNil)
So(tu.Status, ShouldEqual, consts.UserStatusVerified)
})
})
}
@@ -1203,3 +1215,195 @@ func (s *SuperTestSuite) Test_PayoutAccountReview() {
})
})
}
func (s *SuperTestSuite) Test_UpdateUserProfile() {
Convey("UpdateUserProfile", s.T(), func() {
ctx := s.T().Context()
database.Truncate(ctx, s.DB, models.TableNameUser)
meta := types.JSON([]byte(`{"real_name":"Old Name","id_card":"ENC:1111"}`))
u := &models.User{
Username: "profile_user",
Nickname: "Old",
Avatar: "http://old-avatar",
Gender: consts.GenderSecret,
Bio: "old bio",
Metas: meta,
}
models.UserQuery.WithContext(ctx).Create(u)
Convey("should update profile fields and real-name meta", func() {
form := &super_dto.SuperUserProfileUpdateForm{
Nickname: lo.ToPtr("New Nick"),
Avatar: lo.ToPtr("http://new-avatar"),
Gender: lo.ToPtr(consts.GenderMale),
Bio: lo.ToPtr("new bio"),
IsRealNameVerified: lo.ToPtr(true),
RealName: lo.ToPtr("New Name"),
IDCard: lo.ToPtr("123456789012345678"),
}
err := Super.UpdateUserProfile(ctx, 1001, u.ID, form)
So(err, ShouldBeNil)
updated, err := models.UserQuery.WithContext(ctx).Where(models.UserQuery.ID.Eq(u.ID)).First()
So(err, ShouldBeNil)
So(updated.Nickname, ShouldEqual, "New Nick")
So(updated.Avatar, ShouldEqual, "http://new-avatar")
So(updated.Gender, ShouldEqual, consts.GenderMale)
So(updated.Bio, ShouldEqual, "new bio")
So(updated.IsRealNameVerified, ShouldBeTrue)
So(updated.VerifiedAt.IsZero(), ShouldBeFalse)
metaMap := make(map[string]interface{})
So(json.Unmarshal(updated.Metas, &metaMap), ShouldBeNil)
So(metaMap["real_name"], ShouldEqual, "New Name")
So(metaMap["id_card"], ShouldEqual, "ENC:123456789012345678")
})
})
}
func (s *SuperTestSuite) Test_UpdateCreatorSettings() {
Convey("UpdateCreatorSettings", s.T(), func() {
ctx := s.T().Context()
database.Truncate(ctx, s.DB, models.TableNameTenant, models.TableNameUser)
owner := &models.User{Username: "settings_owner"}
models.UserQuery.WithContext(ctx).Create(owner)
tenant := &models.Tenant{
UserID: owner.ID,
Name: "Old Tenant",
Code: "creator-settings",
Status: consts.TenantStatusVerified,
Config: types.NewJSONType(fields.TenantConfig{}),
}
models.TenantQuery.WithContext(ctx).Create(tenant)
form := &v1_dto.Settings{
Name: "New Tenant",
Bio: "new bio",
Avatar: "http://avatar",
Cover: "http://cover",
Description: "new description",
}
err := Super.UpdateCreatorSettings(ctx, 2001, tenant.ID, form)
So(err, ShouldBeNil)
updated, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.ID.Eq(tenant.ID)).First()
So(err, ShouldBeNil)
So(updated.Name, ShouldEqual, "New Tenant")
cfg := updated.Config.Data()
So(cfg.Bio, ShouldEqual, "new bio")
So(cfg.Avatar, ShouldEqual, "http://avatar")
So(cfg.Cover, ShouldEqual, "http://cover")
So(cfg.Description, ShouldEqual, "new description")
})
}
func (s *SuperTestSuite) Test_PayoutAccountCreateUpdate() {
Convey("PayoutAccountCreateUpdate", s.T(), func() {
ctx := s.T().Context()
database.Truncate(ctx, s.DB, models.TableNamePayoutAccount, models.TableNameUser, models.TableNameTenant)
admin := &models.User{Username: "payout_admin_2"}
owner := &models.User{Username: "payout_owner_2"}
models.UserQuery.WithContext(ctx).Create(admin, owner)
tenant := &models.Tenant{
UserID: owner.ID,
Name: "Payout Tenant 2",
Code: "payout-2",
Status: consts.TenantStatusVerified,
}
models.TenantQuery.WithContext(ctx).Create(tenant)
Convey("should create payout account", func() {
err := Super.CreatePayoutAccount(ctx, admin.ID, tenant.ID, &super_dto.SuperPayoutAccountCreateForm{
UserID: owner.ID,
Type: "bank",
Name: "Bank",
Account: "123",
Realname: "Owner",
})
So(err, ShouldBeNil)
account, err := models.PayoutAccountQuery.WithContext(ctx).
Where(models.PayoutAccountQuery.TenantID.Eq(tenant.ID), models.PayoutAccountQuery.UserID.Eq(owner.ID)).
First()
So(err, ShouldBeNil)
So(account.Status, ShouldEqual, consts.PayoutAccountStatusPending)
})
Convey("should reset status when updating approved account", func() {
account := &models.PayoutAccount{
TenantID: tenant.ID,
UserID: owner.ID,
Type: consts.PayoutAccountTypeBank,
Name: "Bank",
Account: "111",
Realname: "Owner",
Status: consts.PayoutAccountStatusApproved,
ReviewedBy: admin.ID,
ReviewReason: "ok",
ReviewedAt: time.Now(),
}
models.PayoutAccountQuery.WithContext(ctx).Create(account)
err := Super.UpdatePayoutAccount(ctx, admin.ID, account.ID, &super_dto.SuperPayoutAccountUpdateForm{
Account: lo.ToPtr("222"),
})
So(err, ShouldBeNil)
updated, err := models.PayoutAccountQuery.WithContext(ctx).Where(models.PayoutAccountQuery.ID.Eq(account.ID)).First()
So(err, ShouldBeNil)
So(updated.Account, ShouldEqual, "222")
So(updated.Status, ShouldEqual, consts.PayoutAccountStatusPending)
So(updated.ReviewedBy, ShouldEqual, int64(0))
So(updated.ReviewReason, ShouldEqual, "")
})
})
}
func (s *SuperTestSuite) Test_UpdateNotificationTemplate() {
Convey("UpdateNotificationTemplate", s.T(), func() {
ctx := s.T().Context()
database.Truncate(ctx, s.DB, models.TableNameNotificationTemplate, models.TableNameTenant, models.TableNameUser)
owner := &models.User{Username: "tmpl_owner"}
models.UserQuery.WithContext(ctx).Create(owner)
tenant := &models.Tenant{
UserID: owner.ID,
Name: "Template Tenant",
Code: "tmpl",
Status: consts.TenantStatusVerified,
}
models.TenantQuery.WithContext(ctx).Create(tenant)
tmpl := &models.NotificationTemplate{
TenantID: tenant.ID,
Name: "Old Template",
Type: consts.NotificationTypeSystem,
Title: "Old Title",
Content: "Old Content",
IsActive: true,
}
models.NotificationTemplateQuery.WithContext(ctx).Create(tmpl)
item, err := Super.UpdateNotificationTemplate(ctx, 3001, tmpl.ID, &super_dto.SuperNotificationTemplateUpdateForm{
Name: lo.ToPtr("New Template"),
Title: lo.ToPtr("New Title"),
Content: lo.ToPtr("New Content"),
IsActive: lo.ToPtr(false),
})
So(err, ShouldBeNil)
So(item.Name, ShouldEqual, "New Template")
So(item.IsActive, ShouldBeFalse)
updated, err := models.NotificationTemplateQuery.WithContext(ctx).Where(models.NotificationTemplateQuery.ID.Eq(tmpl.ID)).First()
So(err, ShouldBeNil)
So(updated.Title, ShouldEqual, "New Title")
So(updated.Content, ShouldEqual, "New Content")
So(updated.IsActive, ShouldBeFalse)
})
}