diff --git a/controllers/pkg/database/cockroach/accountv2.go b/controllers/pkg/database/cockroach/accountv2.go index f331269e41df..92b6065e89fa 100644 --- a/controllers/pkg/database/cockroach/accountv2.go +++ b/controllers/pkg/database/cockroach/accountv2.go @@ -1188,3 +1188,20 @@ func (c *Cockroach) GetUserRealNameInfoByUserID(userID string) (*types.UserRealN } return &userRealNameInfo, nil } + +func (c *Cockroach) GetEnterpriseRealNameInfoByUserID(userID string) (*types.EnterpriseRealNameInfo, error) { + // get user info + ops := &types.UserQueryOpts{ID: userID} + user, err := c.GetUserCr(ops) + + if err != nil { + return nil, fmt.Errorf("failed to get user: %v", err) + } + + // get user realname info + var enterpriseRealNameInfo types.EnterpriseRealNameInfo + if err := c.DB.Where(&types.EnterpriseRealNameInfo{UserUID: user.UserUID}).First(&enterpriseRealNameInfo).Error; err != nil { + return nil, fmt.Errorf("failed to get enterprise real name info: %w", err) + } + return &enterpriseRealNameInfo, nil +} diff --git a/controllers/pkg/types/global.go b/controllers/pkg/types/global.go index 12ce3a8db7a1..4404fd102802 100644 --- a/controllers/pkg/types/global.go +++ b/controllers/pkg/types/global.go @@ -301,3 +301,21 @@ type UserRealNameInfo struct { func (UserRealNameInfo) TableName() string { return "UserRealNameInfo" } + +type EnterpriseRealNameInfo struct { + ID uuid.UUID `gorm:"column:id;type:uuid;default:gen_random_uuid();primary_key"` + UserUID uuid.UUID `gorm:"column:userUid;type:uuid;unique"` + EnterpriseName *string `gorm:"column:enterpriseName;type:text"` + EnterpriseQualification *string `gorm:"column:enterpriseQualification;type:text"` + LegalRepresentativePhone *string `gorm:"column:legalRepresentativePhone;type:text"` + IsVerified bool `gorm:"column:isVerified;type:boolean;default:false"` + VerificationStatus *string `gorm:"column:verificationStatus;type:text"` + CreatedAt time.Time `gorm:"column:createdAt;type:timestamp(3) with time zone;default:current_timestamp()"` + UpdatedAt time.Time `gorm:"column:updatedAt;type:timestamp(3) with time zone;autoUpdateTime"` + AdditionalInfo json.RawMessage `gorm:"column:additionalInfo;type:jsonb"` + SupportingMaterials json.RawMessage `gorm:"column:supportingMaterials;type:jsonb"` +} + +func (EnterpriseRealNameInfo) TableName() string { + return "EnterpriseRealNameInfo" +} diff --git a/service/account/api/api.go b/service/account/api/api.go index 0928529fbfda..0c675e0a5d76 100644 --- a/service/account/api/api.go +++ b/service/account/api/api.go @@ -1025,27 +1025,35 @@ func GetUserRealNameInfo(c *gin.Context) { } userRealNameInfo, err := dao.DBClient.GetUserRealNameInfo(req) - - if err != nil { + 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 } - if userRealNameInfo == nil { - c.JSON(http.StatusInternalServerError, helper.ErrorMessage{Error: "user real name info not found"}) + enterpriseRealNameInfo, err := dao.DBClient.GetEnterpriseRealNameInfo(req) + 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 } - if !userRealNameInfo.IsVerified { - c.JSON(http.StatusInternalServerError, helper.ErrorMessage{Error: "user real name info is not verified"}) + isVerified := (userRealNameInfo != nil && userRealNameInfo.IsVerified) || + (enterpriseRealNameInfo != nil && enterpriseRealNameInfo.IsVerified) + + if !isVerified { + c.JSON(http.StatusOK, helper.GetRealNameInfoResp{ + Data: helper.GetRealNameInfoRespData{ + UserID: req.UserID, + IsRealName: false, + }, + Message: "user is not verified", + }) return } - // Return success response c.JSON(http.StatusOK, helper.GetRealNameInfoResp{ Data: helper.GetRealNameInfoRespData{ UserID: req.UserID, - IsRealName: userRealNameInfo.IsVerified, + IsRealName: true, }, Message: "successfully get user real name info", }) diff --git a/service/account/dao/init.go b/service/account/dao/init.go index 7b2ff2eeda03..c0105f364c20 100644 --- a/service/account/dao/init.go +++ b/service/account/dao/init.go @@ -60,6 +60,7 @@ func Init(ctx context.Context) error { } file := helper.ConfigPath + Cfg = &Config{} // Initialize Cfg regardless of file existence if _, err := os.Stat(file); err == nil { data, err := os.ReadFile(file) if err != nil { @@ -67,8 +68,7 @@ func Init(ctx context.Context) error { } fmt.Printf("config file found, use config file: \n%s\n", file) - Cfg = &Config{} - // json marshal + // json unmarshal if err = json.Unmarshal(data, Cfg); err != nil { return fmt.Errorf("unmarshal config file error: %v", err) } diff --git a/service/account/dao/interface.go b/service/account/dao/interface.go index ebf798c91121..960cd9511abe 100644 --- a/service/account/dao/interface.go +++ b/service/account/dao/interface.go @@ -2,6 +2,7 @@ package dao import ( "context" + "errors" "fmt" "strconv" "strings" @@ -67,6 +68,7 @@ type Interface interface { GetRechargeDiscount(req helper.AuthReq) (helper.RechargeDiscountResp, error) ProcessPendingTaskRewards() error GetUserRealNameInfo(req *helper.GetRealNameInfoReq) (*types.UserRealNameInfo, error) + GetEnterpriseRealNameInfo(req *helper.GetRealNameInfoReq) (*types.EnterpriseRealNameInfo, error) ReconcileUnsettledLLMBilling(startTime, endTime time.Time) error ReconcileActiveBilling(startTime, endTime time.Time) error ArchiveHourlyBilling(hourStart, hourEnd time.Time) error @@ -1574,12 +1576,29 @@ 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 userRealNameInfo, nil } +func (m *Account) GetEnterpriseRealNameInfo(req *helper.GetRealNameInfoReq) (*types.EnterpriseRealNameInfo, error) { + // get enterprise info + 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 enterpriseRealNameInfo, nil +} + func (m *Account) ReconcileActiveBilling(startTime, endTime time.Time) error { ctx := context.Background() billings := make(map[uuid.UUID]*billingBatch) diff --git a/service/account/dao/interface_test.go b/service/account/dao/interface_test.go index cf6259a2071d..344cd26bbbb5 100644 --- a/service/account/dao/interface_test.go +++ b/service/account/dao/interface_test.go @@ -645,6 +645,28 @@ func TestAccount_GetUserRealNameInfo(t *testing.T) { t.Logf("userRealNameInfo = %+v", userRealNameInfo) } +func TestAccount_GetEnterpriseRealNameInfo(t *testing.T) { + db, err := newAccountForTest("", os.Getenv("GLOBAL_COCKROACH_URI"), os.Getenv("LOCAL_COCKROACH_URI")) + if err != nil { + t.Fatalf("NewAccountInterface() error = %v", err) + return + } + + enterpriseRealNameInfo, err := db.GetEnterpriseRealNameInfo(&helper.GetRealNameInfoReq{ + AuthBase: helper.AuthBase{ + Auth: &helper.Auth{ + UserID: "E1xAJ0fy4k", + }, + }, + }) + + if err != nil { + t.Fatalf("GetUserRealNameInfo() error = %v", err) + return + } + t.Logf("enterpriseRealNameInfo = %+v", enterpriseRealNameInfo) +} + func init() { // set env os.Setenv("MONGO_URI", "")