package projects import ( db "ttime/internal/database" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/golang-jwt/jwt/v5" ) // AddUserToProjectHandler is a handler that adds a user to a project with a specified role func AddUserToProjectHandler(c *fiber.Ctx) error { // Extract necessary parameters from the request var requestData struct { Username string `json:"username"` ProjectName string `json:"projectName"` Role string `json:"role"` } if err := c.BodyParser(&requestData); err != nil { log.Info("Error parsing request body:", err) return c.Status(400).SendString("Bad request") } // Check if the user adding another user to the project is a site admin user := c.Locals("user").(*jwt.Token) claims := user.Claims.(jwt.MapClaims) adminUsername := claims["name"].(string) log.Info("Admin username from claims:", adminUsername) isAdmin, err := db.GetDb(c).IsSiteAdmin(adminUsername) if err != nil { log.Info("Error checking admin status:", err) return c.Status(500).SendString(err.Error()) } if !isAdmin { log.Info("User is not a site admin:", adminUsername) return c.Status(403).SendString("User is not a site admin") } // Add the user to the project with the specified role err = db.GetDb(c).AddUserToProject(requestData.Username, requestData.ProjectName, requestData.Role) if err != nil { log.Info("Error adding user to project:", err) return c.Status(500).SendString(err.Error()) } // Return success message log.Info("User added to project successfully:", requestData.Username) return c.SendStatus(fiber.StatusOK) }