package reports import ( "strconv" db "ttime/internal/database" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/golang-jwt/jwt/v5" ) // Handler for retrieving weekly report func GetWeeklyReport(c *fiber.Ctx) error { // Extract the necessary parameters from the request user := c.Locals("user").(*jwt.Token) claims := user.Claims.(jwt.MapClaims) username := claims["name"].(string) // Extract project name and week from query parameters projectName := c.Query("projectName") week := c.Query("week") target_user := c.Query("targetUser") // The user whose report is being requested // If the target user is not empty, use it as the username if target_user == "" { target_user = username } log.Info(username, " trying to get weekly report for: ", target_user) if projectName == "" || week == "" { log.Info("Missing project name or week number") return c.Status(400).SendString("Missing project name or week number") } // Convert week to integer weekInt, err := strconv.Atoi(week) if err != nil { log.Info("Invalid week number") return c.Status(400).SendString("Invalid week number") } // If the token user is not an admin, check if the target user is the same as the token user 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()) } if !(pm || target_user == username) { log.Info("Unauthorized access") return c.Status(403).SendString("Unauthorized access") } // Call the database function to get the weekly report report, err := db.GetDb(c).GetWeeklyReport(target_user, projectName, weekInt) if err != nil { log.Info("Error getting weekly report from db:", err) return c.Status(500).SendString(err.Error()) } log.Info("Returning weekly report") // Return the retrieved weekly report return c.JSON(report) }