60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package controllers
|
||
|
||
import (
|
||
"amocrm/internal/service"
|
||
"amocrm/internal/tools"
|
||
"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")
|
||
}
|
||
|
||
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")
|
||
}
|
||
|
||
req := service.ParamsWebhookCreate{
|
||
Code: code,
|
||
Referer: referer,
|
||
AccountID: *accountID,
|
||
FromWidget: fromWidget,
|
||
Platform: platform,
|
||
}
|
||
|
||
err = c.service.WebhookCreate(ctx.Context(), req)
|
||
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)
|
||
}
|