2024-04-09 15:52:37 +00:00
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"amocrm/internal/service"
|
2024-04-19 16:05:42 +00:00
|
|
|
|
"amocrm/internal/tools"
|
2024-04-09 15:52:37 +00:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// контроллер на который редиректятся ответы по авторизации в амо
|
|
|
|
|
func (c *Controller) WebhookCreate(ctx *fiber.Ctx) error {
|
|
|
|
|
code := ctx.Query("code") // Authorization 20 минут
|
|
|
|
|
referer := ctx.Query("referer") // адрес аккаунта пользователя
|
|
|
|
|
state := ctx.Query("state") // строка которая передавалась в соц аус сервисе
|
|
|
|
|
fromWidget := ctx.Query("from_widget")
|
|
|
|
|
platform := ctx.Query("platform") // ru/global 1/2
|
|
|
|
|
noAccess := ctx.Query("error")
|
|
|
|
|
|
|
|
|
|
if noAccess != "" {
|
|
|
|
|
return ctx.Status(http.StatusForbidden).SendString("Access denied")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 16:05:42 +00:00
|
|
|
|
accountID, err := tools.DeserializeProtobufMessage(state)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Error("error Deserialize Protobuf Message", zap.Error(err))
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if accountID == nil {
|
|
|
|
|
c.logger.Error("error account id do not be nil", zap.Error(err))
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("nil account id")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
|
req := service.ParamsWebhookCreate{
|
|
|
|
|
Code: code,
|
|
|
|
|
Referer: referer,
|
2024-04-19 16:05:42 +00:00
|
|
|
|
AccountID: *accountID,
|
2024-04-09 15:52:37 +00:00
|
|
|
|
FromWidget: fromWidget,
|
|
|
|
|
Platform: platform,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 16:05:42 +00:00
|
|
|
|
err = c.service.WebhookCreate(ctx.Context(), req)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.logger.Error("error create webhook", zap.Error(err))
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Controller) WebhookDelete(ctx *fiber.Ctx) error {
|
|
|
|
|
err := c.service.WebhookDelete(ctx.Context())
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|