From 47dff7695f32f9af2529603028881ff469cc4dfb Mon Sep 17 00:00:00 2001 From: Wellington Junior Date: Mon, 21 Aug 2023 17:11:11 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9A=80=20Add=20the=20new=20error=20lo?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/controller/http/v1/assets.go | 73 +++++++++---------- .../internal/controller/http/v1/contracts.go | 19 ++--- backend/internal/controller/http/v1/error.go | 7 +- backend/internal/controller/http/v1/role.go | 2 +- .../controller/http/v1/role_permissions.go | 2 +- backend/internal/controller/http/v1/users.go | 4 +- backend/internal/controller/http/v1/utils.go | 22 +++--- backend/internal/controller/http/v1/vault.go | 54 +++++++------- .../controller/http/v1/vault_category.go | 6 +- .../internal/controller/http/v1/wallets.go | 20 ++--- 10 files changed, 106 insertions(+), 103 deletions(-) diff --git a/backend/internal/controller/http/v1/assets.go b/backend/internal/controller/http/v1/assets.go index a100b88c..d3045046 100644 --- a/backend/internal/controller/http/v1/assets.go +++ b/backend/internal/controller/http/v1/assets.go @@ -97,7 +97,7 @@ type UpdateAuthFlagsRequest struct { func (r *assetsRoutes) createAsset(c *gin.Context) { var request CreateAssetRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } @@ -108,19 +108,19 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { sponsor, err := r.w.Get(sponsorID) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } res, err := r.m.SendMessage(entity.CreateKeypairChannel, entity.CreateKeypairRequest{Amount: 2}) if err != nil { - errorResponse(c, http.StatusInternalServerError, "kms messaging problems") + errorResponse(c, http.StatusInternalServerError, "kms messaging problems", err) return } kpRes, ok := res.Message.(entity.CreateKeypairResponse) if !ok || len(kpRes.PublicKeys) != 2 { - errorResponse(c, http.StatusInternalServerError, "unexpected kms response") + errorResponse(c, http.StatusInternalServerError, "unexpected kms response", err) return } issuerPk := kpRes.PublicKeys[0] @@ -186,12 +186,12 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { Operations: ops, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok = res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -218,7 +218,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { } asset, err = r.as.Create(asset) if err != nil { - errorResponse(c, http.StatusNotFound, "database problems") + errorResponse(c, http.StatusNotFound, "database problems", err) return } @@ -239,7 +239,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { func (r *assetsRoutes) mintAsset(c *gin.Context) { var request MintAssetRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } @@ -249,13 +249,13 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { } _, err := r.w.Get(request.SponsorId) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } asset, err := r.as.GetById(request.Id) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } ops := []entity.Operation{ @@ -275,15 +275,14 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { PublicKeys: []string{asset.Issuer.Key.PublicKey}, Operations: ops, }) - if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok := res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -304,13 +303,13 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { func (r *assetsRoutes) burnAsset(c *gin.Context) { var request BurnAssetRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } asset, err := r.as.GetById(request.Id) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } @@ -320,7 +319,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { } _, err = r.w.Get(request.SponsorId) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } ops := []entity.Operation{ @@ -342,13 +341,13 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { Operations: ops, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok := res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -371,13 +370,13 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { func (r *assetsRoutes) transferAsset(c *gin.Context) { var request TransferAssetRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } sourceWallet, err := r.w.Get(request.SourceWalletID) if err != nil { - errorResponse(c, http.StatusNotFound, "source wallet not found") + errorResponse(c, http.StatusNotFound, "source wallet not found", err) return } @@ -387,13 +386,13 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { } _, err = r.w.Get(request.SponsorId) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } asset, err := r.as.GetById(request.AssetID) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } ops := []entity.Operation{ @@ -415,13 +414,13 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { Operations: ops, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok := res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -442,7 +441,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { func (r *assetsRoutes) clawbackAsset(c *gin.Context) { var request ClawbackAssetRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } @@ -452,13 +451,13 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { } sponsor, err := r.w.Get(request.SponsorId) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } asset, err := r.as.Get(request.Code) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } @@ -482,13 +481,13 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { Operations: ops, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok := res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -509,13 +508,13 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { var request UpdateAuthFlagsRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } asset, err := r.as.Get(request.Code) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } @@ -523,7 +522,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { if request.TrustorId != 0 { trustor, err = r.w.Get(request.TrustorId) if err != nil { - errorResponse(c, http.StatusNotFound, "trustor wallet not found") + errorResponse(c, http.StatusNotFound, "trustor wallet not found", err) return } } else if request.TrustorPK != "" { @@ -544,7 +543,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { sponsor, err := r.w.Get(_sponsorId) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } @@ -554,13 +553,13 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { Operations: []entity.Operation{op}, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok := res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -578,7 +577,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { func (r *assetsRoutes) getAllAssets(c *gin.Context) { assets, err := r.as.GetAll() if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting assets") + errorResponse(c, http.StatusInternalServerError, "error getting assets", err) return } @@ -597,7 +596,7 @@ func (r *assetsRoutes) getAssetById(c *gin.Context) { assetId := c.Param("id") assets, err := r.as.GetById(assetId) if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting asset") + errorResponse(c, http.StatusInternalServerError, "error getting asset", err) return } diff --git a/backend/internal/controller/http/v1/contracts.go b/backend/internal/controller/http/v1/contracts.go index 29e59c12..1ef5a5fa 100644 --- a/backend/internal/controller/http/v1/contracts.go +++ b/backend/internal/controller/http/v1/contracts.go @@ -19,7 +19,8 @@ type contractRoutes struct { } func newContractRoutes(handler *gin.RouterGroup, m HTTPControllerMessenger, a usecase.AuthUseCase, c usecase.ContractUseCase, v usecase.VaultUseCase, - as usecase.AssetUseCase) { + as usecase.AssetUseCase, +) { r := &contractRoutes{m, a, c, v, as} h := handler.Group("/contract").Use(Auth(r.a.ValidateToken())) { @@ -56,25 +57,25 @@ func (r *contractRoutes) createContract(c *gin.Context) { var err error if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } asset, err := r.as.GetById(request.AssetId) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } vaultId, err := strconv.Atoi(request.VaultId) if err != nil { - errorResponse(c, http.StatusBadRequest, "invalid vault ID") + errorResponse(c, http.StatusBadRequest, "invalid vault ID", err) return } vault, err := r.v.GetById(vaultId) if err != nil { - errorResponse(c, http.StatusNotFound, "vault not found") + errorResponse(c, http.StatusNotFound, "vault not found", err) return } @@ -91,7 +92,7 @@ func (r *contractRoutes) createContract(c *gin.Context) { contract, err = r.c.Create(contract) if err != nil { - errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error())) + errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error()), err) return } @@ -109,7 +110,7 @@ func (r *contractRoutes) createContract(c *gin.Context) { func (r *contractRoutes) getAllContracts(c *gin.Context) { contract, err := r.v.GetAll() if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting contract") + errorResponse(c, http.StatusInternalServerError, "error getting contract", err) return } @@ -129,12 +130,12 @@ func (r *contractRoutes) getContractById(c *gin.Context) { vaultId, err := strconv.Atoi(idStr) if err != nil { - errorResponse(c, http.StatusBadRequest, "invalid vault ID") + errorResponse(c, http.StatusBadRequest, "invalid vault ID", err) return } contract, err := r.v.GetById(vaultId) if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting contract") + errorResponse(c, http.StatusInternalServerError, "error getting contract", err) return } diff --git a/backend/internal/controller/http/v1/error.go b/backend/internal/controller/http/v1/error.go index 506b4159..f0db75ba 100644 --- a/backend/internal/controller/http/v1/error.go +++ b/backend/internal/controller/http/v1/error.go @@ -8,6 +8,9 @@ type response struct { Error string `json:"error" example:"message"` } -func errorResponse(c *gin.Context, code int, msg string) { - c.AbortWithStatusJSON(code, response{msg}) +func errorResponse(c *gin.Context, code int, msg string, err error) { + c.AbortWithStatusJSON(code, gin.H{ + "message": msg, + "error": err, + }) } diff --git a/backend/internal/controller/http/v1/role.go b/backend/internal/controller/http/v1/role.go index 2e474301..57ca9d02 100644 --- a/backend/internal/controller/http/v1/role.go +++ b/backend/internal/controller/http/v1/role.go @@ -33,7 +33,7 @@ func newRoleRoutes(handler *gin.RouterGroup, roleUseCase usecase.RoleUseCase, me func (r *role) list(c *gin.Context) { roles, err := r.roleUseCase.List() if err != nil { - errorResponse(c, http.StatusInternalServerError, "database problems") + errorResponse(c, http.StatusInternalServerError, "database problems", err) } c.JSON(http.StatusOK, roles) } diff --git a/backend/internal/controller/http/v1/role_permissions.go b/backend/internal/controller/http/v1/role_permissions.go index 88c5cd07..4bcbf181 100644 --- a/backend/internal/controller/http/v1/role_permissions.go +++ b/backend/internal/controller/http/v1/role_permissions.go @@ -33,7 +33,7 @@ func (r *rolePermissions) rolePermissions(c *gin.Context) { token := c.GetHeader("Authorization") rolePermissions, err := r.rolePermissionUseCase.GetRolePermissions(token) if err != nil { - errorResponse(c, http.StatusInternalServerError, "database problems") + errorResponse(c, http.StatusInternalServerError, "database problems", err) } c.JSON(http.StatusOK, rolePermissions) } diff --git a/backend/internal/controller/http/v1/users.go b/backend/internal/controller/http/v1/users.go index f77d6898..9e99aaca 100644 --- a/backend/internal/controller/http/v1/users.go +++ b/backend/internal/controller/http/v1/users.go @@ -107,7 +107,7 @@ func (r *usersRoutes) autentication(c *gin.Context) { var user entity.User if err := c.ShouldBindJSON(&user); err != nil { // r.l.Error(err, "http - v1 - create") - errorResponse(c, http.StatusBadRequest, "invalid request body") + errorResponse(c, http.StatusBadRequest, "invalid request body", err) fmt.Println(err) return } @@ -115,7 +115,7 @@ func (r *usersRoutes) autentication(c *gin.Context) { user, err := r.t.Autentication(user.Email, user.Password) if err != nil { // r.l.Error(err, "http - v1 - create") - errorResponse(c, http.StatusInternalServerError, "database problems") + errorResponse(c, http.StatusInternalServerError, "database problems", err) fmt.Println(err) return } diff --git a/backend/internal/controller/http/v1/utils.go b/backend/internal/controller/http/v1/utils.go index 9c867c5a..ff102150 100644 --- a/backend/internal/controller/http/v1/utils.go +++ b/backend/internal/controller/http/v1/utils.go @@ -36,18 +36,18 @@ func (m *HTTPControllerMessenger) SendMessage(chanName string, value interface{} res := <-channel notify.Stop(msgKey, channel) - if notifyData, ok := res.(*entity.NotifyData); ok { - switch msg := notifyData.Message.(type) { - case entity.EnvelopeResponse: - if msg.StatusCode != 200 { - return nil, fmt.Errorf("sendMessage - error response: %v", msg) - } - return notifyData, nil + // if notifyData, ok := res.(*entity.NotifyData); ok { + // switch msg := notifyData.Message.(type) { + // case entity.EnvelopeResponse: + // if msg.StatusCode != 200 { + // return nil, fmt.Errorf("sendMessage - error response: %v", msg) + // } + // return notifyData, nil - default: - return notifyData, nil - } - } + // default: + // return notifyData, nil + // } + // } return res.(*entity.NotifyData), nil } diff --git a/backend/internal/controller/http/v1/vault.go b/backend/internal/controller/http/v1/vault.go index 13fe5cad..46df7cd1 100644 --- a/backend/internal/controller/http/v1/vault.go +++ b/backend/internal/controller/http/v1/vault.go @@ -68,13 +68,13 @@ func (r *vaultRoutes) createVault(c *gin.Context) { var err error if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } vaultCategory, err := r.vc.GetById(request.VaultCategoryId) if err != nil { - errorResponse(c, http.StatusNotFound, "source wallet not found") + errorResponse(c, http.StatusNotFound, "source wallet not found", err) return } @@ -82,19 +82,19 @@ func (r *vaultRoutes) createVault(c *gin.Context) { sponsor, err := r.w.Get(sponsorID) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } res, err := r.m.SendMessage(entity.CreateKeypairChannel, entity.CreateKeypairRequest{Amount: 1}) if err != nil { - errorResponse(c, http.StatusInternalServerError, "kms messaging problems") + errorResponse(c, http.StatusInternalServerError, "kms messaging problems", err) return } kpRes, ok := res.Message.(entity.CreateKeypairResponse) if !ok || len(kpRes.PublicKeys) != 1 { - errorResponse(c, http.StatusInternalServerError, "unexpected kms response") + errorResponse(c, http.StatusInternalServerError, "unexpected kms response", err) return } walletPk := kpRes.PublicKeys[0] @@ -112,7 +112,7 @@ func (r *vaultRoutes) createVault(c *gin.Context) { for _, assetId := range request.AssetsId { asset, err := r.as.GetById(strconv.Itoa(assetId)) if err != nil { - errorResponse(c, http.StatusNotFound, "asset not found") + errorResponse(c, http.StatusNotFound, "asset not found", err) return } @@ -133,12 +133,12 @@ func (r *vaultRoutes) createVault(c *gin.Context) { Operations: ops, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok = res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -159,7 +159,7 @@ func (r *vaultRoutes) createVault(c *gin.Context) { vault, err = r.v.Create(vault) if err != nil { - errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error())) + errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error()), err) return } @@ -177,7 +177,7 @@ func (r *vaultRoutes) createVault(c *gin.Context) { func (r *vaultRoutes) getAllVaults(c *gin.Context) { vault, err := r.v.GetAll() if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting vault") + errorResponse(c, http.StatusInternalServerError, "error getting vault", err) return } @@ -196,13 +196,13 @@ func (r *vaultRoutes) getVaultById(c *gin.Context) { idStr := c.Param("id") id, err := strconv.Atoi(idStr) if err != nil { - errorResponse(c, http.StatusBadRequest, "invalid vault ID") + errorResponse(c, http.StatusBadRequest, "invalid vault ID", err) return } vault, err := r.v.GetById(id) if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting vault") + errorResponse(c, http.StatusInternalServerError, "error getting vault", err) return } @@ -226,13 +226,13 @@ func (r *vaultRoutes) updateVaultCategory(c *gin.Context) { idStr := c.Param("id") id, err := strconv.Atoi(idStr) if err != nil { - errorResponse(c, http.StatusBadRequest, "invalid vault ID") + errorResponse(c, http.StatusBadRequest, "invalid vault ID", err) return } var request UpdateVaultCategoryRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } @@ -240,10 +240,10 @@ func (r *vaultRoutes) updateVaultCategory(c *gin.Context) { existingVault, err := r.v.GetById(id) if err != nil { if err.Error() == "VaultRepo - GetVaultById - Vault not found" { - errorResponse(c, http.StatusNotFound, "vault not found") + errorResponse(c, http.StatusNotFound, "vault not found", err) return } - errorResponse(c, http.StatusInternalServerError, "error finding vault") + errorResponse(c, http.StatusInternalServerError, "error finding vault", err) return } @@ -253,7 +253,7 @@ func (r *vaultRoutes) updateVaultCategory(c *gin.Context) { updatedVault, err := r.v.UpdateVault(existingVault) if err != nil { - errorResponse(c, http.StatusInternalServerError, "error updating vault category") + errorResponse(c, http.StatusInternalServerError, "error updating vault category", err) return } @@ -277,26 +277,26 @@ func (r *vaultRoutes) updateVaultAsset(c *gin.Context) { idStr := c.Param("id") id, err := strconv.Atoi(idStr) if err != nil { - errorResponse(c, http.StatusBadRequest, "invalid vault ID") + errorResponse(c, http.StatusBadRequest, "invalid vault ID", err) return } var request []UpdateVaultAssetRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } // Get the vault by its ID and the asset by its ID vault, err := r.v.GetById(id) if err != nil { - errorResponse(c, http.StatusNotFound, "vault not found") + errorResponse(c, http.StatusNotFound, "vault not found", err) return } sponsor, err := r.w.Get(_sponsorId) if err != nil { - errorResponse(c, http.StatusNotFound, "sponsor wallet not found") + errorResponse(c, http.StatusNotFound, "sponsor wallet not found", err) return } @@ -335,13 +335,13 @@ func (r *vaultRoutes) updateVaultAsset(c *gin.Context) { Operations: ops, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems") + errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } _, ok := res.Message.(entity.EnvelopeResponse) if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response") + errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) return } @@ -364,7 +364,7 @@ func (r *vaultRoutes) deleteVault(c *gin.Context) { idStr := c.Param("id") id, err := strconv.Atoi(idStr) if err != nil { - errorResponse(c, http.StatusBadRequest, "invalid vault ID") + errorResponse(c, http.StatusBadRequest, "invalid vault ID", err) return } @@ -372,10 +372,10 @@ func (r *vaultRoutes) deleteVault(c *gin.Context) { existingVault, err := r.v.GetById(id) if err != nil { if err.Error() == "VaultRepo - GetVaultById - Vault not found" { - errorResponse(c, http.StatusNotFound, "vault not found") + errorResponse(c, http.StatusNotFound, "vault not found", err) return } - errorResponse(c, http.StatusInternalServerError, "error finding vault") + errorResponse(c, http.StatusInternalServerError, "error finding vault", err) return } @@ -384,7 +384,7 @@ func (r *vaultRoutes) deleteVault(c *gin.Context) { deletedVault, err := r.v.DeleteVault(existingVault) if err != nil { - errorResponse(c, http.StatusInternalServerError, "error delete vault") + errorResponse(c, http.StatusInternalServerError, "error delete vault", err) return } diff --git a/backend/internal/controller/http/v1/vault_category.go b/backend/internal/controller/http/v1/vault_category.go index b7b1c276..9cc51491 100644 --- a/backend/internal/controller/http/v1/vault_category.go +++ b/backend/internal/controller/http/v1/vault_category.go @@ -45,7 +45,7 @@ func (r *vaultCategoryRoutes) createVaultCategory(c *gin.Context) { var err error if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error())) + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err) return } @@ -56,7 +56,7 @@ func (r *vaultCategoryRoutes) createVaultCategory(c *gin.Context) { vaultCategory, err = r.vc.Create(vaultCategory) if err != nil { - errorResponse(c, http.StatusNotFound, "database problems") + errorResponse(c, http.StatusNotFound, "database problems", err) return } @@ -74,7 +74,7 @@ func (r *vaultCategoryRoutes) createVaultCategory(c *gin.Context) { func (r *vaultCategoryRoutes) getAllVaultCategories(c *gin.Context) { vaultCategories, err := r.vc.GetAll() if err != nil { - errorResponse(c, http.StatusInternalServerError, "error getting vault categories") + errorResponse(c, http.StatusInternalServerError, "error getting vault categories", err) return } diff --git a/backend/internal/controller/http/v1/wallets.go b/backend/internal/controller/http/v1/wallets.go index f4fa1a81..156be433 100644 --- a/backend/internal/controller/http/v1/wallets.go +++ b/backend/internal/controller/http/v1/wallets.go @@ -38,7 +38,7 @@ func (r *walletsRoutes) list(c *gin.Context) { wallets, err := r.w.List(walletType) if err != nil { - errorResponse(c, http.StatusInternalServerError, "database problems") + errorResponse(c, http.StatusInternalServerError, "database problems", err) } c.JSON(http.StatusOK, wallets) @@ -61,13 +61,13 @@ type CreateWalletRequest struct { func (r *walletsRoutes) create(c *gin.Context) { var request CreateWalletRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, "invalid request body") + errorResponse(c, http.StatusBadRequest, "invalid request body", err) return } res, err := r.m.SendMessage(entity.CreateKeypairChannel, entity.CreateKeypairRequest{Amount: 1}) if err != nil { - errorResponse(c, http.StatusInternalServerError, "messaging problems") + errorResponse(c, http.StatusInternalServerError, "messaging problems", err) return } @@ -82,7 +82,7 @@ func (r *walletsRoutes) create(c *gin.Context) { }, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "database problems") + errorResponse(c, http.StatusInternalServerError, "database problems", err) return } @@ -107,19 +107,19 @@ type FundWalletRequest struct { func (r *walletsRoutes) fundWallet(c *gin.Context) { var request FundWalletRequest if err := c.ShouldBindJSON(&request); err != nil { - errorResponse(c, http.StatusBadRequest, "invalid request body: %x") + errorResponse(c, http.StatusBadRequest, "invalid request body: %x", err) return } wallet, err := r.w.Get(request.Id) if err != nil { fmt.Println(err) - errorResponse(c, http.StatusNotFound, "wallet not found") + errorResponse(c, http.StatusNotFound, "wallet not found", err) return } if wallet.Funded { - errorResponse(c, http.StatusBadRequest, "wallet is already funded") + errorResponse(c, http.StatusBadRequest, "wallet is already funded", err) return } @@ -129,13 +129,13 @@ func (r *walletsRoutes) fundWallet(c *gin.Context) { Account: wallet.Key.PublicKey, }) if err != nil { - errorResponse(c, http.StatusInternalServerError, "messaging problems") + errorResponse(c, http.StatusInternalServerError, "messaging problems", err) } fundRes := res.Message.(entity.HorizonResponse) if fundRes.StatusCode != 200 { - errorResponse(c, http.StatusInternalServerError, "friendbot error") + errorResponse(c, http.StatusInternalServerError, "friendbot error", err) return } @@ -143,7 +143,7 @@ func (r *walletsRoutes) fundWallet(c *gin.Context) { wallet, err = r.w.Update(wallet) if err != nil { fmt.Println(err) - errorResponse(c, http.StatusInternalServerError, "database problems") + errorResponse(c, http.StatusInternalServerError, "database problems", err) return } From 6ca4da08266869ba43f17ecccfbb93ab50cb1e5d Mon Sep 17 00:00:00 2001 From: Wellington Junior Date: Tue, 22 Aug 2023 09:41:19 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Refactor=20the=20er?= =?UTF-8?q?ror=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/docs/docs.go | 254 +++++++++++++++++- backend/docs/swagger.json | 254 +++++++++++++++++- backend/docs/swagger.yaml | 177 +++++++++++- backend/internal/controller/http/v1/assets.go | 47 +--- backend/internal/controller/http/v1/auth.go | 2 +- backend/internal/controller/http/v1/error.go | 16 +- backend/internal/controller/http/v1/router.go | 2 +- backend/internal/controller/http/v1/utils.go | 29 +- .../internal/controller/http/v1/wallets.go | 10 +- backend/internal/entity/message.go | 7 +- .../internal/usecase/repo/wallet_postgres.go | 26 +- 11 files changed, 727 insertions(+), 97 deletions(-) diff --git a/backend/docs/docs.go b/backend/docs/docs.go index 7786fa24..aa022e8e 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -48,6 +48,35 @@ const docTemplate = `{ } } }, + "/asset": { + "get": { + "description": "Get asset by id", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Assets" + ], + "summary": "Get asset by id", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/entity.Asset" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/v1.response" + } + } + } + } + }, "/assets": { "get": { "description": "Get all assets", @@ -390,6 +419,58 @@ const docTemplate = `{ } } }, + "/contract": { + "post": { + "description": "Create new contract", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Contract" + ], + "summary": "Create a new contract", + "parameters": [ + { + "description": "Contract info", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.CreateContractRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/entity.Contract" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/v1.response" + } + } + } + } + }, "/role": { "get": { "description": "List role", @@ -788,6 +869,57 @@ const docTemplate = `{ } } }, + "/vault-delete/{id}": { + "put": { + "description": "Update a vault by providing the Vault ID and the updated the status.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Vault" + ], + "summary": "Update a vault status", + "parameters": [ + { + "type": "string", + "format": "uuid", + "description": "Vault ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Updated vault status information", + "schema": { + "$ref": "#/definitions/entity.Vault" + } + }, + "400": { + "description": "Bad Request: Invalid input data", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "404": { + "description": "Not Found: Vault not found", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "500": { + "description": "Internal Server Error: Failed to update vault status", + "schema": { + "$ref": "#/definitions/v1.response" + } + } + } + } + }, "/wallets": { "get": { "description": "List wallets by type", @@ -951,6 +1083,43 @@ const docTemplate = `{ } } }, + "entity.Contract": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "asset": { + "$ref": "#/definitions/entity.Asset" + }, + "created_at": { + "type": "string" + }, + "id": { + "type": "integer", + "example": 1 + }, + "min_deposit": { + "type": "integer" + }, + "name": { + "type": "string", + "example": "Smart Contract" + }, + "penalty_rate": { + "type": "integer" + }, + "term": { + "type": "integer" + }, + "vault": { + "$ref": "#/definitions/entity.Vault" + }, + "yield_rate": { + "type": "integer" + } + } + }, "entity.Key": { "type": "object", "properties": { @@ -1064,6 +1233,9 @@ const docTemplate = `{ "entity.Vault": { "type": "object", "properties": { + "active": { + "type": "integer" + }, "id": { "type": "integer", "example": 1 @@ -1090,6 +1262,10 @@ const docTemplate = `{ "name": { "type": "string", "example": "Treasury" + }, + "theme": { + "type": "string", + "example": "blue" } } }, @@ -1204,6 +1380,53 @@ const docTemplate = `{ } } }, + "v1.CreateContractRequest": { + "type": "object", + "required": [ + "address", + "asset_id", + "min_deposit", + "name", + "penalty_rate", + "term", + "vault_id", + "yield_rate" + ], + "properties": { + "address": { + "type": "string", + "example": "GSDSC..." + }, + "asset_id": { + "type": "string", + "example": "1" + }, + "min_deposit": { + "type": "integer", + "example": 1 + }, + "name": { + "type": "string", + "example": "Treasury" + }, + "penalty_rate": { + "type": "integer", + "example": 1 + }, + "term": { + "type": "integer", + "example": 1 + }, + "vault_id": { + "type": "string", + "example": "1" + }, + "yield_rate": { + "type": "integer", + "example": 1 + } + } + }, "v1.CreateVaultCategoryRequest": { "type": "object", "required": [ @@ -1213,6 +1436,10 @@ const docTemplate = `{ "name": { "type": "string", "example": "Treasury" + }, + "theme": { + "type": "string", + "example": "blue" } } }, @@ -1348,12 +1575,25 @@ const docTemplate = `{ "v1.UpdateVaultAssetRequest": { "type": "object", "required": [ - "asset_id" + "asset_code", + "asset_issuer_pk" ], "properties": { - "asset_id": { - "type": "integer", - "example": 1 + "asset_code": { + "type": "string", + "example": "1" + }, + "asset_issuer_pk": { + "type": "string", + "example": "1" + }, + "is_add": { + "type": "boolean", + "example": true + }, + "is_remove": { + "type": "boolean", + "example": false } } }, @@ -1377,8 +1617,10 @@ const docTemplate = `{ "type": "object", "properties": { "error": { - "type": "string", - "example": "message" + "type": "string" + }, + "message": { + "type": "string" } } }, diff --git a/backend/docs/swagger.json b/backend/docs/swagger.json index 35ab077d..5fb620f3 100644 --- a/backend/docs/swagger.json +++ b/backend/docs/swagger.json @@ -36,6 +36,35 @@ } } }, + "/asset": { + "get": { + "description": "Get asset by id", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Assets" + ], + "summary": "Get asset by id", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/entity.Asset" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/v1.response" + } + } + } + } + }, "/assets": { "get": { "description": "Get all assets", @@ -378,6 +407,58 @@ } } }, + "/contract": { + "post": { + "description": "Create new contract", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Contract" + ], + "summary": "Create a new contract", + "parameters": [ + { + "description": "Contract info", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/v1.CreateContractRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/entity.Contract" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/v1.response" + } + } + } + } + }, "/role": { "get": { "description": "List role", @@ -776,6 +857,57 @@ } } }, + "/vault-delete/{id}": { + "put": { + "description": "Update a vault by providing the Vault ID and the updated the status.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Vault" + ], + "summary": "Update a vault status", + "parameters": [ + { + "type": "string", + "format": "uuid", + "description": "Vault ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Updated vault status information", + "schema": { + "$ref": "#/definitions/entity.Vault" + } + }, + "400": { + "description": "Bad Request: Invalid input data", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "404": { + "description": "Not Found: Vault not found", + "schema": { + "$ref": "#/definitions/v1.response" + } + }, + "500": { + "description": "Internal Server Error: Failed to update vault status", + "schema": { + "$ref": "#/definitions/v1.response" + } + } + } + } + }, "/wallets": { "get": { "description": "List wallets by type", @@ -939,6 +1071,43 @@ } } }, + "entity.Contract": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "asset": { + "$ref": "#/definitions/entity.Asset" + }, + "created_at": { + "type": "string" + }, + "id": { + "type": "integer", + "example": 1 + }, + "min_deposit": { + "type": "integer" + }, + "name": { + "type": "string", + "example": "Smart Contract" + }, + "penalty_rate": { + "type": "integer" + }, + "term": { + "type": "integer" + }, + "vault": { + "$ref": "#/definitions/entity.Vault" + }, + "yield_rate": { + "type": "integer" + } + } + }, "entity.Key": { "type": "object", "properties": { @@ -1052,6 +1221,9 @@ "entity.Vault": { "type": "object", "properties": { + "active": { + "type": "integer" + }, "id": { "type": "integer", "example": 1 @@ -1078,6 +1250,10 @@ "name": { "type": "string", "example": "Treasury" + }, + "theme": { + "type": "string", + "example": "blue" } } }, @@ -1192,6 +1368,53 @@ } } }, + "v1.CreateContractRequest": { + "type": "object", + "required": [ + "address", + "asset_id", + "min_deposit", + "name", + "penalty_rate", + "term", + "vault_id", + "yield_rate" + ], + "properties": { + "address": { + "type": "string", + "example": "GSDSC..." + }, + "asset_id": { + "type": "string", + "example": "1" + }, + "min_deposit": { + "type": "integer", + "example": 1 + }, + "name": { + "type": "string", + "example": "Treasury" + }, + "penalty_rate": { + "type": "integer", + "example": 1 + }, + "term": { + "type": "integer", + "example": 1 + }, + "vault_id": { + "type": "string", + "example": "1" + }, + "yield_rate": { + "type": "integer", + "example": 1 + } + } + }, "v1.CreateVaultCategoryRequest": { "type": "object", "required": [ @@ -1201,6 +1424,10 @@ "name": { "type": "string", "example": "Treasury" + }, + "theme": { + "type": "string", + "example": "blue" } } }, @@ -1336,12 +1563,25 @@ "v1.UpdateVaultAssetRequest": { "type": "object", "required": [ - "asset_id" + "asset_code", + "asset_issuer_pk" ], "properties": { - "asset_id": { - "type": "integer", - "example": 1 + "asset_code": { + "type": "string", + "example": "1" + }, + "asset_issuer_pk": { + "type": "string", + "example": "1" + }, + "is_add": { + "type": "boolean", + "example": true + }, + "is_remove": { + "type": "boolean", + "example": false } } }, @@ -1365,8 +1605,10 @@ "type": "object", "properties": { "error": { - "type": "string", - "example": "message" + "type": "string" + }, + "message": { + "type": "string" } } }, diff --git a/backend/docs/swagger.yaml b/backend/docs/swagger.yaml index dd9dece2..0a1dcff6 100644 --- a/backend/docs/swagger.yaml +++ b/backend/docs/swagger.yaml @@ -20,6 +20,31 @@ definitions: example: USD Coin type: string type: object + entity.Contract: + properties: + address: + type: string + asset: + $ref: '#/definitions/entity.Asset' + created_at: + type: string + id: + example: 1 + type: integer + min_deposit: + type: integer + name: + example: Smart Contract + type: string + penalty_rate: + type: integer + term: + type: integer + vault: + $ref: '#/definitions/entity.Vault' + yield_rate: + type: integer + type: object entity.Key: properties: id: @@ -96,6 +121,8 @@ definitions: type: object entity.Vault: properties: + active: + type: integer id: example: 1 type: integer @@ -115,6 +142,9 @@ definitions: name: example: Treasury type: string + theme: + example: blue + type: string type: object entity.Wallet: properties: @@ -196,11 +226,50 @@ definitions: - code - name type: object + v1.CreateContractRequest: + properties: + address: + example: GSDSC... + type: string + asset_id: + example: "1" + type: string + min_deposit: + example: 1 + type: integer + name: + example: Treasury + type: string + penalty_rate: + example: 1 + type: integer + term: + example: 1 + type: integer + vault_id: + example: "1" + type: string + yield_rate: + example: 1 + type: integer + required: + - address + - asset_id + - min_deposit + - name + - penalty_rate + - term + - vault_id + - yield_rate + type: object v1.CreateVaultCategoryRequest: properties: name: example: Treasury type: string + theme: + example: blue + type: string required: - name type: object @@ -298,11 +367,21 @@ definitions: type: object v1.UpdateVaultAssetRequest: properties: - asset_id: - example: 1 - type: integer + asset_code: + example: "1" + type: string + asset_issuer_pk: + example: "1" + type: string + is_add: + example: true + type: boolean + is_remove: + example: false + type: boolean required: - - asset_id + - asset_code + - asset_issuer_pk type: object v1.UpdateVaultCategoryRequest: properties: @@ -318,7 +397,8 @@ definitions: v1.response: properties: error: - example: message + type: string + message: type: string type: object v1.userResponse: @@ -350,6 +430,25 @@ paths: summary: Get all vault categories tags: - Vault category + /asset: + get: + consumes: + - application/json + description: Get asset by id + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/entity.Asset' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/v1.response' + summary: Get asset by id + tags: + - Assets /assets: get: consumes: @@ -574,6 +673,40 @@ paths: summary: Transfer an asset tags: - Assets + /contract: + post: + consumes: + - application/json + description: Create new contract + parameters: + - description: Contract info + in: body + name: request + required: true + schema: + $ref: '#/definitions/v1.CreateContractRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/entity.Contract' + "400": + description: Bad Request + schema: + $ref: '#/definitions/v1.response' + "404": + description: Not Found + schema: + $ref: '#/definitions/v1.response' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/v1.response' + summary: Create a new contract + tags: + - Contract /role: get: consumes: @@ -838,6 +971,40 @@ paths: summary: Update a vault category tags: - Vault Category + /vault-delete/{id}: + put: + consumes: + - application/json + description: Update a vault by providing the Vault ID and the updated the status. + parameters: + - description: Vault ID + format: uuid + in: path + name: id + required: true + type: string + produces: + - application/json + responses: + "200": + description: Updated vault status information + schema: + $ref: '#/definitions/entity.Vault' + "400": + description: 'Bad Request: Invalid input data' + schema: + $ref: '#/definitions/v1.response' + "404": + description: 'Not Found: Vault not found' + schema: + $ref: '#/definitions/v1.response' + "500": + description: 'Internal Server Error: Failed to update vault status' + schema: + $ref: '#/definitions/v1.response' + summary: Update a vault status + tags: + - Vault /wallets: get: consumes: diff --git a/backend/internal/controller/http/v1/assets.go b/backend/internal/controller/http/v1/assets.go index d3045046..36f6ed10 100644 --- a/backend/internal/controller/http/v1/assets.go +++ b/backend/internal/controller/http/v1/assets.go @@ -180,7 +180,7 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { }) } - res, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ MainSource: sponsor.Key.PublicKey, PublicKeys: []string{sponsor.Key.PublicKey, distPk, issuerPk}, Operations: ops, @@ -189,11 +189,6 @@ func (r *assetsRoutes) createAsset(c *gin.Context) { errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return } - _, ok = res.Message.(entity.EnvelopeResponse) - if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) - return - } asset := entity.Asset{ Name: request.Name, @@ -270,7 +265,7 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { Origin: asset.Issuer.Key.PublicKey, }, } - res, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ MainSource: asset.Issuer.Key.PublicKey, PublicKeys: []string{asset.Issuer.Key.PublicKey}, Operations: ops, @@ -280,12 +275,6 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) { return } - _, ok := res.Message.(entity.EnvelopeResponse) - if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) - return - } - c.JSON(http.StatusOK, gin.H{"message": "asset minted"}) } @@ -335,7 +324,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { }, } - res, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ MainSource: asset.Distributor.Key.PublicKey, PublicKeys: []string{asset.Distributor.Key.PublicKey}, Operations: ops, @@ -343,12 +332,7 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) { if err != nil { errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return - } - _, ok := res.Message.(entity.EnvelopeResponse) - if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) - return } c.JSON(http.StatusOK, gin.H{ @@ -408,20 +392,13 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) { }, } - res, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ MainSource: sourceWallet.Key.PublicKey, PublicKeys: []string{sourceWallet.Key.PublicKey}, Operations: ops, }) if err != nil { errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) - return - } - - _, ok := res.Message.(entity.EnvelopeResponse) - if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) - return } c.JSON(http.StatusOK, gin.H{"message": "asset transferred"}) @@ -475,20 +452,13 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) { }, } - res, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ MainSource: sponsor.Key.PublicKey, PublicKeys: []string{asset.Issuer.Key.PublicKey, sponsor.Key.PublicKey}, Operations: ops, }) if err != nil { errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) - return - } - - _, ok := res.Message.(entity.EnvelopeResponse) - if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) - return } c.JSON(http.StatusOK, gin.H{"message": "asset clawed back"}) @@ -547,7 +517,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { return } - res, err := r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ + _, err = r.m.SendMessage(entity.EnvelopeChannel, entity.EnvelopeRequest{ MainSource: sponsor.Key.PublicKey, PublicKeys: []string{asset.Issuer.Key.PublicKey, sponsor.Key.PublicKey}, Operations: []entity.Operation{op}, @@ -555,12 +525,7 @@ func (r *assetsRoutes) updateAuthFlags(c *gin.Context) { if err != nil { errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err) return - } - _, ok := res.Message.(entity.EnvelopeResponse) - if !ok { - errorResponse(c, http.StatusInternalServerError, "unexpected starlabs response", err) - return } c.JSON(http.StatusOK, gin.H{"message": "authorization flags updated"}) diff --git a/backend/internal/controller/http/v1/auth.go b/backend/internal/controller/http/v1/auth.go index 8506a58a..c1831db3 100644 --- a/backend/internal/controller/http/v1/auth.go +++ b/backend/internal/controller/http/v1/auth.go @@ -39,7 +39,7 @@ func ValidateToken(signedToken string, jwtSecretKey string) (err error) { func GenerateJWT(user entity.User, jwtSecretKey string) (tokenString string, err error) { expirationTime := time.Now().Add(1440 * time.Minute) claims := &JWTClaim{ - ID: user.ID, // TODO + ID: user.ID, Name: user.Name, Email: user.Email, StandardClaims: jwt.StandardClaims{ diff --git a/backend/internal/controller/http/v1/error.go b/backend/internal/controller/http/v1/error.go index f0db75ba..d7838f9b 100644 --- a/backend/internal/controller/http/v1/error.go +++ b/backend/internal/controller/http/v1/error.go @@ -5,12 +5,18 @@ import ( ) type response struct { - Error string `json:"error" example:"message"` + Message string `json:"message"` + Error string `json:"error,omitempty"` } func errorResponse(c *gin.Context, code int, msg string, err error) { - c.AbortWithStatusJSON(code, gin.H{ - "message": msg, - "error": err, - }) + var errorMsg string + if err != nil { + errorMsg = err.Error() + } + resp := response{ + Message: msg, + Error: errorMsg, + } + c.AbortWithStatusJSON(code, resp) } diff --git a/backend/internal/controller/http/v1/router.go b/backend/internal/controller/http/v1/router.go index 069e4260..57a5edb9 100644 --- a/backend/internal/controller/http/v1/router.go +++ b/backend/internal/controller/http/v1/router.go @@ -62,7 +62,7 @@ func NewRouter( groupV1 := handler.Group("/v1") { newUserRoutes(groupV1, userUseCase, authUseCase, rolePermissionUc) - newWalletsRoutes(groupV1, walletUseCase, messengerController) + newWalletsRoutes(groupV1, walletUseCase, messengerController, authUseCase) newAssetsRoutes(groupV1, walletUseCase, assetUseCase, messengerController, authUseCase) newRoleRoutes(groupV1, roleUseCase, messengerController) newRolePermissionsRoutes(groupV1, rolePermissionUc, messengerController) diff --git a/backend/internal/controller/http/v1/utils.go b/backend/internal/controller/http/v1/utils.go index ff102150..2939525c 100644 --- a/backend/internal/controller/http/v1/utils.go +++ b/backend/internal/controller/http/v1/utils.go @@ -3,6 +3,7 @@ package v1 import ( "crypto/rand" "crypto/sha256" + "encoding/json" "fmt" "github.com/CheesecakeLabs/token-factory-v2/backend/internal/entity" @@ -36,18 +37,22 @@ func (m *HTTPControllerMessenger) SendMessage(chanName string, value interface{} res := <-channel notify.Stop(msgKey, channel) - // if notifyData, ok := res.(*entity.NotifyData); ok { - // switch msg := notifyData.Message.(type) { - // case entity.EnvelopeResponse: - // if msg.StatusCode != 200 { - // return nil, fmt.Errorf("sendMessage - error response: %v", msg) - // } - // return notifyData, nil - - // default: - // return notifyData, nil - // } - // } + if notifyData, ok := res.(*entity.NotifyData); ok { + switch msg := notifyData.Message.(type) { + case entity.EnvelopeResponse: + if msg.Error != nil { + errorDetails := msg.Error.(map[string]interface{}) + errorDetailsJSON, err := json.Marshal(errorDetails) + if err != nil { + return notifyData, fmt.Errorf("error marshaling error details: %v", err) + } + return notifyData, fmt.Errorf(string(errorDetailsJSON)) + } + return notifyData, nil + default: + return notifyData, nil + } + } return res.(*entity.NotifyData), nil } diff --git a/backend/internal/controller/http/v1/wallets.go b/backend/internal/controller/http/v1/wallets.go index 156be433..90fb3843 100644 --- a/backend/internal/controller/http/v1/wallets.go +++ b/backend/internal/controller/http/v1/wallets.go @@ -1,7 +1,6 @@ package v1 import ( - "fmt" "net/http" "github.com/CheesecakeLabs/token-factory-v2/backend/internal/entity" @@ -12,11 +11,12 @@ import ( type walletsRoutes struct { w usecase.WalletUseCase m HTTPControllerMessenger + a usecase.AuthUseCase } -func newWalletsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase, m HTTPControllerMessenger) { - r := &walletsRoutes{w, m} - h := handler.Group("/wallets") +func newWalletsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase, m HTTPControllerMessenger, a usecase.AuthUseCase) { + r := &walletsRoutes{w, m, a} + h := handler.Group("/wallets").Use(Auth(a.ValidateToken())) { h.GET("", r.list) h.POST("", r.create) @@ -113,7 +113,6 @@ func (r *walletsRoutes) fundWallet(c *gin.Context) { wallet, err := r.w.Get(request.Id) if err != nil { - fmt.Println(err) errorResponse(c, http.StatusNotFound, "wallet not found", err) return } @@ -142,7 +141,6 @@ func (r *walletsRoutes) fundWallet(c *gin.Context) { wallet.Funded = true wallet, err = r.w.Update(wallet) if err != nil { - fmt.Println(err) errorResponse(c, http.StatusInternalServerError, "database problems", err) return } diff --git a/backend/internal/entity/message.go b/backend/internal/entity/message.go index 289cf625..20eefb52 100644 --- a/backend/internal/entity/message.go +++ b/backend/internal/entity/message.go @@ -39,9 +39,10 @@ type ( } EnvelopeResponse struct { - Id int `json:"id"` - Hash string `json:"hash"` - StatusCode int `json:"statusCode"` + Id int `json:"id"` + Hash string `json:"hash"` + StatusCode int `json:"statusCode"` + Error interface{} `json:"error"` } Operation struct { diff --git a/backend/internal/usecase/repo/wallet_postgres.go b/backend/internal/usecase/repo/wallet_postgres.go index 9b2f6005..30acb36d 100644 --- a/backend/internal/usecase/repo/wallet_postgres.go +++ b/backend/internal/usecase/repo/wallet_postgres.go @@ -19,12 +19,15 @@ func NewWalletRepo(pg *postgres.Postgres) WalletRepo { } func (r WalletRepo) GetWallet(id int) (entity.Wallet, error) { - stmt := `SELECT * FROM wallet WHERE id=$1` + stmt := `SELECT w.id, w.type, w.funded, k.id, k.public_key, k.weight, k.wallet_id + FROM Wallet w + JOIN Key k ON w.id = k.wallet_id + WHERE w.id=$1` row := r.Db.QueryRow(stmt, id) var wallet entity.Wallet - err := row.Scan(&wallet.Id, &wallet.Type, &wallet.Funded) - + var key entity.Key + err := row.Scan(&wallet.Id, &wallet.Type, &wallet.Funded, &key.Id, &key.PublicKey, &key.Weight, &key.WalletId) if err != nil { if err == sql.ErrNoRows { return entity.Wallet{}, fmt.Errorf("WalletRepo - GetWallet - wallet not found") @@ -32,13 +35,17 @@ func (r WalletRepo) GetWallet(id int) (entity.Wallet, error) { return entity.Wallet{}, fmt.Errorf("WalletRepo - GetWallet - row.Scan: %w", err) } + wallet.Key = key + return wallet, nil } func (r WalletRepo) GetWallets(wType string) ([]entity.Wallet, error) { - stmt := `SELECT * FROM Wallet WHERE type=$1` + stmt := `SELECT w.id, w.type, w.funded, k.id, k.public_key, k.weight, k.wallet_id + FROM Wallet w + JOIN Key k ON w.id = k.wallet_id + WHERE w.type=$1` rows, err := r.Db.Query(stmt, wType) - if err != nil { return nil, fmt.Errorf("WalletRepo - GetWallets - db.Query: %w", err) } @@ -49,12 +56,14 @@ func (r WalletRepo) GetWallets(wType string) ([]entity.Wallet, error) { for rows.Next() { var wallet entity.Wallet + var key entity.Key - err = rows.Scan(&wallet.Id, &wallet.Type, &wallet.Funded) + err = rows.Scan(&wallet.Id, &wallet.Type, &wallet.Funded, &key.Id, &key.PublicKey, &key.Weight, &key.WalletId) if err != nil { return nil, fmt.Errorf("WalletRepo - GetWallets - rows.Scan: %w", err) } + wallet.Key = key entities = append(entities, wallet) } @@ -67,7 +76,6 @@ func (r WalletRepo) GetKeyByWallet(walletId int) (entity.Key, error) { var key entity.Key err := row.Scan(&key.Id, &key.PublicKey, &key.Weight, &key.WalletId) - if err != nil { if err == sql.ErrNoRows { return entity.Key{}, fmt.Errorf("WalletRepo - GetKeyByWallet - key not found") @@ -82,7 +90,6 @@ func (r WalletRepo) CreateWallet(data entity.Wallet) (entity.Wallet, error) { res := data stmt := `INSERT INTO Wallet (type) VALUES ($1) RETURNING id;` err := r.Db.QueryRow(stmt, data.Type).Scan(&res.Id) - if err != nil { return entity.Wallet{}, fmt.Errorf("WalletRepo - CreateWallet - db.QueryRow: %w", err) } @@ -94,7 +101,6 @@ func (r WalletRepo) CreateKey(data entity.Key) (entity.Key, error) { res := data stmt := `INSERT INTO Key (public_key, weight, wallet_id) VALUES ($1, $2, $3) RETURNING id;` err := r.Db.QueryRow(stmt, data.PublicKey, data.Weight, data.WalletId).Scan(&res.Id) - if err != nil { return entity.Key{}, fmt.Errorf("WalletRepo - CreateKey - db.QueryRow: %w", err) } @@ -114,13 +120,11 @@ func (r WalletRepo) CreateWalletWithKey(data entity.Wallet) (entity.Wallet, erro return entity.Wallet{}, fmt.Errorf("WalletRepo - CreateWalletWithKey - uc.repo.CreateKey: %w", err) } return wallet, nil - } func (r WalletRepo) UpdateWallet(data entity.Wallet) (entity.Wallet, error) { stmt := `UPDATE Wallet SET funded=($1) WHERE id=($2);` result, err := r.Db.Exec(stmt, data.Funded, data.Id) - if err != nil { return entity.Wallet{}, fmt.Errorf("WalletRepo - UpdateWallet - db.Exec: %v", err) }