Skip to content

Commit 6a4704c

Browse files
committed
fix(user): removed sss3 insert, changes in create user endpoint, update password endpoint
1 parent 5825826 commit 6a4704c

22 files changed

+352
-166
lines changed

docs/openapi.json

+73-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,57 @@
228228
}
229229
}
230230
},
231-
"/user/{walletAddress}/update ": {
231+
"/user/verified/update": {
232+
"put": {
233+
"tags": ["user"],
234+
"summary": "Update User Email Verified At",
235+
"description": "Update user email verified at",
236+
"requestBody": {
237+
"description": "Update user email verified at request",
238+
"content": {
239+
"application/json": {
240+
"schema": {
241+
"$ref": "#/components/schemas/UpdateUserEmailVerifiedAtRequest"
242+
}
243+
}
244+
},
245+
"required": true
246+
},
247+
"responses": {
248+
"200": {
249+
"description": "Success",
250+
"content": {
251+
"application/json": {
252+
"schema": {
253+
"$ref": "#/components/schemas/APIResponse"
254+
}
255+
}
256+
}
257+
},
258+
"4xx": {
259+
"description": "Client side errors",
260+
"content": {
261+
"application/json": {
262+
"schema": {
263+
"$ref": "#/components/schemas/APIErrorResponse"
264+
}
265+
}
266+
}
267+
},
268+
"5xx": {
269+
"description": "Server side errors",
270+
"content": {
271+
"application/json": {
272+
"schema": {
273+
"$ref": "#/components/schemas/APIErrorResponse"
274+
}
275+
}
276+
}
277+
}
278+
}
279+
}
280+
},
281+
"/user/{walletAddress}/update": {
232282
"put": {
233283
"tags": ["user"],
234284
"summary": "Update User By Wallet Address",
@@ -362,14 +412,17 @@
362412
"components": {
363413
"schemas": {
364414
"CreateUserRequest": {
365-
"required": ["email", "name"],
415+
"required": ["email", "name", "password"],
366416
"type": "object",
367417
"properties": {
368418
"email": {
369419
"type": "string"
370420
},
371421
"name": {
372422
"type": "string"
423+
},
424+
"password": {
425+
"type": "string"
373426
}
374427
}
375428
},
@@ -382,11 +435,24 @@
382435
}
383436
}
384437
},
438+
"UpdateUserEmailVerifiedAtRequest": {
439+
"required": ["email"],
440+
"type": "object",
441+
"properties": {
442+
"email": {
443+
"type": "string",
444+
"format": "email"
445+
}
446+
}
447+
},
385448
"UpdateUserPasswordRequest": {
386-
"required": ["password"],
449+
"required": ["currentPassword", "newPassword"],
387450
"type": "object",
388451
"properties": {
389-
"password": {
452+
"currentPassword": {
453+
"type": "string"
454+
},
455+
"newPassword": {
390456
"type": "string"
391457
}
392458
}
@@ -434,6 +500,9 @@
434500
"properties": {
435501
"sss2": {
436502
"type": "string"
503+
},
504+
"sss3": {
505+
"type": "string"
437506
}
438507
}
439508
},

infrastructures/database/mysql/migrations/000002_create_user_sss_schema.down.sql

-1
This file was deleted.

infrastructures/database/mysql/migrations/000002_create_user_sss_schema.up.sql

-8
This file was deleted.

interfaces/http/rest/router.go

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (router *router) InitRouter() *chi.Mux {
8888
r.Get("/list", userQueryController.GetUsers)
8989
r.Get("/{walletAddress}", userQueryController.GetUserByWalletAddress)
9090
r.Put("/{walletAddress}/update", userCommandController.UpdateUserByWalletAddress)
91+
r.Put("/verified/update", userCommandController.UpdateUserEmailVerifiedAt)
9192
r.Put("/{walletAddress}/password/update", userCommandController.UpdateUserPassword)
9293
})
9394
})

interfaces/servicecontainer.go

+7
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,17 @@ func (k *kernel) userCommandServiceContainer() *userService.UserCommandService {
9494
MySQLDBHandlerInterface: mysqlDBHandler,
9595
}
9696

97+
queryRepository := &userRepository.UserQueryRepository{
98+
MySQLDBHandlerInterface: mysqlDBHandler,
99+
}
100+
97101
service := &userService.UserCommandService{
98102
UserCommandRepositoryInterface: &userRepository.UserCommandRepositoryCircuitBreaker{
99103
UserCommandRepositoryInterface: repository,
100104
},
105+
UserQueryRepositoryInterface: &userRepository.UserQueryRepositoryCircuitBreaker{
106+
UserQueryRepositoryInterface: queryRepository,
107+
},
101108
}
102109

103110
return service

