2024-04-09 15:52:37 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2024-05-24 18:36:50 +00:00
|
|
|
"amocrm/internal/service_errors"
|
|
|
|
"errors"
|
2024-04-09 15:52:37 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"go.uber.org/zap"
|
2024-05-26 23:09:13 +00:00
|
|
|
"strings"
|
2024-04-09 15:52:37 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Controller) UpdateListUsers(ctx *fiber.Ctx) error {
|
2024-04-21 14:52:55 +00:00
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
}
|
|
|
|
|
2024-05-05 12:22:54 +00:00
|
|
|
//accountID := "654a8909725f47e926f0bebc"
|
2024-04-21 14:52:55 +00:00
|
|
|
|
|
|
|
err := c.service.UpdateListUsers(ctx.Context(), accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-04-21 15:24:13 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
req, err := extractParams(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-21 15:24:13 +00:00
|
|
|
response, err := c.service.GettingUserWithPagination(ctx.Context(), req, accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
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 {
|
2024-04-11 09:55:26 +00:00
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
}
|
2024-04-09 15:52:37 +00:00
|
|
|
|
2024-04-11 09:55:26 +00:00
|
|
|
err := c.service.SoftDeleteAccount(ctx.Context(), accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
2024-04-11 09:55:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GetCurrentAccount(ctx *fiber.Ctx) error {
|
2024-04-11 09:55:26 +00:00
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
}
|
2024-04-09 15:52:37 +00:00
|
|
|
|
2024-04-11 09:55:26 +00:00
|
|
|
response, err := c.service.GetCurrentAccount(ctx.Context(), accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
2024-05-24 18:36:50 +00:00
|
|
|
if errors.Is(err, service_errors.ErrUserNotFound) {
|
|
|
|
return ctx.Status(fiber.StatusNotFound).SendString("user not found")
|
|
|
|
}
|
2024-04-23 11:18:45 +00:00
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) ConnectAccount(ctx *fiber.Ctx) error {
|
2024-05-26 23:12:16 +00:00
|
|
|
authHeader := ctx.Get("Authorization")
|
2024-05-26 23:05:41 +00:00
|
|
|
if authHeader == "" {
|
2024-05-26 23:09:13 +00:00
|
|
|
ctx.Status(fiber.StatusUnauthorized).SendString("no JWT found")
|
2024-05-26 23:05:41 +00:00
|
|
|
return nil
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 23:05:41 +00:00
|
|
|
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
|
|
|
if tokenString == authHeader {
|
2024-05-26 23:09:13 +00:00
|
|
|
ctx.Status(fiber.StatusUnauthorized).SendString("invalid JWT Header: missing Bearer")
|
2024-05-26 23:05:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-04-20 12:57:37 +00:00
|
|
|
|
2024-05-26 23:05:41 +00:00
|
|
|
response, err := c.service.ConnectAccount(ctx.Context(), tokenString)
|
2024-04-09 15:52:37 +00:00
|
|
|
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)
|
|
|
|
}
|