42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
|
package reports
|
||
|
|
||
|
import (
|
||
|
db "ttime/internal/database"
|
||
|
"ttime/internal/types"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
"github.com/gofiber/fiber/v2/log"
|
||
|
"github.com/golang-jwt/jwt/v5"
|
||
|
)
|
||
|
|
||
|
func 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 {
|
||
|
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 {
|
||
|
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 {
|
||
|
log.Info("Invalid time report")
|
||
|
return c.Status(400).SendString("Invalid time report")
|
||
|
}
|
||
|
|
||
|
if err := db.GetDb(c).AddWeeklyReport(report.ProjectName, username, report.Week, report.DevelopmentTime, report.MeetingTime, report.AdminTime, report.OwnWorkTime, report.StudyTime, report.TestingTime); err != nil {
|
||
|
log.Info("Error adding weekly report to db:", err)
|
||
|
return c.Status(500).SendString(err.Error())
|
||
|
}
|
||
|
|
||
|
log.Info("Weekly report added")
|
||
|
return c.Status(200).SendString("Time report added")
|
||
|
}
|