TTime/backend/internal/handlers/projects/AddUserToProject.go

43 lines
1.2 KiB
Go
Raw Normal View History

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 {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
2024-04-03 15:53:36 +02:00
pm_name := claims["name"].(string)
project := c.Params("projectName")
username := c.Query("userName")
2024-04-03 15:53:36 +02:00
// Check if the user is a project manager
isPM, err := db.GetDb(c).IsProjectManager(pm_name, project)
if err != nil {
2024-04-03 15:53:36 +02:00
log.Info("Error checking if user is project manager:", err)
return c.Status(500).SendString(err.Error())
}
2024-04-03 15:53:36 +02:00
if !isPM {
log.Info("User: ", pm_name, " is not a project manager in project: ", project)
return c.Status(403).SendString("User is not a project manager")
}
// Add the user to the project with the specified role
2024-04-03 15:53:36 +02:00
err = db.GetDb(c).AddUserToProject(username, project, "member")
if err != nil {
log.Info("Error adding user to project:", err)
return c.Status(500).SendString(err.Error())
}
// Return success message
2024-04-03 15:53:36 +02:00
log.Info("User : ", username, " added to project: ", project)
return c.SendStatus(fiber.StatusOK)
}