Skip to content

Commit

Permalink
♻️ Change the user auth flag
Browse files Browse the repository at this point in the history
  • Loading branch information
wjuniorbh92 committed Nov 1, 2023
1 parent ed05036 commit af0069e
Showing 1 changed file with 91 additions and 87 deletions.
178 changes: 91 additions & 87 deletions backend/internal/controller/http/v1/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ func (r *assetsRoutes) createAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - create asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - create asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

sponsorID := request.SponsorId
if sponsorID == 0 {
sponsorID = _sponsorId
Expand Down Expand Up @@ -268,20 +283,6 @@ func (r *assetsRoutes) createAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - create asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - create asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
}

amount, err := strconv.ParseFloat(request.Amount, 64)
if err != nil {
amount = 0
Expand Down Expand Up @@ -347,8 +348,22 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - mint asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - mint asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

sponsorID := request.SponsorId
var err error
if sponsorID == 0 {
_, err = r.w.Get(_sponsorId)
} else {
Expand Down Expand Up @@ -391,20 +406,6 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - mint asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - mint asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
}

amount, err := strconv.ParseFloat(request.Amount, 64)
if err != nil {
amount = 0
Expand Down Expand Up @@ -447,6 +448,21 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - burn asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - burn asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

asset, err := r.as.GetById(request.Id)
if err != nil {
r.logger.Error(err, "http - v1 - burn asset - get asset")
Expand Down Expand Up @@ -492,20 +508,6 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {

}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - burn asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - burn asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
}

amount, err := strconv.ParseFloat(request.Amount, 64)
if err != nil {
amount = 0
Expand Down Expand Up @@ -549,6 +551,21 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - transfer asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - transfer asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

sourceWallet, err := r.w.Get(request.SourceWalletID)
if err != nil {
r.logger.Error(err, "http - v1 - transfer asset - get source wallet")
Expand Down Expand Up @@ -600,20 +617,6 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - transfer asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - transfer asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
}

amount, err := strconv.ParseFloat(request.Amount, 64)
if err != nil {
amount = 0
Expand Down Expand Up @@ -657,9 +660,24 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - clawback asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - clawback asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

sponsorID := request.SponsorId
var sponsor entity.Wallet
var err error

if sponsorID == 0 {
sponsor, err = r.w.Get(_sponsorId)
} else {
Expand Down Expand Up @@ -706,21 +724,6 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - clawback asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - clawback asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

amount, err := strconv.ParseFloat(request.Amount, 64)
if err != nil {
amount = 0
Expand Down Expand Up @@ -762,6 +765,21 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) {
return
}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - clawback asset - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - clawback asset - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

asset, err := r.as.Get(request.Code)
if err != nil {
r.logger.Error(err, "http - v1 - update auth flags - get asset")
Expand Down Expand Up @@ -814,20 +832,6 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) {

}

token := c.Request.Header.Get("Authorization")
user, err := r.a.GetUserByToken(token)
if err != nil {
r.logger.Error(err, "http - v1 - update auth flags - get user by token")
errorResponse(c, http.StatusNotFound, "user not found", err)
return
}

userID, err := strconv.Atoi(user.ID)
if err != nil {
r.logger.Error(err, "http - v1 - update auth flags - parse user id")
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
}

err = r.l.CreateLogTransaction(entity.LogTransaction{
Asset: asset,
TransactionTypeID: entity.UpdateAuthFlags,
Expand Down

0 comments on commit af0069e

Please sign in to comment.