TTime/backend/internal/handlers/handlers_report_related.go

117 lines
3.6 KiB
Go
Raw Normal View History

package handlers
import (
2024-03-17 18:05:54 +01:00
"strconv"
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
func (gs *GState) SubmitWeeklyReport(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)
report := new(types.NewWeeklyReport)
if err := c.BodyParser(report); err != nil {
2024-03-19 00:00:18 +01:00
log.Info("Error parsing weekly report")
return c.Status(400).SendString(err.Error())
}
// Make sure all the fields of the report are valid
if report.Week < 1 || report.Week > 52 {
2024-03-19 00:00:18 +01:00
log.Info("Invalid week number")
return c.Status(400).SendString("Invalid week number")
}
if report.DevelopmentTime < 0 || report.MeetingTime < 0 || report.AdminTime < 0 || report.OwnWorkTime < 0 || report.StudyTime < 0 || report.TestingTime < 0 {
2024-03-19 00:00:18 +01:00
log.Info("Invalid time report")
return c.Status(400).SendString("Invalid time report")
}
if err := gs.Db.AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil {
2024-03-19 00:00:18 +01:00
log.Info("Error adding weekly report")
return c.Status(500).SendString(err.Error())
}
2024-03-19 00:00:18 +01:00
log.Info("Weekly report added")
return c.Status(200).SendString("Time report added")
}
2024-03-17 18:05:54 +01:00
// Handler for retrieving weekly report
func (gs *GState) 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)
2024-03-19 00:00:18 +01:00
log.Info("Getting weekly report for: ", username)
2024-03-17 18:05:54 +01:00
// Extract project name and week from query parameters
projectName := c.Query("projectName")
week := c.Query("week")
2024-03-19 00:00:18 +01:00
if projectName == "" || week == "" {
log.Info("Missing project name or week number")
return c.Status(400).SendString("Missing project name or week number")
}
2024-03-17 18:05:54 +01:00
// Convert week to integer
weekInt, err := strconv.Atoi(week)
if err != nil {
2024-03-19 00:00:18 +01:00
log.Info("Invalid week number")
2024-03-17 18:05:54 +01:00
return c.Status(400).SendString("Invalid week number")
}
// Call the database function to get the weekly report
report, err := gs.Db.GetWeeklyReport(username, projectName, weekInt)
if err != nil {
2024-03-19 00:00:18 +01:00
log.Info("Error getting weekly report from db:", err)
2024-03-17 18:05:54 +01:00
return c.Status(500).SendString(err.Error())
}
2024-03-19 00:00:18 +01:00
log.Info("Returning weekly report")
2024-03-17 18:05:54 +01:00
// Return the retrieved weekly report
return c.JSON(report)
}
type ReportId struct {
ReportId int
}
func (gs *GState) 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-19 00:00:18 +01:00
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 := gs.Db.GetUserId(projectManagerUsername)
if err != nil {
2024-03-19 00:00:18 +01:00
log.Info("Failed to get project manager ID")
return c.Status(500).SendString("Failed to get project manager ID")
}
2024-03-19 00:00:18 +01:00
log.Info("Project manager ID: ", projectManagerID)
// Call the database function to sign the weekly report
err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID)
if err != nil {
2024-03-19 00:00:18 +01:00
log.Info("Error signing weekly report:", err)
return c.Status(500).SendString(err.Error())
}
return c.Status(200).SendString("Weekly report signed successfully")
}