feat(accounts): improve account creation

This commit is contained in:
2021-11-12 00:38:44 +01:00
parent 78071a6a91
commit 7bf8db8050
5 changed files with 24 additions and 19 deletions

View File

@@ -1,7 +1,6 @@
package account
import (
"fmt"
"net/http"
"nos-comptes/handler"
"nos-comptes/internal/storage/dao/postgresql"
@@ -64,11 +63,13 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) {
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
return
}
utils.GetLogger().Info(userId)
_, err = c.userService.GetUserById(userId)
if e, ok := err.(*model.APIError); ok {
utils.GetLogger().Info(err)
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
return
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
utils.JSONError(gc.Writer, model.ErrInternalServer)
@@ -81,19 +82,15 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) {
return
}
account = Account{AccountEditable: accountEditable, UserId: userId}
utils.GetLogger().Warn(account)
utils.GetLogger().Warn(accountEditable.Name)
utils.GetLogger().Warn(accountEditable.Provider)
accountFound, err := c.service.GetAccountWithNameForUser(account.Name, userId)
utils.GetLogger().Warn(err)
utils.GetLogger().Warn(accountFound)
if e, ok := err.(*model.APIError); ok {
if e.Type != model.ErrNotFound.Type {
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAccount: get account error")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
return
}
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get account")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
@@ -101,12 +98,13 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) {
if accountFound != nil {
utils.GetLoggerFromCtx(gc).WithError(&model.ErrAlreadyExists).WithField("type", model.ErrAlreadyExists.Type).Error("error CreateAccount: account already exists")
utils.JSONErrorWithMessage(gc.Writer, model.ErrAlreadyExists, "account already exists with the same Name")
return
}
account.UserId = userId
accountSaved, err := c.service.CreateAccount(account)
if err != nil {
fmt.Println(err)
utils.JSONError(gc.Writer, model.ErrInternalServer)
utils.GetLogger().Info(err)
utils.JSONErrorWithMessage(gc.Writer, model.ErrInternalServer, err.Error())
return
}
utils.JSON(gc.Writer, http.StatusCreated, accountSaved)