From f6e4603603a4973fb951f48bfad652783437231c Mon Sep 17 00:00:00 2001 From: dDogge Date: Sun, 17 Mar 2024 23:31:52 +0100 Subject: [PATCH] Added handler for SignWeeklyReport named SignReport in handlers_report_related --- backend/internal/handlers/global_state.go | 1 + .../handlers/handlers_report_related.go | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 57a1969..c8beb1c 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -16,6 +16,7 @@ type GlobalState interface { GetUserProjects(c *fiber.Ctx) error // To get all projects SubmitWeeklyReport(c *fiber.Ctx) error GetWeeklyReport(c *fiber.Ctx) error + SignReport(c *fiber.Ctx) error // GetProject(c *fiber.Ctx) error // To get a specific project // UpdateProject(c *fiber.Ctx) error // To update a project // DeleteProject(c *fiber.Ctx) error // To delete a project diff --git a/backend/internal/handlers/handlers_report_related.go b/backend/internal/handlers/handlers_report_related.go index 8754afd..8168dbd 100644 --- a/backend/internal/handlers/handlers_report_related.go +++ b/backend/internal/handlers/handlers_report_related.go @@ -60,3 +60,31 @@ func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error { // Return the retrieved weekly report return c.JSON(report) } + +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) + managerUsername := claims["name"].(string) + + // Extract the report ID and project manager ID from request parameters + reportID, err := strconv.Atoi(c.Params("reportId")) + if err != nil { + return c.Status(400).SendString("Invalid report ID") + } + + // Call the database function to get the project manager ID + managerID, err := gs.Db.GetUserId(managerUsername) + if err != nil { + return c.Status(500).SendString("Failed to get project manager ID") + } + + // Call the database function to sign the weekly report + err = gs.Db.SignWeeklyReport(reportID, managerID) + if err != nil { + return c.Status(500).SendString("Failed to sign the weekly report: " + err.Error()) + } + + // Return success response + return c.Status(200).SendString("Weekly report signed successfully") +}