79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package admin
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"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"
|
|
)
|
|
|
|
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
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|