SignReport handler changes along with tests and TS interface
This commit is contained in:
parent
b927fb80fb
commit
4538a3b193
4 changed files with 47 additions and 20 deletions
|
@ -1,6 +1,7 @@
|
||||||
package reports
|
package reports
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
db "ttime/internal/database"
|
db "ttime/internal/database"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
@ -8,41 +9,33 @@ import (
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"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 {
|
func SignReport(c *fiber.Ctx) error {
|
||||||
// Extract the necessary parameters from the token
|
// Extract the necessary parameters from the token
|
||||||
user := c.Locals("user").(*jwt.Token)
|
user := c.Locals("user").(*jwt.Token)
|
||||||
claims := user.Claims.(jwt.MapClaims)
|
claims := user.Claims.(jwt.MapClaims)
|
||||||
projectManagerUsername := claims["name"].(string)
|
projectManagerUsername := claims["name"].(string)
|
||||||
|
|
||||||
log.Info("Signing report for: ", projectManagerUsername)
|
// Extract report ID from the path
|
||||||
|
reportId, err := strconv.Atoi(c.Params("reportId"))
|
||||||
// Extract report ID from the request query parameters
|
if err != nil {
|
||||||
// reportID := c.Query("reportId")
|
log.Info("Invalid report ID")
|
||||||
rid := new(ReportId)
|
return c.Status(400).SendString("Invalid report ID")
|
||||||
if err := c.BodyParser(rid); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
log.Info("Signing report for: ", rid.ReportId)
|
|
||||||
|
|
||||||
// Get the project manager's ID
|
// Get the project manager's ID
|
||||||
projectManagerID, err := db.GetDb(c).GetUserId(projectManagerUsername)
|
projectManagerID, err := db.GetDb(c).GetUserId(projectManagerUsername)
|
||||||
if err != nil {
|
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")
|
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
|
// 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 {
|
if err != nil {
|
||||||
log.Info("Error signing weekly report:", err)
|
log.Info("Error signing weekly report:", err)
|
||||||
return c.Status(500).SendString(err.Error())
|
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")
|
return c.Status(200).SendString("Weekly report signed successfully")
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,7 +125,7 @@ func main() {
|
||||||
api.Get("/getWeeklyReport", reports.GetWeeklyReport)
|
api.Get("/getWeeklyReport", reports.GetWeeklyReport)
|
||||||
api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport)
|
api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport)
|
||||||
api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports)
|
api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports)
|
||||||
api.Post("/signReport", reports.SignReport)
|
api.Put("/signReport/:reportId", reports.SignReport)
|
||||||
api.Put("/addUserToProject", projects.AddUserToProjectHandler)
|
api.Put("/addUserToProject", projects.AddUserToProjectHandler)
|
||||||
api.Get("/getWeeklyReportsUser/:projectName", reports.GetWeeklyReportsUserHandler)
|
api.Get("/getWeeklyReportsUser/:projectName", reports.GetWeeklyReportsUserHandler)
|
||||||
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)
|
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)
|
||||||
|
|
|
@ -153,6 +153,18 @@ interface API {
|
||||||
projectName: string,
|
projectName: string,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<APIResponse<string>>;
|
): Promise<APIResponse<string>>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<APIResponse<string>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** An instance of the API */
|
/** An instance of the API */
|
||||||
|
@ -581,4 +593,27 @@ export const api: API = {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async signReport(
|
||||||
|
reportId: number,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<string>> {
|
||||||
|
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" };
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -301,9 +301,8 @@ def test_sign_report():
|
||||||
report_id = response.json()["reportId"]
|
report_id = response.json()["reportId"]
|
||||||
|
|
||||||
# Sign the report as the project manager
|
# Sign the report as the project manager
|
||||||
response = requests.post(
|
response = requests.put(
|
||||||
signReportPath,
|
signReportPath + "/" + str(report_id),
|
||||||
json={"reportId": report_id},
|
|
||||||
headers={"Authorization": "Bearer " + project_manager_token},
|
headers={"Authorization": "Bearer " + project_manager_token},
|
||||||
)
|
)
|
||||||
assert response.status_code == 200, "Sign report failed"
|
assert response.status_code == 200, "Sign report failed"
|
||||||
|
|
Loading…
Reference in a new issue