作者 yangfu

添加重置手机号

@@ -10,9 +10,9 @@ import ( @@ -10,9 +10,9 @@ import (
10 10
11 type PhoneAuthResetPhoneCommand struct { 11 type PhoneAuthResetPhoneCommand struct {
12 // 用户Id 用户唯一标识 12 // 用户Id 用户唯一标识
13 - UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"`  
14 - OldPhone int `cname:"" json:"oldPhone" valid:"Required"`  
15 - NewPhone int `cname:"" json:"newPhone" valid:"Required"` 13 + UserId int64 `cname:"用户Id 用户唯一标识" json:"userId"`
  14 + OldPhone string `cname:"" json:"oldPhone" valid:"Required"`
  15 + NewPhone string `cname:"" json:"newPhone" valid:"Required"`
16 } 16 }
17 17
18 func (phoneAuthResetPhoneCommand *PhoneAuthResetPhoneCommand) Valid(validation *validation.Validation) { 18 func (phoneAuthResetPhoneCommand *PhoneAuthResetPhoneCommand) Valid(validation *validation.Validation) {
@@ -200,10 +200,20 @@ func (authService *AuthService) PhoneAuthResetPhone(phoneAuthResetPhoneCommand * @@ -200,10 +200,20 @@ func (authService *AuthService) PhoneAuthResetPhone(phoneAuthResetPhoneCommand *
200 defer func() { 200 defer func() {
201 transactionContext.RollbackTransaction() 201 transactionContext.RollbackTransaction()
202 }() 202 }()
  203 +
  204 + resetPhoneService, err := factory.CreatePgAuthResetPhoneService(map[string]interface{}{
  205 + "transactionContext": transactionContext,
  206 + })
  207 + if err != nil {
  208 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  209 + }
  210 + if err := resetPhoneService.ResetPhone(nil, phoneAuthResetPhoneCommand.OldPhone, phoneAuthResetPhoneCommand.NewPhone); err != nil {
  211 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  212 + }
203 if err := transactionContext.CommitTransaction(); err != nil { 213 if err := transactionContext.CommitTransaction(); err != nil {
204 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 214 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
205 } 215 }
206 - return nil, nil 216 + return struct{}{}, nil
207 } 217 }
208 218
209 // 刷新IM信息 219 // 刷新IM信息
@@ -13,3 +13,11 @@ func CreateSignUpCompanyService(options map[string]interface{}) (service.PgSignU @@ -13,3 +13,11 @@ func CreateSignUpCompanyService(options map[string]interface{}) (service.PgSignU
13 } 13 }
14 return domainService.NewPgSignUpCompanyServiceService(transactionContext) 14 return domainService.NewPgSignUpCompanyServiceService(transactionContext)
15 } 15 }
  16 +
  17 +func CreatePgAuthResetPhoneService(options map[string]interface{}) (service.PgAuthResetPhoneService, error) {
  18 + var transactionContext *pgTransaction.TransactionContext
  19 + if value, ok := options["transactionContext"]; ok {
  20 + transactionContext = value.(*pgTransaction.TransactionContext)
  21 + }
  22 + return domainService.NewPgAuthResetPhoneService(transactionContext)
  23 +}
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  4 +
  5 +// PgAuthResetPhoneService 重置手机号服务
  6 +type PgAuthResetPhoneService interface {
  7 + ResetPhone(optUser *domain.User, oldPhone, newPhone string) error
  8 +}
1 package domain 1 package domain
2 2
3 -import "time" 3 +import (
  4 + "time"
  5 +)
4 6
5 const ( 7 const (
6 DefaultAdminUserCode = "ADMIN01" 8 DefaultAdminUserCode = "ADMIN01"
@@ -96,18 +98,18 @@ func (user *User) Update(data map[string]interface{}) error { @@ -96,18 +98,18 @@ func (user *User) Update(data map[string]interface{}) error {
96 if departmentId, ok := data["departmentId"]; ok { 98 if departmentId, ok := data["departmentId"]; ok {
97 user.DepartmentId = departmentId.(int64) 99 user.DepartmentId = departmentId.(int64)
98 } 100 }
99 - if userName, ok := data["userName"]; ok {  
100 - user.UserInfo.UserName = userName.(string)  
101 - }  
102 - if phone, ok := data["phone"]; ok {  
103 - user.UserInfo.Phone = phone.(string)  
104 - }  
105 - if avatar, ok := data["avatar"]; ok {  
106 - user.UserInfo.Avatar = avatar.(string)  
107 - }  
108 - if email, ok := data["email"]; ok {  
109 - user.UserInfo.Email = email.(string)  
110 - } 101 + //if userName, ok := data["userName"]; ok {
  102 + // user.UserInfo.UserName = userName.(string)
  103 + //}
  104 + //if phone, ok := data["phone"]; ok {
  105 + // user.UserInfo.Phone = phone.(string)
  106 + //}
  107 + //if avatar, ok := data["avatar"]; ok {
  108 + // user.UserInfo.Avatar = avatar.(string)
  109 + //}
  110 + //if email, ok := data["email"]; ok {
  111 + // user.UserInfo.Email = email.(string)
  112 + //}
111 if userOrg, ok := data["userOrg"]; ok { 113 if userOrg, ok := data["userOrg"]; ok {
112 user.UserOrg = userOrg.([]*Org) 114 user.UserOrg = userOrg.([]*Org)
113 } 115 }
@@ -151,3 +153,5 @@ func (user *User) Update(data map[string]interface{}) error { @@ -151,3 +153,5 @@ func (user *User) Update(data map[string]interface{}) error {
151 } 153 }
152 154
153 type UserStatus int 155 type UserStatus int
  156 +
  157 +/***** 1.基础模块函数 *****/
@@ -137,6 +137,9 @@ func (userBase *UserBase) ResetPassword(account, password string) error { @@ -137,6 +137,9 @@ func (userBase *UserBase) ResetPassword(account, password string) error {
137 } 137 }
138 138
139 func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error { 139 func (userBase *UserBase) ResetPhone(oldPhone, newPhone string) error {
  140 + if userBase.Status != int(UserStatusEnable) {
  141 + return fmt.Errorf("该用户不存在")
  142 + }
140 if !strings.EqualFold(oldPhone, userBase.Account) { 143 if !strings.EqualFold(oldPhone, userBase.Account) {
141 return fmt.Errorf("该用户不存在") 144 return fmt.Errorf("该用户不存在")
142 } 145 }
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
  8 +)
  9 +
  10 +// PgAuthResetPhoneService 重置手机号服务
  11 +type PgAuthResetPhoneService struct {
  12 + transactionContext *pgTransaction.TransactionContext
  13 +}
  14 +
  15 +// ResetPhone 重置手机号
  16 +//
  17 +// oldPhone 旧手机号
  18 +// newPhone 新手机号
  19 +func (ptr *PgAuthResetPhoneService) ResetPhone(optUser *domain.User, oldPhone, newPhone string) error {
  20 + if len(oldPhone) == 0 {
  21 + return fmt.Errorf("旧手机号不能为空")
  22 + }
  23 + if len(newPhone) == 0 {
  24 + return fmt.Errorf("新手机号不能为空")
  25 + }
  26 +
  27 + // 1.重置当前账号的手机号
  28 + var err error
  29 + var userBase *domain.UserBase
  30 + userBaseRepository, _ := repository.NewUserBaseRepository(ptr.transactionContext)
  31 + if userBase, err = userBaseRepository.FindOne(map[string]interface{}{"account": oldPhone}); err != nil {
  32 + return err
  33 + }
  34 + if err == domain.ErrorNotFound {
  35 + return fmt.Errorf("该用户不存在")
  36 + }
  37 + if err = userBase.ResetPhone(oldPhone, newPhone); err != nil {
  38 + return err
  39 + }
  40 + if _, err := userBaseRepository.Save(userBase); err != nil {
  41 + return err
  42 + }
  43 +
  44 + // 2.重置跟账号相关的账号的手机号(冗余的扩展数据)
  45 + userRepository, _ := repository.NewUserRepository(ptr.transactionContext)
  46 + for i := 0; i < len(userBase.RelatedUsers); i++ {
  47 + userId := userBase.RelatedUsers[i]
  48 + if user, _ := userRepository.FindOne(map[string]interface{}{"userId": userId}); user != nil {
  49 + user.Update(map[string]interface{}{"phone": newPhone})
  50 + if _, err := userRepository.Save(user); err != nil {
  51 + return err
  52 + }
  53 + }
  54 + }
  55 +
  56 + return err
  57 +}
  58 +
  59 +func NewPgAuthResetPhoneService(transactionContext *pgTransaction.TransactionContext) (*PgAuthResetPhoneService, error) {
  60 + if transactionContext == nil {
  61 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  62 + } else {
  63 + return &PgAuthResetPhoneService{
  64 + transactionContext: transactionContext,
  65 + }, nil
  66 + }
  67 +}
@@ -66,7 +66,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) @@ -66,7 +66,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
66 &user.DepartmentId, 66 &user.DepartmentId,
67 &user.UserOrg, 67 &user.UserOrg,
68 &user.UserRole, 68 &user.UserRole,
69 - pg.Array(&user.FavoriteMenus), 69 + &user.FavoriteMenus,
70 &user.CooperationInfo, 70 &user.CooperationInfo,
71 &user.EnableStatus, 71 &user.EnableStatus,
72 &user.Ext, 72 &user.Ext,
@@ -83,7 +83,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) @@ -83,7 +83,7 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
83 user.DepartmentId, 83 user.DepartmentId,
84 user.UserOrg, 84 user.UserOrg,
85 user.UserRole, 85 user.UserRole,
86 - pg.Array(user.FavoriteMenus), 86 + user.FavoriteMenus,
87 user.CooperationInfo, 87 user.CooperationInfo,
88 user.EnableStatus, 88 user.EnableStatus,
89 user.Ext, 89 user.Ext,
@@ -102,9 +102,9 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) @@ -102,9 +102,9 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
102 &user.UserCode, 102 &user.UserCode,
103 &user.OrganizationId, 103 &user.OrganizationId,
104 &user.DepartmentId, 104 &user.DepartmentId,
105 - pg.Array(&user.UserOrg),  
106 - pg.Array(&user.UserRole),  
107 - pg.Array(&user.FavoriteMenus), 105 + &user.UserOrg,
  106 + &user.UserRole,
  107 + &user.FavoriteMenus,
108 &user.CooperationInfo, 108 &user.CooperationInfo,
109 &user.EnableStatus, 109 &user.EnableStatus,
110 &user.Ext, 110 &user.Ext,
@@ -118,9 +118,9 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error) @@ -118,9 +118,9 @@ func (repository *UserRepository) Save(user *domain.User) (*domain.User, error)
118 user.UserCode, 118 user.UserCode,
119 user.OrganizationId, 119 user.OrganizationId,
120 user.DepartmentId, 120 user.DepartmentId,
121 - pg.Array(user.UserOrg),  
122 - pg.Array(user.UserRole),  
123 - pg.Array(user.FavoriteMenus), 121 + user.UserOrg,
  122 + user.UserRole,
  123 + user.FavoriteMenus,
124 user.CooperationInfo, 124 user.CooperationInfo,
125 user.EnableStatus, 125 user.EnableStatus,
126 user.Ext, 126 user.Ext,
@@ -146,7 +146,7 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) ( @@ -146,7 +146,7 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (
146 tx := repository.transactionContext.PgTx 146 tx := repository.transactionContext.PgTx
147 userModel := new(models.User) 147 userModel := new(models.User)
148 query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions) 148 query := sqlbuilder.BuildQuery(tx.Model(userModel), queryOptions)
149 - query.SetWhereByQueryOption("user.user_id = ?", "userId") 149 + query.SetWhereByQueryOption("user_id = ?", "userId")
150 if err := query.First(); err != nil { 150 if err := query.First(); err != nil {
151 if err.Error() == "pg: no rows in result set" { 151 if err.Error() == "pg: no rows in result set" {
152 return nil, fmt.Errorf("没有此资源") 152 return nil, fmt.Errorf("没有此资源")
@@ -10,7 +10,6 @@ import ( @@ -10,7 +10,6 @@ import (
10 ) 10 )
11 11
12 var _ = Describe("企业注册", func() { 12 var _ = Describe("企业注册", func() {
13 - return  
14 BeforeEach(func() { 13 BeforeEach(func() {
15 //_, err := pG.DB.QueryOne( 14 //_, err := pG.DB.QueryOne(
16 // pg.Scan(&Id), 15 // pg.Scan(&Id),
@@ -11,23 +11,29 @@ import ( @@ -11,23 +11,29 @@ import (
11 ) 11 )
12 12
13 var _ = Describe("重置手机号", func() { 13 var _ = Describe("重置手机号", func() {
14 - return  
15 var Id int64 14 var Id int64
16 BeforeEach(func() { 15 BeforeEach(func() {
17 _, err := pG.DB.QueryOne( 16 _, err := pG.DB.QueryOne(
18 pg.Scan(&Id), 17 pg.Scan(&Id),
19 - "INSERT INTO s () VALUES () RETURNING _id", 18 + "INSERT INTO \"users\".\"user_base\"(\"user_base_id\", \"user_info\", \"account\", \"password\", \"im\", \"related_user\", \"status\", \"created_at\", \"updated_at\") VALUES (999, '{\"phone\": \"phone\", \"userName\": \"string\"}', 'phone', 'string', '{\"accid\": \"\", \"imToken\": \"\", \"csAccountId\": \"\"}', '{999}', 1, '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08') RETURNING user_base_id;",
  19 + )
  20 + Expect(err).NotTo(HaveOccurred())
  21 +
  22 + _, err = pG.DB.QueryOne(
  23 + pg.Scan(&Id),
  24 + "INSERT INTO \"users\".\"user\"(\"user_id\", \"company_id\", \"user_base_id\", \"user_type\", \"user_code\", \"organization_id\", \"department_id\", \"user_org\", \"user_role\", \"favorite_menus\", \"cooperation_info\", \"enable_status\", \"ext\", \"created_at\", \"updated_at\") VALUES (999, 5, 999, 1025, 'ADMIN01', 5, 5, '[{\"orgId\": 5, \"orgName\": \"string1\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"deletedAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[{\"ext\": {\"orgName\": \"string1\"}, \"roleId\": 5, \"roleName\": \"企业管理员\", \"createdAt\": \"0001-01-01T00:00:00Z\", \"updatedAt\": \"0001-01-01T00:00:00Z\"}]', '[]', '{\"cooperationCompany\": \"\", \"cooperationDeadline\": \"0001-01-01T00:00:00Z\"}', 1, '{\"phone\": \"18860183031\", \"depName\": \"string1\", \"orgName\": \"string1\"}', '2021-07-24 10:16:17.680805+08', '2021-07-24 10:16:17.680805+08');\n",
20 ) 25 )
21 Expect(err).NotTo(HaveOccurred()) 26 Expect(err).NotTo(HaveOccurred())
22 }) 27 })
  28 +
23 Describe("重置手机号", func() { 29 Describe("重置手机号", func() {
24 Context("", func() { 30 Context("", func() {
25 It("", func() { 31 It("", func() {
26 httpExpect := httpexpect.New(GinkgoT(), server.URL) 32 httpExpect := httpexpect.New(GinkgoT(), server.URL)
27 body := map[string]interface{}{ 33 body := map[string]interface{}{
28 "userId": "int64", 34 "userId": "int64",
29 - "oldPhone": "int",  
30 - "newPhone": "int", 35 + "oldPhone": "phone",
  36 + "newPhone": "phone1",
31 } 37 }
32 httpExpect.POST("/auth/reset-phone"). 38 httpExpect.POST("/auth/reset-phone").
33 WithJSON(body). 39 WithJSON(body).
@@ -42,7 +48,9 @@ var _ = Describe("重置手机号", func() { @@ -42,7 +48,9 @@ var _ = Describe("重置手机号", func() {
42 }) 48 })
43 }) 49 })
44 AfterEach(func() { 50 AfterEach(func() {
45 - _, err := pG.DB.Exec("DELETE FROM s WHERE true") 51 + _, err := pG.DB.Exec("DELETE FROM users.user_base WHERE user_base_id = 999")
  52 + Expect(err).NotTo(HaveOccurred())
  53 + _, err = pG.DB.Exec(`DELETE FROM users."user" WHERE user_id = 999`)
46 Expect(err).NotTo(HaveOccurred()) 54 Expect(err).NotTo(HaveOccurred())
47 }) 55 })
48 }) 56 })