chore: adapt code to new teleport feature
This commit is contained in:
@@ -4,9 +4,9 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/delivery/middleware/perms"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/customerService"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/response"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
||||
@@ -28,37 +28,34 @@ func CustomerHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
handler := NewCustomerHandler()
|
||||
|
||||
r.Get("", handler.customerData)
|
||||
r.Get("/list", handler.listCustomers)
|
||||
// r.Get("/list", handler.listCustomers)
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *customerHandler) customerData(fc fiber.Ctx) error {
|
||||
var customerId uint
|
||||
|
||||
user, ok := localeExtractor.GetCustomer(fc)
|
||||
if !ok || user == nil {
|
||||
return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
}
|
||||
|
||||
customerIdStr := fc.Query("id")
|
||||
if customerIdStr != "" {
|
||||
user, ok := fc.Locals("user").(*model.UserSession)
|
||||
if !ok {
|
||||
return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
}
|
||||
id, err := strconv.ParseUint(customerIdStr, 10, 64)
|
||||
if err != nil {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
|
||||
if user.UserID != uint(id) && !user.HasPermission(perms.UserReadAny) {
|
||||
if user.ID != uint(id) && !user.HasPermission(perms.UserReadAny) {
|
||||
return fc.Status(fiber.StatusForbidden).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrForbidden)))
|
||||
}
|
||||
|
||||
customerId = uint(id)
|
||||
} else {
|
||||
id, ok := fc.Locals("userID").(uint)
|
||||
if !ok {
|
||||
return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
}
|
||||
customerId = id
|
||||
customerId = user.ID
|
||||
}
|
||||
|
||||
customer, err := h.service.GetById(customerId)
|
||||
@@ -70,40 +67,41 @@ func (h *customerHandler) customerData(fc fiber.Ctx) error {
|
||||
return fc.JSON(response.Make(&customer, 0, i18n.T_(fc, response.Message_OK)))
|
||||
}
|
||||
|
||||
func (h *customerHandler) listCustomers(fc fiber.Ctx) error {
|
||||
var customerId uint
|
||||
customerIdStr := fc.Query("id")
|
||||
if customerIdStr != "" {
|
||||
user, ok := fc.Locals("user").(*model.UserSession)
|
||||
if !ok {
|
||||
return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
}
|
||||
id, err := strconv.ParseUint(customerIdStr, 10, 64)
|
||||
if err != nil {
|
||||
return fiber.ErrBadRequest
|
||||
}
|
||||
// func (h *customerHandler) listCustomers(fc fiber.Ctx) error {
|
||||
// var customerId uint
|
||||
// customerIdStr := fc.Query("id")
|
||||
// if customerIdStr != "" {
|
||||
|
||||
if user.UserID != uint(id) && !user.HasPermission(perms.UserReadAny) {
|
||||
return fc.Status(fiber.StatusForbidden).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrForbidden)))
|
||||
}
|
||||
// user, ok := localeExtractor.GetCustomer(fc)
|
||||
// if !ok || user == nil {
|
||||
// return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
// JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
// }
|
||||
// id, err := strconv.ParseUint(customerIdStr, 10, 64)
|
||||
// if err != nil {
|
||||
// return fiber.ErrBadRequest
|
||||
// }
|
||||
|
||||
customerId = uint(id)
|
||||
} else {
|
||||
id, ok := fc.Locals("userID").(uint)
|
||||
if !ok {
|
||||
return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
}
|
||||
customerId = id
|
||||
}
|
||||
// if user.UserID != uint(id) && !user.HasPermission(perms.UserReadAny) {
|
||||
// return fc.Status(fiber.StatusForbidden).
|
||||
// JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrForbidden)))
|
||||
// }
|
||||
|
||||
customer, err := h.service.GetById(customerId)
|
||||
if err != nil {
|
||||
return fc.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err)))
|
||||
}
|
||||
// customerId = uint(id)
|
||||
// } else {
|
||||
// id, ok := fc.Locals("userID").(uint)
|
||||
// if !ok {
|
||||
// return fc.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
||||
// JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, responseErrors.ErrBadAttribute)))
|
||||
// }
|
||||
// customerId = id
|
||||
// }
|
||||
|
||||
return fc.JSON(response.Make(&customer, 0, i18n.T_(fc, response.Message_OK)))
|
||||
}
|
||||
// customer, err := h.service.GetById(customerId)
|
||||
// if err != nil {
|
||||
// return fc.Status(responseErrors.GetErrorStatus(err)).
|
||||
// JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(fc, err)))
|
||||
// }
|
||||
|
||||
// return fc.JSON(response.Make(&customer, 0, i18n.T_(fc, response.Message_OK)))
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user