internal/errors/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const (
1111
HystrixTimeout string = "HYSTRIX_TIMEOUT"
1212
// InvalidRequestPayload is the code for binding errors
1313
InvalidRequestPayload string = "INVALID_REQUEST_PAYLOAD"
14+
// InvalidPassword is the code for invalid password
15+
InvalidPassword string = "INVALID_PASSWORD"
1416
// InvalidPayload is the code for payload not satisfying requirements
1517
InvalidPayload string = "INVALID_PAYLOAD"
1618
// MaximumLimitReached is the code when the max limit is reached

module/user/application/UserCommandServiceInterface.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
// UserCommandServiceInterface holds the implementable methods for the user command service
1010
type UserCommandServiceInterface interface {
1111
// CreateUser creates a new user
12-
CreateUser(ctx context.Context, data types.CreateUser) (string, error)
12+
CreateUser(ctx context.Context, data types.CreateUser) (types.CreateUserResult, error)
1313
// UpdateUser updates user
1414
UpdateUser(ctx context.Context, data types.UpdateUser) error
15+
// UpdateUserEmailVerifiedAt updates user email verified at
16+
UpdateUserEmailVerifiedAt(ctx context.Context, email string) error
1517
// UpdateUserPassword updates user password
1618
UpdateUserPassword(ctx context.Context, data types.UpdateUserPassword) error
1719
}

module/user/application/UserQueryServiceInterface.go

-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@ type UserQueryServiceInterface interface {
1414
GetUserByWalletAddress(ctx context.Context, walletAddress string) (entity.User, error)
1515
// GetUserByEmail get user by email
1616
GetUserByEmail(ctx context.Context, email string) (entity.User, error)
17-
// GetUserSSS get user sss3
18-
GetUserSSS(ctx context.Context, walletAddress string) (entity.UserSSS, error)
1917
}

module/user/domain/entity/UserSSS.go

-15
This file was deleted.

module/user/domain/repository/UserCommandRepositoryInterface.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
type UserCommandRepositoryInterface interface {
99
// InsertUser inserts a new user
1010
InsertUser(data types.CreateUser) error
11-
// UpdateUserPassword updates user password
12-
UpdateUserPassword(data types.UpdateUserPassword) error
1311
// UpdateUser updates user
1412
UpdateUser(data types.UpdateUser) error
13+
// UpdateUserEmailVerifiedAt updates user email verified at
14+
UpdateUserEmailVerifiedAt(email string) error
15+
// UpdateUserPassword updates user password
16+
UpdateUserPassword(data types.UpdateUserPassword) error
1517
}

module/user/domain/repository/UserQueryRepositoryInterface.go

-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ type UserQueryRepositoryInterface interface {
1212
SelectUserByWalletAddress(walletAddress string) (entity.User, error)
1313
// SelectUserByEmail select a user by email
1414
SelectUserByEmail(email string) (entity.User, error)
15-
// SelectUserSSS select user sss3
16-
SelectUserSSS(walletAddress string) (entity.UserSSS, error)
1715
}

module/user/infrastructure/repository/UserCommandRepository.go

+19-29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"log"
7+
"time"
78

89
"github.com/go-sql-driver/mysql"
910

@@ -38,12 +39,6 @@ func (repository *UserCommandRepository) InsertUser(data repositoryTypes.CreateU
3839
return errors.New(apiError.DatabaseError)
3940
}
4041

41-
// insert user sss record
42-
err = repository.insertUserSSS(data.SSS3, data.WalletAddress)
43-
if err != nil {
44-
return err
45-
}
46-
4742
return nil
4843
}
4944

@@ -55,8 +50,7 @@ func (repository *UserCommandRepository) UpdateUser(data repositoryTypes.UpdateU
5550
}
5651

