51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
|
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")
|
||
|
|
||
|
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")
|
||
|
}
|
||
|
|
||
|
// If the user is not a project manager, they can't view statistics
|
||
|
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 {
|
||
|
log.Info("Unauthorized access")
|
||
|
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)
|
||
|
|
||
|
}
|