460 lines
14 KiB
Go
460 lines
14 KiB
Go
package handlers
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/authentication"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/response"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type CreateAdminReq struct {
|
|
FirstName string `json:"first_name" example:"John"`
|
|
LastName string `json:"last_name" example:"Doe"`
|
|
Email string `json:"email" example:"john.doe@example.com"`
|
|
PhoneNumber string `json:"phone_number" example:"1234567890"`
|
|
Password string `json:"password" example:"password123"`
|
|
CompanyID *int64 `json:"company_id,omitempty" example:"1"`
|
|
}
|
|
|
|
// CreateAdmin godoc
|
|
// @Summary Create Admin
|
|
// @Description Create Admin
|
|
// @Tags admin
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param manger body CreateAdminReq true "Create admin"
|
|
// @Success 200 {object} response.APIResponse
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /admin [post]
|
|
func (h *Handler) CreateAdmin(c *fiber.Ctx) error {
|
|
var companyID domain.ValidInt64
|
|
var req CreateAdminReq
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
h.mongoLoggerSvc.Error("failed to parse CreateAdmin request",
|
|
zap.Int64("status_code", fiber.StatusBadRequest),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid request", err, nil)
|
|
}
|
|
|
|
valErrs, ok := h.validator.Validate(c, req)
|
|
if !ok {
|
|
h.mongoLoggerSvc.Error("validation failed for CreateAdmin request",
|
|
zap.Int64("status_code", fiber.StatusBadRequest),
|
|
zap.Any("validation_errors", valErrs),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid request", valErrs, nil)
|
|
}
|
|
|
|
if req.CompanyID == nil {
|
|
companyID = domain.ValidInt64{
|
|
Value: 0,
|
|
Valid: false,
|
|
}
|
|
} else {
|
|
_, err := h.companySvc.GetCompanyByID(c.Context(), *req.CompanyID)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("invalid company ID for CreateAdmin",
|
|
zap.Int64("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("company_id", *req.CompanyID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Company ID is invalid", nil, nil)
|
|
}
|
|
companyID = domain.ValidInt64{
|
|
Value: *req.CompanyID,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
user := domain.CreateUserReq{
|
|
FirstName: req.FirstName,
|
|
LastName: req.LastName,
|
|
Email: req.Email,
|
|
PhoneNumber: req.PhoneNumber,
|
|
Password: req.Password,
|
|
Role: string(domain.RoleAdmin),
|
|
CompanyID: companyID,
|
|
}
|
|
|
|
newUser, err := h.userSvc.CreateUser(c.Context(), user, true)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("failed to create admin user",
|
|
zap.Int64("status_code", fiber.StatusInternalServerError),
|
|
zap.Any("request", req),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to create admin", nil, nil)
|
|
}
|
|
|
|
if req.CompanyID != nil {
|
|
_, err := h.companySvc.UpdateCompany(c.Context(), domain.UpdateCompany{
|
|
ID: *req.CompanyID,
|
|
AdminID: &newUser.ID,
|
|
})
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("failed to update company with new admin",
|
|
zap.Int64("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("company_id", *req.CompanyID),
|
|
zap.Int64("admin_id", newUser.ID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to update company", nil, nil)
|
|
}
|
|
}
|
|
|
|
h.mongoLoggerSvc.Info("admin created successfully",
|
|
zap.Int64("admin_id", newUser.ID),
|
|
zap.String("email", newUser.Email),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Admin created successfully", nil, nil)
|
|
}
|
|
|
|
type AdminRes struct {
|
|
ID int64 `json:"id"`
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
Email string `json:"email"`
|
|
PhoneNumber string `json:"phone_number"`
|
|
Role domain.Role `json:"role"`
|
|
EmailVerified bool `json:"email_verified"`
|
|
PhoneVerified bool `json:"phone_verified"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
LastLogin time.Time `json:"last_login"`
|
|
SuspendedAt time.Time `json:"suspended_at"`
|
|
Suspended bool `json:"suspended"`
|
|
}
|
|
|
|
// GetAllAdmins godoc
|
|
// @Summary Get all Admins
|
|
// @Description Get all Admins
|
|
// @Tags admin
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param page query int false "Page number"
|
|
// @Param page_size query int false "Page size"
|
|
// @Success 200 {object} AdminRes
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /admin [get]
|
|
func (h *Handler) GetAllAdmins(c *fiber.Ctx) error {
|
|
|
|
searchQuery := c.Query("query")
|
|
searchString := domain.ValidString{
|
|
Value: searchQuery,
|
|
Valid: searchQuery != "",
|
|
}
|
|
|
|
createdBeforeQuery := c.Query("created_before")
|
|
var createdBefore domain.ValidTime
|
|
if createdBeforeQuery != "" {
|
|
createdBeforeParsed, err := time.Parse(time.RFC3339, createdBeforeQuery)
|
|
if err != nil {
|
|
h.logger.Error("invalid start_time format", "error", err)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid start_time format", nil, nil)
|
|
}
|
|
createdBefore = domain.ValidTime{
|
|
Value: createdBeforeParsed,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
createdAfterQuery := c.Query("created_after")
|
|
var createdAfter domain.ValidTime
|
|
if createdAfterQuery != "" {
|
|
createdAfterParsed, err := time.Parse(time.RFC3339, createdAfterQuery)
|
|
if err != nil {
|
|
h.logger.Error("invalid start_time format", "error", err)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid start_time format", nil, nil)
|
|
}
|
|
createdAfter = domain.ValidTime{
|
|
Value: createdAfterParsed,
|
|
Valid: true,
|
|
}
|
|
}
|
|
filter := domain.UserFilter{
|
|
Role: string(domain.RoleAdmin),
|
|
CompanyID: domain.ValidInt64{
|
|
Value: int64(c.QueryInt("company_id")),
|
|
Valid: false,
|
|
},
|
|
Page: domain.ValidInt{
|
|
Value: c.QueryInt("page", 1) - 1,
|
|
Valid: true,
|
|
},
|
|
PageSize: domain.ValidInt{
|
|
Value: c.QueryInt("page_size", 10),
|
|
Valid: true,
|
|
},
|
|
Query: searchString,
|
|
CreatedBefore: createdBefore,
|
|
CreatedAfter: createdAfter,
|
|
}
|
|
|
|
valErrs, ok := h.validator.Validate(c, filter)
|
|
if !ok {
|
|
h.mongoLoggerSvc.Error("invalid filter values in GetAllAdmins request",
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.Any("validation_errors", valErrs),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid request", valErrs, nil)
|
|
}
|
|
|
|
admins, total, err := h.userSvc.GetAllUsers(c.Context(), filter)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("failed to get admins from user service",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Any("filter", filter),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to get Admins", nil, nil)
|
|
}
|
|
|
|
result := make([]AdminRes, len(admins))
|
|
for index, admin := range admins {
|
|
lastLogin, err := h.authSvc.GetLastLogin(c.Context(), admin.ID)
|
|
if err != nil {
|
|
if err == authentication.ErrRefreshTokenNotFound {
|
|
lastLogin = &admin.CreatedAt
|
|
} else {
|
|
h.mongoLoggerSvc.Error("failed to get last login for admin",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("admin_id", admin.ID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user last login")
|
|
}
|
|
}
|
|
|
|
result[index] = AdminRes{
|
|
ID: admin.ID,
|
|
FirstName: admin.FirstName,
|
|
LastName: admin.LastName,
|
|
Email: admin.Email,
|
|
PhoneNumber: admin.PhoneNumber,
|
|
Role: admin.Role,
|
|
EmailVerified: admin.EmailVerified,
|
|
PhoneVerified: admin.PhoneVerified,
|
|
CreatedAt: admin.CreatedAt,
|
|
UpdatedAt: admin.UpdatedAt,
|
|
SuspendedAt: admin.SuspendedAt,
|
|
Suspended: admin.Suspended,
|
|
LastLogin: *lastLogin,
|
|
}
|
|
}
|
|
|
|
h.mongoLoggerSvc.Info("admins retrieved successfully",
|
|
zap.Int("status_code", fiber.StatusOK),
|
|
zap.Int("count", len(result)),
|
|
zap.Int("page", filter.Page.Value+1),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
|
|
return response.WritePaginatedJSON(c, fiber.StatusOK, "Admins retrieved successfully", result, nil, filter.Page.Value, int(total))
|
|
}
|
|
|
|
// GetAdminByID godoc
|
|
// @Summary Get admin by id
|
|
// @Description Get a single admin by id
|
|
// @Tags admin
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "User ID"
|
|
// @Success 200 {object} AdminRes
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /admin/{id} [get]
|
|
func (h *Handler) GetAdminByID(c *fiber.Ctx) error {
|
|
userIDstr := c.Params("id")
|
|
userID, err := strconv.ParseInt(userIDstr, 10, 64)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("invalid admin ID param",
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.String("param", userIDstr),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid admin ID", nil, nil)
|
|
}
|
|
|
|
user, err := h.userSvc.GetUserByID(c.Context(), userID)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("failed to fetch admin by ID",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("admin_id", userID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to get admin", nil, nil)
|
|
}
|
|
|
|
lastLogin, err := h.authSvc.GetLastLogin(c.Context(), user.ID)
|
|
if err != nil && err != authentication.ErrRefreshTokenNotFound {
|
|
h.mongoLoggerSvc.Error("failed to get admin last login",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("admin_id", user.ID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve user last login")
|
|
}
|
|
if err == authentication.ErrRefreshTokenNotFound {
|
|
lastLogin = &user.CreatedAt
|
|
}
|
|
|
|
res := AdminRes{
|
|
ID: user.ID,
|
|
FirstName: user.FirstName,
|
|
LastName: user.LastName,
|
|
Email: user.Email,
|
|
PhoneNumber: user.PhoneNumber,
|
|
Role: user.Role,
|
|
EmailVerified: user.EmailVerified,
|
|
PhoneVerified: user.PhoneVerified,
|
|
CreatedAt: user.CreatedAt,
|
|
UpdatedAt: user.UpdatedAt,
|
|
SuspendedAt: user.SuspendedAt,
|
|
Suspended: user.Suspended,
|
|
LastLogin: *lastLogin,
|
|
}
|
|
|
|
h.mongoLoggerSvc.Info("admin retrieved successfully",
|
|
zap.Int("status_code", fiber.StatusOK),
|
|
zap.Int64("admin_id", user.ID),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Admin retrieved successfully", res, nil)
|
|
}
|
|
|
|
type updateAdminReq struct {
|
|
FirstName string `json:"first_name" example:"John"`
|
|
LastName string `json:"last_name" example:"Doe"`
|
|
Suspended bool `json:"suspended" example:"false"`
|
|
CompanyID *int64 `json:"company_id,omitempty" example:"1"`
|
|
}
|
|
|
|
// UpdateAdmin godoc
|
|
// @Summary Update Admin
|
|
// @Description Update Admin
|
|
// @Tags admin
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param admin body updateAdminReq true "Update Admin"
|
|
// @Success 200 {object} response.APIResponse
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 401 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /admin/{id} [put]
|
|
func (h *Handler) UpdateAdmin(c *fiber.Ctx) error {
|
|
var req updateAdminReq
|
|
if err := c.BodyParser(&req); err != nil {
|
|
h.mongoLoggerSvc.Error("UpdateAdmin failed - invalid request body",
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid request", nil, nil)
|
|
}
|
|
|
|
valErrs, ok := h.validator.Validate(c, req)
|
|
if !ok {
|
|
h.mongoLoggerSvc.Error("UpdateAdmin failed - validation errors",
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.Any("validation_errors", valErrs),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid request", valErrs, nil)
|
|
}
|
|
|
|
AdminIDStr := c.Params("id")
|
|
AdminID, err := strconv.ParseInt(AdminIDStr, 10, 64)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("UpdateAdmin failed - invalid Admin ID param",
|
|
zap.Int("status_code", fiber.StatusBadRequest),
|
|
zap.String("admin_id_param", AdminIDStr),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid Admin ID", nil, nil)
|
|
}
|
|
|
|
var companyID domain.ValidInt64
|
|
if req.CompanyID != nil {
|
|
companyID = domain.ValidInt64{
|
|
Value: *req.CompanyID,
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
err = h.userSvc.UpdateUser(c.Context(), domain.UpdateUserReq{
|
|
UserId: AdminID,
|
|
FirstName: domain.ValidString{
|
|
Value: req.FirstName,
|
|
Valid: req.FirstName != "",
|
|
},
|
|
LastName: domain.ValidString{
|
|
Value: req.LastName,
|
|
Valid: req.LastName != "",
|
|
},
|
|
Suspended: domain.ValidBool{
|
|
Value: req.Suspended,
|
|
Valid: true,
|
|
},
|
|
CompanyID: companyID,
|
|
})
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("UpdateAdmin failed - user service error",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("admin_id", AdminID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to update admin", nil, nil)
|
|
}
|
|
|
|
if req.CompanyID != nil {
|
|
_, err := h.companySvc.UpdateCompany(c.Context(), domain.UpdateCompany{
|
|
ID: *req.CompanyID,
|
|
AdminID: &AdminID,
|
|
})
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("UpdateAdmin failed to update company",
|
|
zap.Int("status_code", fiber.StatusInternalServerError),
|
|
zap.Int64("admin_id", AdminID),
|
|
zap.Error(err),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to update company", nil, nil)
|
|
}
|
|
}
|
|
|
|
h.mongoLoggerSvc.Info("UpdateAdmin succeeded",
|
|
zap.Int("status_code", fiber.StatusOK),
|
|
zap.Int64("admin_id", AdminID),
|
|
zap.Time("timestamp", time.Now()),
|
|
)
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Managers updated successfully", nil, nil)
|
|
}
|