2023-06-12 14:19:10 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"mime/multipart"
|
2023-07-03 11:40:20 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/internal/client"
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/internal/models"
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/verification/internal/repository"
|
2023-06-12 14:19:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type VerificationController struct {
|
|
|
|
repository *repository.VerificationRepository
|
|
|
|
telegram *client.Telegram
|
|
|
|
customer *client.Customer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVerificationController(rep *repository.VerificationRepository, telegram *client.Telegram, customer *client.Customer) *VerificationController {
|
|
|
|
return &VerificationController{repository: rep, telegram: telegram, customer: customer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerificationController) GetRoutes() []Route {
|
|
|
|
return []Route{
|
|
|
|
{"GET", "/verification/:userID", "GetVerification", r.GetVerification},
|
|
|
|
{"POST", "/verification", "CreateVerification", r.CreateVerification},
|
|
|
|
{"PATCH", "/verification", "SetVerificationStatus", r.SetVerificationStatus},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerificationController) GetVerification(c *fiber.Ctx) error {
|
|
|
|
userID := c.Params("userID")
|
|
|
|
|
|
|
|
if userID == "" {
|
|
|
|
userID = c.Locals("userID").(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 {
|
2023-07-26 18:43:55 +00:00
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
2023-06-12 14:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerificationController) CreateVerification(c *fiber.Ctx) error {
|
|
|
|
var req models.ReqCreateVerification
|
|
|
|
|
|
|
|
userID := c.Params("userID")
|
|
|
|
|
|
|
|
if userID == "" {
|
|
|
|
userID = c.Locals("userID").(string)
|
|
|
|
if userID == "" {
|
|
|
|
return fiber.NewError(fiber.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.BodyParser(&req)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
errValidate := 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// EGRULE FILE
|
|
|
|
egruleFH, err := c.FormFile("egrule")
|
|
|
|
|
|
|
|
if err != nil || egruleFH.Size == 0 || egruleFH == nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "egrule 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, egruleFH, certFH)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.telegram.SendVerification(result, false)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerificationController) SetVerificationStatus(c *fiber.Ctx) error {
|
|
|
|
var req models.ReqSetVerification
|
|
|
|
|
|
|
|
userID := c.Params("userID")
|
|
|
|
|
|
|
|
if userID == "" {
|
|
|
|
userID = c.Locals("userID").(string)
|
|
|
|
if userID == "" {
|
|
|
|
return fiber.NewError(fiber.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.BodyParser(&req)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
errValidate := validateStruct(&req)
|
|
|
|
if errValidate != nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
|
|
|
}
|
|
|
|
|
2023-07-26 18:48:36 +00:00
|
|
|
_, err = r.repository.Update(c.Context(), &models.Verification{
|
2023-06-12 14:19:10 +00:00
|
|
|
ID: req.ID,
|
|
|
|
UserID: userID,
|
|
|
|
Accepted: req.Accepted,
|
|
|
|
Status: req.Status,
|
|
|
|
Comment: req.Comment,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|