amocrm/internal/controllers/user.go
2024-05-26 23:09:13 +00:00

99 lines
2.8 KiB
Go

package controllers
import (
"amocrm/internal/service_errors"
"errors"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"strings"
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
)
func (c *Controller) UpdateListUsers(ctx *fiber.Ctx) error {
accountID, ok := middleware.GetAccountId(ctx)
if !ok {
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
}
//accountID := "654a8909725f47e926f0bebc"
err := c.service.UpdateListUsers(ctx.Context(), accountID)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.SendStatus(fiber.StatusOK)
}
func (c *Controller) GettingUserWithPagination(ctx *fiber.Ctx) error {
accountID, ok := middleware.GetAccountId(ctx)
if !ok {
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
}
req, err := extractParams(ctx)
if err != nil {
return err
}
response, err := c.service.GettingUserWithPagination(ctx.Context(), req, accountID)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}
func (c *Controller) SoftDeleteAccount(ctx *fiber.Ctx) error {
accountID, ok := middleware.GetAccountId(ctx)
if !ok {
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
}
err := c.service.SoftDeleteAccount(ctx.Context(), accountID)
if err != nil {
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
}
return ctx.SendStatus(fiber.StatusOK)
}
func (c *Controller) GetCurrentAccount(ctx *fiber.Ctx) error {
accountID, ok := middleware.GetAccountId(ctx)
if !ok {
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
}
response, err := c.service.GetCurrentAccount(ctx.Context(), accountID)
if err != nil {
if errors.Is(err, service_errors.ErrUserNotFound) {
return ctx.Status(fiber.StatusNotFound).SendString("user not found")
}
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}
func (c *Controller) ConnectAccount(ctx *fiber.Ctx) error {
authHeader := c.Get("Authorization")
if authHeader == "" {
ctx.Status(fiber.StatusUnauthorized).SendString("no JWT found")
return nil
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
if tokenString == authHeader {
ctx.Status(fiber.StatusUnauthorized).SendString("invalid JWT Header: missing Bearer")
return nil
}
response, err := c.service.ConnectAccount(ctx.Context(), tokenString)
if err != nil {
c.logger.Error("error connect account", zap.Error(err))
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}