Added isProjectManager function to db.go and corresponding test to db_test.go
This commit is contained in:
parent
2b41085865
commit
5778d6e892
2 changed files with 76 additions and 2 deletions
|
@ -35,6 +35,7 @@ type Database interface {
|
|||
GetWeeklyReportsUser(username string, projectname string) ([]types.WeeklyReportList, error)
|
||||
SignWeeklyReport(reportId int, projectManagerId int) error
|
||||
IsSiteAdmin(username string) (bool, error)
|
||||
IsProjectManager(username string, projectname string) (bool, error)
|
||||
}
|
||||
|
||||
// This struct is a wrapper type that holds the database connection
|
||||
|
@ -433,6 +434,27 @@ func (d *Db) GetWeeklyReportsUser(username string, projectName string) ([]types.
|
|||
return reports, nil
|
||||
}
|
||||
|
||||
// IsProjectManager checks if a given username is a project manager for the specified project
|
||||
func (d *Db) IsProjectManager(username string, projectname string) (bool, error) {
|
||||
// Define the SQL query to check if the user is a project manager for the project
|
||||
query := `
|
||||
SELECT COUNT(*) FROM user_roles
|
||||
JOIN users ON user_roles.user_id = users.id
|
||||
JOIN projects ON user_roles.project_id = projects.id
|
||||
WHERE users.username = ? AND projects.name = ? AND user_roles.p_role = 'project_manager'
|
||||
`
|
||||
|
||||
// Execute the query
|
||||
var count int
|
||||
err := d.Get(&count, query, username, projectname)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// If count is greater than 0, the user is a project manager for the project
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// MigrateSampleData applies sample data to the database.
|
||||
func (d *Db) MigrateSampleData() error {
|
||||
// Insert sample data
|
||||
|
|
|
@ -602,6 +602,58 @@ func TestGetWeeklyReportsUser(t *testing.T) {
|
|||
if len(reports) != 2 {
|
||||
t.Errorf("Expected 1 report, got %d", len(reports))
|
||||
}
|
||||
|
||||
// You can add further checks here if needed
|
||||
}
|
||||
|
||||
func TestIsProjectManager(t *testing.T) {
|
||||
db, err := setupState()
|
||||
if err != nil {
|
||||
t.Error("setupState failed:", err)
|
||||
}
|
||||
|
||||
// Add a project manager
|
||||
err = db.AddUser("projectManager", "password")
|
||||
if err != nil {
|
||||
t.Error("AddUser failed:", err)
|
||||
}
|
||||
|
||||
// Add a regular user
|
||||
err = db.AddUser("testuser", "password")
|
||||
if err != nil {
|
||||
t.Error("AddUser failed:", err)
|
||||
}
|
||||
|
||||
// Add project
|
||||
err = db.AddProject("testproject", "description", "projectManager")
|
||||
if err != nil {
|
||||
t.Error("AddProject failed:", err)
|
||||
}
|
||||
|
||||
// Add both regular users as members to the project
|
||||
err = db.AddUserToProject("testuser", "testproject", "member")
|
||||
if err != nil {
|
||||
t.Error("AddUserToProject failed:", err)
|
||||
}
|
||||
|
||||
err = db.AddUserToProject("projectManager", "testproject", "project_manager")
|
||||
if err != nil {
|
||||
t.Error("AddUserToProject failed:", err)
|
||||
}
|
||||
|
||||
// Check if the regular user is not a project manager
|
||||
isManager, err := db.IsProjectManager("testuser", "testproject")
|
||||
if err != nil {
|
||||
t.Error("IsProjectManager failed:", err)
|
||||
}
|
||||
if isManager {
|
||||
t.Error("Expected testuser not to be a project manager, but it is.")
|
||||
}
|
||||
|
||||
// Check if the project manager is indeed a project manager
|
||||
isManager, err = db.IsProjectManager("projectManager", "testproject")
|
||||
if err != nil {
|
||||
t.Error("IsProjectManager failed:", err)
|
||||
}
|
||||
if !isManager {
|
||||
t.Error("Expected projectManager to be a project manager, but it's not.")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue