feat: update account name

This commit is contained in:
Kirill 2023-06-15 19:00:01 +03:00
parent 91d372a4f0
commit cea2068f7a
4 changed files with 78 additions and 4 deletions

@ -23,6 +23,7 @@ type accountService interface {
RemoveAccount(ctx context.Context, userID string) (*models.Account, errors.Error)
DeleteAccount(ctx context.Context, userID string) (*models.Account, errors.Error)
SetVerificationStatus(ctx context.Context, userID string, status models.AccountStatus) (*models.Account, errors.Error)
UpdateAccountName(ctx context.Context, userID string, name *models.Name) (*models.Account, errors.Error)
}
type Deps struct {
@ -152,3 +153,29 @@ func (receiver *Controller) SetVerificationStatus(ctx echo.Context, userID strin
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) UpdateAccountName(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <UpdateAccountName> of <AccountController>")
return errors.HTTP(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
}
request, bindErr := echotools.Bind[models.Name](ctx)
if bindErr != nil {
receiver.logger.Error("failed to bind json request on <UpdateAccountName> of <AccountController>", zap.Error(bindErr))
return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to bind json: %w", bindErr), errors.ErrInternalError))
}
account, err := receiver.service.UpdateAccountName(ctx.Request().Context(), userID, request)
if err != nil {
receiver.logger.Error("failed to update account name on <UpdateAccountName> of <AccountController>", zap.Error(err))
return errors.HTTP(ctx, err)
}
return ctx.JSON(http.StatusOK, account)
}

@ -360,14 +360,50 @@ func (receiver *AccountRepository) SetStatus(ctx context.Context, userID string,
}}
if err := receiver.mongoDB.FindOneAndUpdate(ctx, filter, update, options).Decode(&account); err != nil {
receiver.logger.Error("failed to change wallet on <SetStatus> of <AccountRepository>",
receiver.logger.Error("failed to set status on <SetStatus> of <AccountRepository>",
zap.Error(err),
zap.String("userID", userID),
zap.String("status", string(status)),
)
removeErr := errors.New(
fmt.Errorf("failed to change wallet of account <%s> on <SetStatus> of <AccountRepository>: %w", userID, err),
fmt.Errorf("failed to set status <%s> to account <%s> on <SetStatus> of <AccountRepository>: %w", status, userID, err),
errors.ErrInternalError,
)
if err == mongo.ErrNoDocuments {
removeErr.SetType(errors.ErrNotFound)
}
return nil, removeErr
}
return &account, nil
}
func (receiver *AccountRepository) UpdateName(ctx context.Context, userID string, name *models.Name) (*models.Account, errors.Error) {
account := models.Account{}
options := options.FindOneAndUpdate().SetReturnDocument(options.After)
filter := bson.M{
fields.Account.UserID: userID,
fields.Account.Deleted: false,
}
update := bson.M{"$set": bson.M{
fields.Account.Name: name,
fields.Account.UpdatedAt: time.Now(),
}}
if err := receiver.mongoDB.FindOneAndUpdate(ctx, filter, update, options).Decode(&account); err != nil {
receiver.logger.Error("failed to change name on <UpdateName> of <AccountRepository>",
zap.Error(err),
zap.String("userID", userID),
zap.Any("name", name),
)
removeErr := errors.New(
fmt.Errorf("failed to change name of account <%s> on <UpdateName> of <AccountRepository>: %w", userID, err),
errors.ErrInternalError,
)

@ -19,6 +19,7 @@ type accountRepository interface {
Delete(ctx context.Context, id string) (*models.Account, errors.Error)
CountAll(ctx context.Context) (int64, errors.Error)
SetStatus(ctx context.Context, userID string, status models.AccountStatus) (*models.Account, errors.Error)
UpdateName(ctx context.Context, userID string, name *models.Name) (*models.Account, errors.Error)
}
type authClient interface {
@ -224,3 +225,13 @@ func (receiver *Service) SetVerificationStatus(ctx context.Context, userID strin
return account, nil
}
func (receiver *Service) UpdateAccountName(ctx context.Context, userID string, name *models.Name) (*models.Account, errors.Error) {
account, err := receiver.repository.UpdateName(ctx, userID, name)
if err != nil {
receiver.logger.Error("failed to update account name on <UpdateAccountName> of <AccountService>", zap.Error(err))
return nil, err
}
return account, nil
}

@ -2,7 +2,6 @@ package swagger
import (
"log"
"net/http"
"github.com/labstack/echo/v4"
)
@ -18,6 +17,7 @@ type accountController interface {
GetDirectAccount(ctx echo.Context, userID string) error
GetAccounts(echo.Context, PaginationAccountsParams) error
SetVerificationStatus(ctx echo.Context, userID string) error
UpdateAccountName(ctx echo.Context) error
}
type currencyController interface {
@ -93,7 +93,7 @@ func (receiver *API) DeleteAccount(ctx echo.Context) error {
}
func (receiver *API) ChangeAccount(ctx echo.Context) error {
return ctx.String(http.StatusNotImplemented, "method not implemented")
return receiver.accountController.UpdateAccountName(ctx)
}
func (receiver *API) SetAccountVerificationStatus(ctx echo.Context, userID string) error {