2024-05-26 15:32:07 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-05-27 11:53:34 +00:00
|
|
|
"github.com/themakers/hlog"
|
2024-05-26 15:32:07 +00:00
|
|
|
"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"
|
|
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/customer_clients"
|
2024-05-27 11:53:34 +00:00
|
|
|
"strings"
|
2024-05-26 15:32:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type VerifyAdminControllerDeps struct {
|
|
|
|
Repository *repository.VerificationRepository
|
|
|
|
Customer *customer_clients.CustomersClient
|
|
|
|
}
|
|
|
|
|
|
|
|
type VerifyAdminController struct {
|
|
|
|
repository *repository.VerificationRepository
|
|
|
|
customer *customer_clients.CustomersClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVerificationAdminController(deps VerifyAdminControllerDeps) *VerifyAdminController {
|
|
|
|
return &VerifyAdminController{
|
|
|
|
repository: deps.Repository,
|
|
|
|
customer: deps.Customer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerifyAdminController) SetVerificationStatus(c *fiber.Ctx) error {
|
|
|
|
var req models.ReqSetVerification
|
2024-05-27 11:53:34 +00:00
|
|
|
hloger := c.Locals(models.HlogCtxKey).(hlog.Logger)
|
2024-05-26 15:32:07 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := r.repository.Update(c.Context(), &models.Verification{
|
|
|
|
ID: req.ID,
|
|
|
|
Accepted: req.Accepted,
|
|
|
|
Status: req.Status,
|
|
|
|
Comment: req.Comment,
|
|
|
|
TaxNumber: req.TaxNumber,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Accepted {
|
|
|
|
_, err := r.customer.SetVerifyAccount(c.Context(), updated.UserID, req.Status)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
2024-05-27 11:53:34 +00:00
|
|
|
|
|
|
|
hloger.Emit(models.InfoVerificationAccepted{
|
|
|
|
CtxUserIP: c.IP(),
|
|
|
|
CtxUserPort: c.Port(),
|
|
|
|
KeyDomain: strings.Join(c.Subdomains(), "/"),
|
|
|
|
KeyPath: c.Path(),
|
|
|
|
CtxID: updated.ID,
|
|
|
|
KeyStatus: updated.Status,
|
|
|
|
CtxTaxNumber: updated.TaxNumber,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !req.Accepted {
|
|
|
|
hloger.Emit(models.InfoVerificationDeclined{
|
|
|
|
CtxUserIP: c.IP(),
|
|
|
|
CtxUserPort: c.Port(),
|
|
|
|
KeyDomain: strings.Join(c.Subdomains(), "/"),
|
|
|
|
KeyPath: c.Path(),
|
|
|
|
CtxID: updated.ID,
|
|
|
|
KeyStatus: updated.Status,
|
|
|
|
CtxComment: updated.Comment,
|
|
|
|
})
|
2024-05-26 15:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *VerifyAdminController) GetVerification(c *fiber.Ctx) error {
|
|
|
|
userID := c.Params("userID")
|
|
|
|
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)
|
|
|
|
}
|