2024-03-17 16:55:40 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-03-17 18:05:54 +01:00
|
|
|
"strconv"
|
2024-03-17 16:55:40 +01:00
|
|
|
"ttime/internal/types"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-03-18 23:38:50 +01:00
|
|
|
"github.com/gofiber/fiber/v2/log"
|
2024-03-17 16:55:40 +01:00
|
|
|
"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")
|
2024-03-17 16:55:40 +01:00
|
|
|
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")
|
2024-03-17 16:55:40 +01:00
|
|
|
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")
|
2024-03-17 16:55:40 +01:00
|
|
|
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-20 22:43:03 +01:00
|
|
|
log.Info("Error adding weekly report to db:", err)
|
2024-03-17 16:55:40 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Weekly report added")
|
2024-03-17 16:55:40 +01:00
|
|
|
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)
|
|
|
|
}
|
2024-03-17 23:31:52 +01:00
|
|
|
|
2024-03-18 16:01:00 +01:00
|
|
|
type ReportId struct {
|
|
|
|
ReportId int
|
|
|
|
}
|
|
|
|
|
2024-03-17 23:31:52 +01:00
|
|
|
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)
|
2024-03-18 16:01:00 +01:00
|
|
|
projectManagerUsername := claims["name"].(string)
|
2024-03-17 23:31:52 +01:00
|
|
|
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Signing report for: ", projectManagerUsername)
|
|
|
|
|
2024-03-18 16:01:00 +01:00
|
|
|
// Extract report ID from the request query parameters
|
|
|
|
// reportID := c.Query("reportId")
|
|
|
|
rid := new(ReportId)
|
|
|
|
if err := c.BodyParser(rid); err != nil {
|
|
|
|
return err
|
2024-03-17 23:31:52 +01:00
|
|
|
}
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Signing report for: ", rid.ReportId)
|
2024-03-18 16:01:00 +01:00
|
|
|
|
|
|
|
// Get the project manager's ID
|
|
|
|
projectManagerID, err := gs.Db.GetUserId(projectManagerUsername)
|
2024-03-17 23:31:52 +01:00
|
|
|
if err != nil {
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Failed to get project manager ID")
|
2024-03-17 23:31:52 +01:00
|
|
|
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)
|
2024-03-17 23:31:52 +01:00
|
|
|
|
|
|
|
// Call the database function to sign the weekly report
|
2024-03-18 16:01:00 +01:00
|
|
|
err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID)
|
2024-03-17 23:31:52 +01:00
|
|
|
if err != nil {
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Error signing weekly report:", err)
|
2024-03-18 16:01:00 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
2024-03-17 23:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(200).SendString("Weekly report signed successfully")
|
|
|
|
}
|
2024-03-19 19:04:45 +01:00
|
|
|
|
|
|
|
// GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project
|
|
|
|
func (gs *GState) GetWeeklyReportsUserHandler(c *fiber.Ctx) error {
|
2024-03-20 16:05:50 +01:00
|
|
|
// Extract the necessary parameters from the token
|
|
|
|
user := c.Locals("user").(*jwt.Token)
|
|
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
|
|
username := claims["name"].(string)
|
|
|
|
|
|
|
|
// Extract necessary (path) parameters from the request
|
2024-03-19 19:04:45 +01:00
|
|
|
projectName := c.Params("projectName")
|
|
|
|
|
2024-03-20 16:05:50 +01:00
|
|
|
// TODO: Here we need to check whether the user is a member of the project
|
|
|
|
// If not, we should return an error. On the other hand, if the user not a member,
|
|
|
|
// the returned list of reports will (should) allways be empty.
|
|
|
|
|
2024-03-19 19:04:45 +01:00
|
|
|
// Retrieve weekly reports for the user in the project from the database
|
|
|
|
reports, err := gs.Db.GetWeeklyReportsUser(username, projectName)
|
|
|
|
if err != nil {
|
2024-03-20 16:05:50 +01:00
|
|
|
log.Error("Error getting weekly reports for user:", username, "in project:", projectName, ":", err)
|
2024-03-19 19:04:45 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Returning weekly reports for user:", username, "in project:", projectName)
|
|
|
|
|
|
|
|
// Return the list of reports as JSON
|
|
|
|
return c.JSON(reports)
|
|
|
|
}
|
2024-03-21 02:48:26 +01:00
|
|
|
|
|
|
|
func (gs *GState) UpdateWeeklyReport(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)
|
|
|
|
|
|
|
|
// Parse the request body into an UpdateWeeklyReport struct
|
|
|
|
var updateReport types.UpdateWeeklyReport
|
|
|
|
if err := c.BodyParser(&updateReport); err != nil {
|
|
|
|
log.Info("Error parsing weekly report")
|
|
|
|
return c.Status(400).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all the fields of the report are valid
|
|
|
|
if updateReport.Week < 1 || updateReport.Week > 52 {
|
|
|
|
log.Info("Invalid week number")
|
|
|
|
return c.Status(400).SendString("Invalid week number")
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateReport.DevelopmentTime < 0 || updateReport.MeetingTime < 0 || updateReport.AdminTime < 0 || updateReport.OwnWorkTime < 0 || updateReport.StudyTime < 0 || updateReport.TestingTime < 0 {
|
|
|
|
log.Info("Invalid time report")
|
|
|
|
return c.Status(400).SendString("Invalid time report")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the weekly report in the database
|
|
|
|
if err := gs.Db.UpdateWeeklyReport(updateReport.ProjectName, username, updateReport.Week, updateReport.DevelopmentTime, updateReport.MeetingTime, updateReport.AdminTime, updateReport.OwnWorkTime, updateReport.StudyTime, updateReport.TestingTime); err != nil {
|
|
|
|
log.Info("Error updating weekly report in db:", err)
|
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Weekly report updated")
|
|
|
|
return c.Status(200).SendString("Weekly report updated")
|
|
|
|
}
|