From 6ca4da08266869ba43f17ecccfbb93ab50cb1e5d Mon Sep 17 00:00:00 2001 From: Wellington Junior Date: Tue, 22 Aug 2023 09:41:19 -0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Refactor=20the=20error?= =?UTF-8?q?=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) }