Rename, fix and testing for getAllWeeklyReports path

This commit is contained in:
Imbus 2024-04-03 17:31:39 +02:00
parent a1d2520d88
commit a39cfedad3
4 changed files with 36 additions and 17 deletions

View file

@ -35,7 +35,7 @@ type Database interface {
GetProject(projectId int) (types.Project, error) GetProject(projectId int) (types.Project, error)
GetUserRole(username string, projectname string) (string, error) GetUserRole(username string, projectname string) (string, error)
GetWeeklyReport(username string, projectName string, week int) (types.WeeklyReport, error) GetWeeklyReport(username string, projectName string, week int) (types.WeeklyReport, error)
GetWeeklyReportsUser(username string, projectname string) ([]types.WeeklyReportList, error) GetAllWeeklyReports(username string, projectname string) ([]types.WeeklyReportList, error)
GetUnsignedWeeklyReports(projectName string) ([]types.WeeklyReport, error) GetUnsignedWeeklyReports(projectName string) ([]types.WeeklyReport, error)
SignWeeklyReport(reportId int, projectManagerId int) error SignWeeklyReport(reportId int, projectManagerId int) error
IsSiteAdmin(username string) (bool, error) IsSiteAdmin(username string) (bool, error)
@ -463,8 +463,8 @@ func (d *Db) Migrate() error {
return nil return nil
} }
// GetWeeklyReportsUser retrieves weekly reports for a specific user and project. // GetAllWeeklyReports retrieves weekly reports for a specific user and project.
func (d *Db) GetWeeklyReportsUser(username string, projectName string) ([]types.WeeklyReportList, error) { func (d *Db) GetAllWeeklyReports(username string, projectName string) ([]types.WeeklyReportList, error) {
query := ` query := `
SELECT SELECT
wr.week, wr.week,

View file

@ -705,7 +705,7 @@ func TestGetWeeklyReportsUser(t *testing.T) {
t.Error("AddWeeklyReport failed:", err) t.Error("AddWeeklyReport failed:", err)
} }
reports, err := db.GetWeeklyReportsUser("testuser", "testproject") reports, err := db.GetAllWeeklyReports("testuser", "testproject")
if err != nil { if err != nil {
t.Error("GetWeeklyReportsUser failed:", err) t.Error("GetWeeklyReportsUser failed:", err)
} }
@ -962,6 +962,5 @@ func TestRemoveProject(t *testing.T) {
if len(projects) != 0 { if len(projects) != 0 {
t.Error("RemoveProject failed: expected 0, got", len(projects)) t.Error("RemoveProject failed: expected 0, got", len(projects))
} }
} }

View file

@ -8,29 +8,49 @@ import (
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
) )
// GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project // GetAllWeeklyReports retrieves all weekly reports for a user in a specific project
func GetWeeklyReportsUserHandler(c *fiber.Ctx) error { func GetAllWeeklyReports(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)
username := claims["name"].(string) username := claims["name"].(string)
// Extract necessary (path) parameters from the request // Extract project name and week from query parameters
projectName := c.Params("projectName") projectName := c.Params("projectName")
target_user := c.Query("targetUser") // The user whose reports are being requested
// TODO: Here we need to check whether the user is a member of the project // If the target user is not empty, use it as the username
// If not, we should return an error. On the other hand, if the user not a member, if target_user == "" {
// the returned list of reports will (should) allways be empty. target_user = username
}
log.Info(username, " trying to get all weekly reports for: ", target_user)
if projectName == "" {
log.Info("Missing project name")
return c.Status(400).SendString("Missing project name")
}
// If the token user is not an admin, check if the target user is the same as the token user
pm, err := db.GetDb(c).IsProjectManager(username, projectName)
if err != nil {
log.Info("Error checking if user is project manager:", err)
return c.Status(500).SendString(err.Error())
}
if pm == false && target_user != username {
log.Info("Unauthorized access")
return c.Status(403).SendString("Unauthorized access")
}
// Retrieve weekly reports for the user in the project from the database // Retrieve weekly reports for the user in the project from the database
reports, err := db.GetDb(c).GetWeeklyReportsUser(username, projectName) reports, err := db.GetDb(c).GetAllWeeklyReports(username, projectName)
if err != nil { if err != nil {
log.Error("Error getting weekly reports for user:", username, "in project:", projectName, ":", err) log.Error("Error getting weekly reports for user:", username, "in project:", projectName, ":", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
log.Info("Returning weekly reports for user:", username, "in project:", projectName) log.Info("Returning weekly report")
// Return the retrieved weekly report
// Return the list of reports as JSON
return c.JSON(reports) return c.JSON(reports)
} }

View file

@ -128,7 +128,7 @@ func main() {
// reportGroup := api.Group("/report") // Not currently in use // reportGroup := api.Group("/report") // Not currently in use
api.Get("/getWeeklyReport", reports.GetWeeklyReport) api.Get("/getWeeklyReport", reports.GetWeeklyReport)
api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports) api.Get("/getUnsignedReports/:projectName", reports.GetUnsignedReports)
api.Get("/getWeeklyReportsUser/:projectName", reports.GetWeeklyReportsUserHandler) api.Get("/getAllWeeklyReports/:projectName", reports.GetAllWeeklyReports)
api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport) api.Post("/submitWeeklyReport", reports.SubmitWeeklyReport)
api.Put("/signReport/:reportId", reports.SignReport) api.Put("/signReport/:reportId", reports.SignReport)
api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport) api.Put("/updateWeeklyReport", reports.UpdateWeeklyReport)