5752
// update user
58-
stmt := fmt.Sprintf("UPDATE %s SET name=:name "+
59-
"WHERE wallet_address=:wallet_address", user.GetModelName())
53+
stmt := fmt.Sprintf("UPDATE %s SET name=:name WHERE wallet_address=:wallet_address", user.GetModelName())
6054
_, err := repository.MySQLDBHandlerInterface.Execute(stmt, user)
6155
if err != nil {
6256
log.Println(err)
@@ -66,16 +60,17 @@ func (repository *UserCommandRepository) UpdateUser(data repositoryTypes.UpdateU
6660
return nil
6761
}
6862

69-
// UpdateUserPassword updates user password
70-
func (repository *UserCommandRepository) UpdateUserPassword(data repositoryTypes.UpdateUserPassword) error {
63+
// UpdateUserEmailVerifiedAt updates user email verified at
64+
func (repository *UserCommandRepository) UpdateUserEmailVerifiedAt(email string) error {
65+
emailVerifiedAt := time.Now()
66+
7167
user := &entity.User{
72-
WalletAddress: data.WalletAddress,
73-
Password: data.Password,
68+
Email: email,
69+
EmailVerifiedAt: &emailVerifiedAt,
7470
}
7571

76-
// update users
77-
stmt := fmt.Sprintf("UPDATE %s SET password=:password "+
78-
"WHERE wallet_address=:wallet_address", user.GetModelName())
72+
// update user email verified at
73+
stmt := fmt.Sprintf("UPDATE %s SET email_verified_at=:email_verified_at WHERE email=:email", user.GetModelName())
7974
_, err := repository.MySQLDBHandlerInterface.Execute(stmt, user)
8075
if err != nil {
8176
log.Println(err)
@@ -85,24 +80,19 @@ func (repository *UserCommandRepository) UpdateUserPassword(data repositoryTypes
8580
return nil
8681
}
8782

88-
// insertUserSSS creates a new user sss
89-
func (repository *UserCommandRepository) insertUserSSS(sss3, userWalletAddress string) error {
90-
sss := entity.UserSSS{
91-
SSS3: sss3,
92-
UserWalletAddress: userWalletAddress,
83+
// UpdateUserPassword updates user password
84+
func (repository *UserCommandRepository) UpdateUserPassword(data repositoryTypes.UpdateUserPassword) error {
85+
user := &entity.User{
86+
WalletAddress: data.WalletAddress,
87+
Password: data.NewPassword,
9388
}
9489

95-
// insert user sss
96-
stmt := fmt.Sprintf("INSERT INTO %s (user_wallet_address,sss_3) "+
97-
"VALUES (:user_wallet_address,:sss_3)", sss.GetModelName())
98-
_, err := repository.MySQLDBHandlerInterface.Execute(stmt, sss)
90+
// update users
91+
stmt := fmt.Sprintf("UPDATE %s SET password=:password "+
92+
"WHERE wallet_address=:wallet_address", user.GetModelName())
93+
_, err := repository.MySQLDBHandlerInterface.Execute(stmt, user)
9994
if err != nil {
10095
log.Println(err)
101-
var mysqlErr *mysql.MySQLError
102-
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {
103-
return errors.New(apiError.DuplicateRecord)
104-
}
105-
10696
return errors.New(apiError.DatabaseError)
10797
}
10898

module/user/infrastructure/repository/UserCommandRepositoryCircuitBreaker.go

+81
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,84 @@ func (repository *UserCommandRepositoryCircuitBreaker) InsertUser(data repositor
4141
return err
4242
}
4343
}
44+
45+
// UpdateUser decorator pattern to update user
46+
func (repository *UserCommandRepositoryCircuitBreaker) UpdateUser(data repositoryTypes.UpdateUser) error {
47+
output := make(chan error, 1)
48+
errChan := make(chan error, 1)
49+
50+
hystrix.ConfigureCommand("update_user", config.Settings())
51+
errors := hystrix.Go("update_user", func() error {
52+
err := repository.UserCommandRepositoryInterface.UpdateUser(data)
53+
if err != nil {
54+
errChan <- err
55+
return nil
56+
}
57+
58+
output <- nil
59+
return nil
60+
}, nil)
61+
62+
select {
63+
case out := <-output:
64+
return out
65+
case err := <-errChan:
66+
return err
67+
case err := <-errors:
68+
return err
69+
}
70+
}
71+
72+
// UpdateUserEmailVerifiedAt decorator pattern to update user email verified at
73+
func (repository *UserCommandRepositoryCircuitBreaker) UpdateUserEmailVerifiedAt(email string) error {
74+
output := make(chan error, 1)
75+
errChan := make(chan error, 1)
76+
77+
hystrix.ConfigureCommand("update_user_email_verified_at", config.Settings())
78+
errors := hystrix.Go("update_user_email_verified_at", func() error {
79+
err := repository.UserCommandRepositoryInterface.UpdateUserEmailVerifiedAt(email)
80+
if err != nil {
81+
errChan <- err
82+
return nil
83+
}
84+
85+
output <- nil
86+
return nil
87+
}, nil)
88+
89+
select {
90+
case out := <-output:
91+
return out
92+
case err := <-errChan:
93+
return err
94+
case err := <-errors:
95+
return err
96+
}
97+
}
98+
99+
// UpdateUserPassword decorator pattern to update user password
100+
func (repository *UserCommandRepositoryCircuitBreaker) UpdateUserPassword(data repositoryTypes.UpdateUserPassword) error {
101+
output := make(chan error, 1)
102+
errChan := make(chan error, 1)
103+
104+
hystrix.ConfigureCommand("update_user_password", config.Settings())
105+
errors := hystrix.Go("update_user_password", func() error {
106+
err := repository.UserCommandRepositoryInterface.UpdateUserPassword(data)
107+
if err != nil {
108+
errChan <- err
109+
return nil
110+
}
111+
112+
output <- nil
113+
return nil
114+
}, nil)
115+
116+
select {
117+
case out := <-output:
118+
return out
119+
case err := <-errChan:
120+
return err
121+
case err := <-errors:
122+
return err
123+
}
124+
}

0 commit comments

Comments
 (0)