2024-03-29 14:50:56 +01:00
|
|
|
package reports
|
|
|
|
|
|
|
|
import (
|
2024-03-29 15:33:20 +01:00
|
|
|
"strconv"
|
2024-03-29 14:50:56 +01:00
|
|
|
db "ttime/internal/database"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/log"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SignReport(c *fiber.Ctx) error {
|
|
|
|
// Extract the necessary parameters from the token
|
|
|
|
user := c.Locals("user").(*jwt.Token)
|
|
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
|
|
projectManagerUsername := claims["name"].(string)
|
|
|
|
|
2024-03-29 15:33:20 +01:00
|
|
|
// Extract report ID from the path
|
|
|
|
reportId, err := strconv.Atoi(c.Params("reportId"))
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Invalid report ID")
|
|
|
|
return c.Status(400).SendString("Invalid report ID")
|
2024-03-29 14:50:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the project manager's ID
|
|
|
|
projectManagerID, err := db.GetDb(c).GetUserId(projectManagerUsername)
|
|
|
|
if err != nil {
|
2024-03-29 15:33:20 +01:00
|
|
|
log.Info("Failed to get project manager ID for user: ", projectManagerUsername)
|
2024-03-29 14:50:56 +01:00
|
|
|
return c.Status(500).SendString("Failed to get project manager ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the database function to sign the weekly report
|
2024-03-29 15:33:20 +01:00
|
|
|
err = db.GetDb(c).SignWeeklyReport(reportId, projectManagerID)
|
2024-03-29 14:50:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Info("Error signing weekly report:", err)
|
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
2024-03-29 15:33:20 +01:00
|
|
|
log.Info("Project manager ID: ", projectManagerID, " signed report ID: ", reportId)
|
2024-03-29 14:50:56 +01:00
|
|
|
return c.Status(200).SendString("Weekly report signed successfully")
|
|
|
|
}
|