314 lines
11 KiB
Go
314 lines
11 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/response"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (h *Handler) CreateReferralCode(c *fiber.Ctx) error {
|
|
companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
if !companyID.Valid {
|
|
h.BadRequestLogger().Error("invalid company id")
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
}
|
|
userID, ok := c.Locals("user_id").(int64)
|
|
if !ok || userID == 0 {
|
|
h.mongoLoggerSvc.Info("Invalid user ID in context",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("userID", userID),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Invalid user identification")
|
|
}
|
|
|
|
if err := h.referralSvc.CreateReferral(c.Context(), userID, companyID.Value); err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to create referral",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create referral")
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Referral created successfully", nil, nil)
|
|
}
|
|
|
|
func (h *Handler) CreateReferralSettings(c *fiber.Ctx) error {
|
|
|
|
var req domain.ReferralSettingsReq
|
|
if err := c.BodyParser(&req); err != nil {
|
|
h.mongoLoggerSvc.Info("Failed to parse settings",
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if valErrs, ok := h.validator.Validate(c, req); !ok {
|
|
var errMsg string
|
|
for field, msg := range valErrs {
|
|
errMsg += fmt.Sprintf("%s: %s; ", field, msg)
|
|
}
|
|
h.mongoLoggerSvc.Info("Failed to validate settings",
|
|
zap.String("errMsg", errMsg),
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusBadRequest, errMsg)
|
|
}
|
|
|
|
settings, err := h.referralSvc.GetReferralSettings(c.Context())
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to fetch previous referral setting",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create referral")
|
|
}
|
|
|
|
// only allow one referral setting for now
|
|
// for future it can be multiple and be able to choose from them
|
|
if settings != nil {
|
|
h.mongoLoggerSvc.Error("referral setting already exists",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "referral setting already exists")
|
|
}
|
|
|
|
if err := h.referralSvc.CreateReferralSettings(c.Context(), req); err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to create referral setting",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create referral")
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Referral created successfully", nil, nil)
|
|
}
|
|
|
|
// func (h *Handler) GetReferralCode(c *fiber.Ctx) error {
|
|
// companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
// if !companyID.Valid {
|
|
// h.BadRequestLogger().Error("invalid company id")
|
|
// return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
// }
|
|
// userID, ok := c.Locals("user_id").(int64)
|
|
// if !ok || userID == 0 {
|
|
// h.mongoLoggerSvc.Error("Invalid user ID in context",
|
|
// zap.Int64("userID", userID),
|
|
// zap.Int("status_code", fiber.StatusInternalServerError),
|
|
// zap.Time("timestamp", time.Now()),
|
|
// )
|
|
// return fiber.NewError(fiber.StatusInternalServerError, "Invalid user id")
|
|
// }
|
|
|
|
// user, err := h.userSvc.GetUserByID(c.Context(), userID)
|
|
// if err != nil {
|
|
// h.mongoLoggerSvc.Error("Failed to get user",
|
|
// zap.Int64("userID", userID),
|
|
// zap.Int("status_code", fiber.StatusInternalServerError),
|
|
// zap.Error(err),
|
|
// zap.Time("timestamp", time.Now()),
|
|
// )
|
|
// return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user")
|
|
// }
|
|
|
|
// if !user.CompanyID.Valid || user.CompanyID.Value != companyID.Value {
|
|
// h.mongoLoggerSvc.Warn("User attempt to login to different company",
|
|
// zap.Int64("userID", userID),
|
|
// zap.Int("status_code", fiber.StatusInternalServerError),
|
|
// zap.Error(err),
|
|
// zap.Time("timestamp", time.Now()),
|
|
// )
|
|
// return fiber.NewError(fiber.StatusBadRequest, "Failed to retrieve user")
|
|
// }
|
|
|
|
// // referrals, err := h.referralSvc.GetReferralStats(c.Context(), user.ID)
|
|
|
|
// if err != nil {
|
|
// h.mongoLoggerSvc.Error("Failed to get user referrals",
|
|
// zap.Int64("userID", userID),
|
|
// zap.Int("status_code", fiber.StatusInternalServerError),
|
|
// zap.Error(err),
|
|
// zap.Time("timestamp", time.Now()),
|
|
// )
|
|
// return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user referral codes")
|
|
// }
|
|
|
|
// }
|
|
|
|
// GetReferralStats godoc
|
|
// @Summary Get referral statistics
|
|
// @Description Retrieves referral statistics for the authenticated user
|
|
// @Tags referral
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} domain.ReferralStats
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Security Bearer
|
|
// @Router /api/v1/tenant/{tenant_slug}/referral/stats [get]
|
|
func (h *Handler) GetReferralStats(c *fiber.Ctx) error {
|
|
companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
if !companyID.Valid {
|
|
h.BadRequestLogger().Error("invalid company id")
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
}
|
|
userID, ok := c.Locals("user_id").(int64)
|
|
if !ok || userID == 0 {
|
|
h.mongoLoggerSvc.Error("Invalid user ID in context",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Invalid user id")
|
|
}
|
|
|
|
user, err := h.userSvc.GetUserByID(c.Context(), userID)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to get user",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user")
|
|
}
|
|
|
|
if !user.CompanyID.Valid || user.CompanyID.Value != companyID.Value {
|
|
h.mongoLoggerSvc.Warn("User attempt to login to different company",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusBadRequest, "Failed to retrieve user")
|
|
}
|
|
|
|
stats, err := h.referralSvc.GetReferralStats(c.Context(), user.ID, companyID.Value)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to get referral stats",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve referral stats")
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Referral stats retrieved successfully", stats, nil)
|
|
}
|
|
|
|
// UpdateReferralSettings godoc
|
|
// @Summary Update referral settings
|
|
// @Description Updates referral settings (admin only)
|
|
// @Tags referral
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param settings body domain.ReferralSettings true "Referral settings"
|
|
// @Success 200 {object} response.APIResponse
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 403 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Security Bearer
|
|
// @Router /api/v1/referral/settings [put]
|
|
func (h *Handler) UpdateReferralSettings(c *fiber.Ctx) error {
|
|
userID, ok := c.Locals("user_id").(int64)
|
|
if !ok || userID == 0 {
|
|
h.logger.Error("Invalid user ID in context")
|
|
h.mongoLoggerSvc.Error("Failed to delete user",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Invalid user id")
|
|
}
|
|
|
|
user, err := h.userSvc.GetUserByID(c.Context(), userID)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to get user",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
if user.Role != domain.RoleAdmin {
|
|
h.mongoLoggerSvc.Error("Access Forbidden",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusForbidden),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusForbidden, "Admin access required")
|
|
}
|
|
|
|
var settings domain.ReferralSettings
|
|
if err := c.BodyParser(&settings); err != nil {
|
|
h.mongoLoggerSvc.Info("Failed to parse settings",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
settings.UpdatedBy = user.PhoneNumber
|
|
if err := h.referralSvc.UpdateReferralSettings(c.Context(), &settings); err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to update referral settings",
|
|
zap.Int64("userID", userID),
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Referral settings updated successfully", nil, nil)
|
|
}
|
|
|
|
// GetReferralSettings godoc
|
|
// @Summary Get referral settings
|
|
// @Description Retrieves current referral settings (admin only)
|
|
// @Tags referral
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} domain.ReferralSettings
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 403 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Security Bearer
|
|
// @Router /api/v1/referral/settings [get]
|
|
func (h *Handler) GetReferralSettings(c *fiber.Ctx) error {
|
|
// userID, ok := c.Locals("user_id").(int64)
|
|
// if !ok || userID == 0 {
|
|
// h.logger.Error("Invalid user ID in context")
|
|
// return fiber.NewError(fiber.StatusUnauthorized, "Invalid user identification")
|
|
// }
|
|
settings, err := h.referralSvc.GetReferralSettings(c.Context())
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("Failed to get referral settings",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Referral settings retrieved successfully", settings, nil)
|
|
}
|