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

49 lines
1.3 KiB
Go
Raw Normal View History

package reports
import (
db "ttime/internal/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
// TODO: This information should be extracted from path parameters
type ReportId struct {
ReportId int
}
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)
log.Info("Signing report for: ", projectManagerUsername)
// Extract report ID from the request query parameters
// reportID := c.Query("reportId")
rid := new(ReportId)
if err := c.BodyParser(rid); err != nil {
return err
}
log.Info("Signing report for: ", rid.ReportId)
// Get the project manager's ID
projectManagerID, err := db.GetDb(c).GetUserId(projectManagerUsername)
if err != nil {
log.Info("Failed to get project manager ID")
return c.Status(500).SendString("Failed to get project manager ID")
}
log.Info("Project manager ID: ", projectManagerID)
// Call the database function to sign the weekly report
err = db.GetDb(c).SignWeeklyReport(rid.ReportId, projectManagerID)
if err != nil {
log.Info("Error signing weekly report:", err)
return c.Status(500).SendString(err.Error())
}
return c.Status(200).SendString("Weekly report signed successfully")
}