package reports import ( db "ttime/internal/database" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/golang-jwt/jwt/v5" ) func GetStatistics(c *fiber.Ctx) error { // Extract the necessary parameters from the token user := c.Locals("user").(*jwt.Token) claims := user.Claims.(jwt.MapClaims) username := claims["name"].(string) // Extract project name from query parameters projectName := c.Query("projectName") userNameParam := c.Query("userName") log.Info(username, " trying to get statistics for project: ", projectName) if projectName == "" { log.Info("Missing project name") return c.Status(400).SendString("Missing project name") } // Check if the user is a project manager pm, err := db.GetDb(c).IsProjectManager(username, projectName) if err != nil { log.Info("Error checking if user is project manager:", err) return c.Status(500).SendString(err.Error()) } // Bail if the user is not a PM or checking its own statistics if !pm && userNameParam != "" && userNameParam != username { log.Info("Unauthorized access for user: ", username, "trying to access project: ", projectName, "statistics for user: ", userNameParam) return c.Status(403).SendString("Unauthorized access") } // Retrieve statistics for the project from the database statistics, err := db.GetDb(c).ReportStatistics(username, projectName) if err != nil { log.Error("Error getting statistics for project:", projectName, ":", err) return c.Status(500).SendString(err.Error()) } log.Info("Returning statistics") // Return the retrieved statistics return c.JSON(statistics) }