TTime/backend/internal/handlers/reports/GetWeeklyReport.go

48 lines
1.3 KiB
Go
Raw Normal View History

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)
log.Info("Getting weekly report for: ", username)
// Extract project name and week from query parameters
projectName := c.Query("projectName")
week := c.Query("week")
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")
}
// Call the database function to get the weekly report
report, err := db.GetDb(c).GetWeeklyReport(username, 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)
}