Rename, fix and testing for getAllWeeklyReports path
This commit is contained in:
		
							parent
							
								
									13eb6597a7
								
							
						
					
					
						commit
						0a951ecd2b
					
				
					 4 changed files with 36 additions and 17 deletions
				
			
		|  | @ -8,29 +8,49 @@ import ( | |||
| 	"github.com/golang-jwt/jwt/v5" | ||||
| ) | ||||
| 
 | ||||
| // GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project | ||||
| func GetWeeklyReportsUserHandler(c *fiber.Ctx) error { | ||||
| // GetAllWeeklyReports retrieves all weekly reports for a user in a specific project | ||||
| func GetAllWeeklyReports(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) | ||||
| 
 | ||||
| 	// Extract necessary (path) parameters from the request | ||||
| 	// Extract project name and week from query parameters | ||||
| 	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 not, we should return an error. On the other hand, if the user not a member, | ||||
| 	// the returned list of reports will (should) allways be empty. | ||||
| 	// If the target user is not empty, use it as the username | ||||
| 	if target_user == "" { | ||||
| 		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 | ||||
| 	reports, err := db.GetDb(c).GetWeeklyReportsUser(username, projectName) | ||||
| 	reports, err := db.GetDb(c).GetAllWeeklyReports(username, projectName) | ||||
| 	if err != nil { | ||||
| 		log.Error("Error getting weekly reports for user:", username, "in project:", projectName, ":", err) | ||||
| 		return c.Status(500).SendString(err.Error()) | ||||
| 	} | ||||
| 
 | ||||
| 	log.Info("Returning weekly reports for user:", username, "in project:", projectName) | ||||
| 
 | ||||
| 	// Return the list of reports as JSON | ||||
| 	log.Info("Returning weekly report") | ||||
| 	// Return the retrieved weekly report | ||||
| 	return c.JSON(reports) | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus