2024-03-29 14:50:56 +01:00
|
|
|
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")
|
2024-04-03 15:53:36 +02:00
|
|
|
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)
|
2024-03-29 14:50:56 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-04-03 15:53:36 +02:00
|
|
|
// 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 == false && target_user != username {
|
|
|
|
log.Info("Unauthorized access")
|
|
|
|
return c.Status(403).SendString("Unauthorized access")
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:50:56 +01:00
|
|
|
// Call the database function to get the weekly report
|
2024-04-03 15:53:36 +02:00
|
|
|
report, err := db.GetDb(c).GetWeeklyReport(target_user, projectName, weekInt)
|
2024-03-29 14:50:56 +01:00
|
|
|
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)
|
|
|
|
}
|