RezinaLMS-BackEnd/internal/web_server/handlers/team_password_reset_handler.go
2026-10-19 17:59:34 +03:00

126 lines
4.9 KiB
Go

package handlers
import (
"Rezina-Backend/internal/domain"
"errors"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
)
// VerifyTeamPasswordReset godoc
// @Summary Verify team password reset link
// @Description Checks whether email+otp from a reset link are still valid without consuming the OTP. Public.
// @Tags team
// @Produce json
// @Param email query string true "Team member email"
// @Param otp query string true "Reset OTP from the email link"
// @Success 200 {object} domain.Response
// @Router /api/v1/team/verifyResetCode [get]
func (h *Handler) VerifyTeamPasswordReset(c *fiber.Ctx) error {
email := c.Query("email")
otp := c.Query("otp")
res := h.teamSvc.VerifyPasswordReset(c.Context(), email, otp)
return c.JSON(domain.Response{
Message: "Password reset link checked",
Success: true,
Data: res,
})
}
// SendTeamPasswordReset godoc
// @Summary Send team password reset link
// @Description Sends a password-reset email with a link to the team member (admin panel). Public.
// @Tags team
// @Accept json
// @Produce json
// @Param body body domain.TeamSendPasswordResetReq true "Email"
// @Success 200 {object} domain.Response
// @Failure 400 {object} domain.ErrorResponse
// @Failure 404 {object} domain.ErrorResponse
// @Failure 500 {object} domain.ErrorResponse
// @Router /api/v1/team/sendResetCode [post]
func (h *Handler) SendTeamPasswordReset(c *fiber.Ctx) error {
var req domain.TeamSendPasswordResetReq
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Failed to send password reset",
Error: "Invalid request body: " + err.Error(),
})
}
if valErrs, ok := h.validator.Validate(c, req); !ok {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Failed to send password reset",
Error: firstValidationError(valErrs),
})
}
if err := h.teamSvc.SendPasswordReset(c.Context(), req.Email); err != nil {
return h.teamPasswordResetError(c, err, "Failed to send password reset")
}
return c.JSON(domain.Response{
Message: "Password reset link sent successfully",
Success: true,
})
}
// ResetTeamPassword godoc
// @Summary Reset team member password
// @Description Completes team password reset using email + OTP from the reset link. Public.
// @Tags team
// @Accept json
// @Produce json
// @Param body body domain.TeamResetPasswordReq true "Reset payload"
// @Success 200 {object} domain.Response
// @Failure 400 {object} domain.ErrorResponse
// @Failure 404 {object} domain.ErrorResponse
// @Failure 500 {object} domain.ErrorResponse
// @Router /api/v1/team/resetPassword [post]
func (h *Handler) ResetTeamPassword(c *fiber.Ctx) error {
var req domain.TeamResetPasswordReq
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Failed to reset password",
Error: "Invalid request body: " + err.Error(),
})
}
if valErrs, ok := h.validator.Validate(c, req); !ok {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Failed to reset password",
Error: firstValidationError(valErrs),
})
}
if err := h.teamSvc.ResetPasswordWithOtp(c.Context(), req); err != nil {
return h.teamPasswordResetError(c, err, "Failed to reset password")
}
return c.JSON(domain.Response{
Message: "Password reset successfully",
Success: true,
})
}
func (h *Handler) teamPasswordResetError(c *fiber.Ctx, err error, message string) error {
switch {
case errors.Is(err, domain.ErrTeamMemberNotFound):
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: message, Error: "Team member not found"})
case errors.Is(err, domain.ErrInvalidTeamMemberStatus):
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: message, Error: "Team member account is not active"})
case errors.Is(err, domain.ErrTeamMemberPendingInvitation):
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: message, Error: "Accept your invitation before resetting password"})
case errors.Is(err, domain.ErrTeamPasswordResetBaseURLNotConfigured):
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: message, Error: "TEAM_PASSWORD_RESET_BASE_URL is not configured"})
case errors.Is(err, domain.ErrOtpNotFound), errors.Is(err, domain.ErrInvalidOtp):
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: message, Error: "Invalid or missing reset code"})
case errors.Is(err, domain.ErrOtpExpired):
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: message, Error: "Reset code has expired"})
case errors.Is(err, domain.ErrOtpAlreadyUsed):
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{Message: message, Error: "Reset code has already been used"})
default:
h.mongoLoggerSvc.Error(message, zap.Error(err))
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{Message: message, Error: err.Error()})
}
}