2024-03-29 14:50:56 +01:00
|
|
|
package reports
|
|
|
|
|
|
|
|
import (
|
|
|
|
db "ttime/internal/database"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/log"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
)
|
|
|
|
|
2024-04-03 17:31:39 +02:00
|
|
|
// GetAllWeeklyReports retrieves all weekly reports for a user in a specific project
|
|
|
|
func GetAllWeeklyReports(c *fiber.Ctx) error {
|
2024-03-29 14:50:56 +01:00
|
|
|
// Extract the necessary parameters from the token
|
|
|
|
user := c.Locals("user").(*jwt.Token)
|
|
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
|
|
username := claims["name"].(string)
|
|
|
|
|
2024-04-03 17:31:39 +02:00
|
|
|
// Extract project name and week from query parameters
|
2024-03-29 14:50:56 +01:00
|
|
|
projectName := c.Params("projectName")
|
2024-04-03 17:31:39 +02:00
|
|
|
target_user := c.Query("targetUser") // The user whose reports are being requested
|
2024-03-29 14:50:56 +01:00
|
|
|
|
2024-04-03 17:31:39 +02:00
|
|
|
// If the target user is not empty, use it as the username
|
|
|
|
if target_user == "" {
|
|
|
|
target_user = username
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info(username, " trying to get all weekly reports for: ", target_user)
|
|
|
|
|
|
|
|
if projectName == "" {
|
|
|
|
log.Info("Missing project name")
|
|
|
|
return c.Status(400).SendString("Missing project name")
|
|
|
|
}
|
|
|
|
|
2024-04-03 17:44:50 +02:00
|
|
|
// If the user is not a project manager, they can only view their own reports
|
2024-04-03 17:31:39 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-04-04 23:23:11 +02:00
|
|
|
if !(pm || target_user == username) {
|
2024-04-03 17:31:39 +02:00
|
|
|
log.Info("Unauthorized access")
|
|
|
|
return c.Status(403).SendString("Unauthorized access")
|
|
|
|
}
|
2024-03-29 14:50:56 +01:00
|
|
|
|
|
|
|
// Retrieve weekly reports for the user in the project from the database
|
2024-04-03 17:44:50 +02:00
|
|
|
reports, err := db.GetDb(c).GetAllWeeklyReports(target_user, projectName)
|
2024-03-29 14:50:56 +01:00
|
|
|
if err != nil {
|
2024-04-03 17:44:50 +02:00
|
|
|
log.Error("Error getting weekly reports for user:", target_user, "in project:", projectName, ":", err)
|
2024-03-29 14:50:56 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-03 17:31:39 +02:00
|
|
|
log.Info("Returning weekly report")
|
|
|
|
// Return the retrieved weekly report
|
2024-03-29 14:50:56 +01:00
|
|
|
return c.JSON(reports)
|
|
|
|
}
|