84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
package controllers
|
||
|
||
import (
|
||
"amocrm/internal/repository"
|
||
"errors"
|
||
"github.com/gofiber/fiber/v2"
|
||
"go.uber.org/zap"
|
||
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/middleware"
|
||
)
|
||
|
||
func (c *Controller) UpdateListUsers(ctx *fiber.Ctx) error {
|
||
err := c.service.UpdateListUsers(ctx.Context())
|
||
|
||
if err != nil {
|
||
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
||
}
|
||
return ctx.SendStatus(fiber.StatusOK)
|
||
}
|
||
|
||
func (c *Controller) GettingUserFromCash(ctx *fiber.Ctx) error {
|
||
req, err := extractParams(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
response, err := c.service.GettingUserWithPagination(ctx.Context(), req)
|
||
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 {
|
||
switch {
|
||
case errors.Is(err, repository.ErrUserNotFound):
|
||
return ctx.Status(fiber.StatusNotFound).SendString("User not found")
|
||
default:
|
||
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
||
}
|
||
}
|
||
return ctx.Status(fiber.StatusOK).JSON(response)
|
||
}
|
||
|
||
// при запросе на этот контроллер приходит только токен из которого получаем id аккаунта в ис
|
||
// отдает ссылку на подключение к amocrm и создает модель в монге имеющую связь с аккаунтом
|
||
// в постгресе по accountid
|
||
// ссылку составляет сервис соц авторизации
|
||
func (c *Controller) ConnectAccount(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.ConnectAccount(ctx.Context(), accountID)
|
||
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)
|
||
}
|