161 lines
4.1 KiB
Go
161 lines
4.1 KiB
Go
package user
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/valyala/fasthttp"
|
|
"mime/multipart"
|
|
"penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw"
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/internal/client"
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/internal/models"
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/internal/repository"
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/pkg/validate_controllers"
|
|
)
|
|
|
|
type VerifyUserControllerDeps struct {
|
|
Repository *repository.VerificationRepository
|
|
Telegram *client.Telegram
|
|
}
|
|
|
|
type VerifyUserController struct {
|
|
repository *repository.VerificationRepository
|
|
telegram *client.Telegram
|
|
}
|
|
|
|
func NewVerificationUserController(deps VerifyUserControllerDeps) *VerifyUserController {
|
|
return &VerifyUserController{
|
|
repository: deps.Repository,
|
|
telegram: deps.Telegram,
|
|
}
|
|
}
|
|
|
|
func (r *VerifyUserController) GetVerification(c *fiber.Ctx) error {
|
|
userID := c.Locals(models.UserCtxKey).(string)
|
|
if userID == "" {
|
|
return fiber.NewError(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
resp, err := r.repository.GetByUserID(c.Context(), userID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
}
|
|
|
|
if resp == nil {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(resp)
|
|
}
|
|
|
|
func (r *VerifyUserController) CreateVerification(c *fiber.Ctx) error {
|
|
var req models.ReqCreateVerification
|
|
baseURL := c.BaseURL()
|
|
hloger := log_mw.ExtractLogger(c)
|
|
|
|
userID := c.Locals(models.UserCtxKey).(string)
|
|
if userID == "" {
|
|
return fiber.NewError(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
err := c.BodyParser(&req)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
}
|
|
|
|
errValidate := validate_controllers.ValidateStruct(&req)
|
|
if errValidate != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
|
}
|
|
|
|
// INN FILE
|
|
innFH, err := c.FormFile("inn")
|
|
|
|
if err != nil || innFH.Size == 0 || innFH == nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "inn file required")
|
|
}
|
|
|
|
// RULE FILE
|
|
ruleFH, err := c.FormFile("rule")
|
|
|
|
if err != nil || ruleFH.Size == 0 || ruleFH == nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "rule file required")
|
|
}
|
|
|
|
// CERTIFICATE FILE
|
|
var certFH *multipart.FileHeader
|
|
if req.Status == "nko" {
|
|
certFH, err = c.FormFile("certificate")
|
|
if err != nil || certFH.Size == 0 || certFH == nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "certificate file required")
|
|
}
|
|
} else {
|
|
certFH = nil
|
|
}
|
|
|
|
result, err := r.repository.Insert(c.Context(), userID, &models.Verification{
|
|
UserID: userID,
|
|
Accepted: false,
|
|
Status: req.Status,
|
|
Comment: "",
|
|
}, innFH, ruleFH, certFH)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
err = r.telegram.SendVerification(result, baseURL, false)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
hloger.Emit(models.InfoVerificationRequested{
|
|
CtxID: result.ID,
|
|
KeyStatus: result.Status,
|
|
})
|
|
|
|
return c.Status(fiber.StatusOK).JSON(result)
|
|
}
|
|
|
|
func (r *VerifyUserController) SetVerificationFile(c *fiber.Ctx) error {
|
|
baseURL := c.BaseURL()
|
|
hloger := log_mw.ExtractLogger(c)
|
|
|
|
userID := c.Locals(models.UserCtxKey).(string)
|
|
if userID == "" {
|
|
return fiber.NewError(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
availableFiles := []string{"inn", "rule", "certificate"}
|
|
var err error
|
|
var fileHeader *multipart.FileHeader
|
|
var result *models.Verification
|
|
|
|
for _, fileName := range availableFiles {
|
|
fileHeader, err = c.FormFile(fileName)
|
|
|
|
if errors.Is(err, fasthttp.ErrMissingFile) {
|
|
continue
|
|
}
|
|
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
result, err = r.repository.UpdateFile(c.Context(), userID, fileName, fileHeader)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
}
|
|
|
|
err = r.telegram.SendVerification(result, baseURL, true)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
hloger.Emit(models.InfoVerificationRequested{
|
|
CtxID: result.ID,
|
|
KeyStatus: result.Status,
|
|
})
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|