Skip to content

Commit

Permalink
chore(add amdin real name info query)
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 committed Feb 14, 2025
1 parent 7257c6e commit 3bc46b4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
6 changes: 6 additions & 0 deletions controllers/pkg/database/cockroach/accountv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,9 @@ func (c *Cockroach) GetUserRealNameInfoByUserID(userID string) (*types.UserRealN
// get user realname info
var userRealNameInfo types.UserRealNameInfo
if err := c.DB.Where(&types.UserRealNameInfo{UserUID: user.UserUID}).First(&userRealNameInfo).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, gorm.ErrRecordNotFound
}
return nil, fmt.Errorf("failed to get user real name info: %w", err)
}
return &userRealNameInfo, nil
Expand All @@ -1201,6 +1204,9 @@ func (c *Cockroach) GetEnterpriseRealNameInfoByUserID(userID string) (*types.Ent
// get user realname info
var enterpriseRealNameInfo types.EnterpriseRealNameInfo
if err := c.DB.Where(&types.EnterpriseRealNameInfo{UserUID: user.UserUID}).First(&enterpriseRealNameInfo).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, gorm.ErrRecordNotFound
}
return nil, fmt.Errorf("failed to get enterprise real name info: %w", err)
}
return &enterpriseRealNameInfo, nil
Expand Down
55 changes: 55 additions & 0 deletions service/account/api/admin.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package api

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/labring/sealos/controllers/pkg/types"
"github.com/labring/sealos/service/account/dao"
"github.com/labring/sealos/service/account/helper"
"gorm.io/gorm"
)

// GetAccount
Expand Down Expand Up @@ -73,6 +77,57 @@ func AdminChargeBilling(c *gin.Context) {
})
}

// AdminGetUserRealNameInfo
// @Summary Get user real name info
// @Description Get user real name info
// @Tags Account
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "successfully retrieved user real name info"
// @Failure 401 {object} map[string]interface{} "authenticate error"
// @Failure 500 {object} map[string]interface{} "failed to get user real name info"
// @Router /admin/v1alpha1/real-name-info [get]
func AdminGetUserRealNameInfo(c *gin.Context) {
err := authenticateAdminRequest(c)
if err != nil {
c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)})
return
}
userUID, exist := c.GetQuery("userUID")
if !exist || userUID == "" {
c.JSON(http.StatusBadRequest, helper.ErrorMessage{Error: "empty userUID"})
return
}
userID, err := dao.DBClient.GetUserID(types.UserQueryOpts{UID: uuid.MustParse(userUID)})
if err != nil {
c.JSON(http.StatusInternalServerError, helper.ErrorMessage{Error: fmt.Sprintf("failed to get user ID: %v", err)})
return
}
ck := dao.DBClient.GetCockroach()

userRealNameInfo, err := ck.GetUserRealNameInfoByUserID(userID)

if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusInternalServerError, helper.ErrorMessage{Error: fmt.Sprintf("failed to get user real name info: %v", err)})
return
}

enterpriseRealNameInfo, err := ck.GetEnterpriseRealNameInfoByUserID(userID)

if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusInternalServerError, helper.ErrorMessage{Error: fmt.Sprintf("failed to get enterprise real name info: %v", err)})
return
}

isVerified := (userRealNameInfo != nil && userRealNameInfo.IsVerified) ||
(enterpriseRealNameInfo != nil && enterpriseRealNameInfo.IsVerified)

c.JSON(http.StatusOK, gin.H{
"userUID": userUID,
"isRealName": isVerified,
})
}

// ActiveBilling
// @Summary Active billing
// @Description Active billing
Expand Down
16 changes: 7 additions & 9 deletions service/account/dao/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dao

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -73,6 +72,7 @@ type Interface interface {
ReconcileActiveBilling(startTime, endTime time.Time) error
ArchiveHourlyBilling(hourStart, hourEnd time.Time) error
ActiveBilling(req resources.ActiveBilling) error
GetCockroach() *cockroach.Cockroach
}

type Account struct {
Expand All @@ -93,6 +93,10 @@ type Cockroach struct {
ck *cockroach.Cockroach
}

func (g *Cockroach) GetCockroach() *cockroach.Cockroach {
return g.ck
}

func (g *Cockroach) GetAccount(ops types.UserQueryOpts) (*types.Account, error) {
account, err := g.ck.GetAccount(&ops)
if err != nil {
Expand Down Expand Up @@ -1576,10 +1580,7 @@ func (m *Account) GetUserRealNameInfo(req *helper.GetRealNameInfoReq) (*types.Us
userRealNameInfo, err := m.ck.GetUserRealNameInfoByUserID(req.UserID)

if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, gorm.ErrRecordNotFound
}
return nil, fmt.Errorf("failed to get user real name info: %v", err)
return nil, err
}

return userRealNameInfo, nil
Expand All @@ -1590,10 +1591,7 @@ func (m *Account) GetEnterpriseRealNameInfo(req *helper.GetRealNameInfoReq) (*ty
enterpriseRealNameInfo, err := m.ck.GetEnterpriseRealNameInfoByUserID(req.UserID)

if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, gorm.ErrRecordNotFound
}
return nil, fmt.Errorf("failed to get enterprise real name info: %v", err)
return nil, err
}

return enterpriseRealNameInfo, nil
Expand Down
1 change: 1 addition & 0 deletions service/account/helper/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
AdminGetAccountWithWorkspace = "/account-with-workspace"
AdminChargeBilling = "/charge-billing"
AdminActiveBilling = "/active-billing"
AdminGetUserRealNameInfo = "/real-name-info"
)

// env
Expand Down
1 change: 1 addition & 0 deletions service/account/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func RegisterPayRouter() {
POST(helper.GetUserRealNameInfo, api.GetUserRealNameInfo)
router.Group(helper.AdminGroup).
GET(helper.AdminGetAccountWithWorkspace, api.AdminGetAccountWithWorkspaceID).
GET(helper.AdminGetUserRealNameInfo, api.AdminGetUserRealNameInfo).
POST(helper.AdminChargeBilling, api.AdminChargeBilling)
//POST(helper.AdminActiveBilling, api.AdminActiveBilling)
docs.SwaggerInfo.Host = env.GetEnvWithDefault("SWAGGER_HOST", "localhost:2333")
Expand Down

0 comments on commit 3bc46b4

Please sign in to comment.