diff --git a/backend/internal/handlers/reports/SignReport.go b/backend/internal/handlers/reports/SignReport.go index 5769caf..a486ecc 100644 --- a/backend/internal/handlers/reports/SignReport.go +++ b/backend/internal/handlers/reports/SignReport.go @@ -1,6 +1,7 @@ package reports import ( + "strconv" db "ttime/internal/database" "github.com/gofiber/fiber/v2" @@ -8,41 +9,33 @@ import ( "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 + // Extract report ID from the path + reportId, err := strconv.Atoi(c.Params("reportId")) + if err != nil { + log.Info("Invalid report ID") + return c.Status(400).SendString("Invalid report ID") } - 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") + log.Info("Failed to get project manager ID for user: ", projectManagerUsername) 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) + err = db.GetDb(c).SignWeeklyReport(reportId, projectManagerID) if err != nil { log.Info("Error signing weekly report:", err) return c.Status(500).SendString(err.Error()) } + log.Info("Project manager ID: ", projectManagerID, " signed report ID: ", reportId) return c.Status(200).SendString("Weekly report signed successfully") } diff --git a/backend/main.go b/backend/main.go index ebe5660..cae088f 100644 --- a/backend/main.go +++ b/backend/main.go @@ -125,7 +125,7 @@ func main() { api.Get("/getWeeklyReport", reports.GetWeeklyReport) api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport) api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports) - api.Post("/signReport", reports.SignReport) + api.Put("/signReport/:reportId", reports.SignReport) api.Put("/addUserToProject", projects.AddUserToProjectHandler) api.Get("/getWeeklyReportsUser/:projectName", reports.GetWeeklyReportsUserHandler) api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index 0160e15..886c957 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -153,6 +153,18 @@ interface API { projectName: string, token: string, ): Promise>; + + /** + * Signs a report. Keep in mind that the user which the token belongs to must be + * the project manager of the project the report belongs to. + * + * @param {number} reportId The id of the report to sign + * @param {string} token The authentication token + */ + signReport( + reportId: number, + token: string, + ): Promise>; } /** An instance of the API */ @@ -581,4 +593,27 @@ export const api: API = { }); } }, + + async signReport( + reportId: number, + token: string, + ): Promise> { + try { + const response = await fetch(`/api/signReport/${reportId}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + }); + + if (!response.ok) { + return { success: false, message: "Failed to sign report" }; + } else { + return { success: true, message: "Report signed" }; + } + } catch (e) { + return { success: false, message: "Failed to sign report" }; + } + } }; diff --git a/testing.py b/testing.py index 5fb8ffe..d4594d1 100644 --- a/testing.py +++ b/testing.py @@ -301,9 +301,8 @@ def test_sign_report(): report_id = response.json()["reportId"] # Sign the report as the project manager - response = requests.post( - signReportPath, - json={"reportId": report_id}, + response = requests.put( + signReportPath + "/" + str(report_id), headers={"Authorization": "Bearer " + project_manager_token}, ) assert response.status_code == 200, "Sign report failed"