Merge branch 'BumBranch' into gruppDM
This commit is contained in:
commit
08a0ebb533
9 changed files with 312 additions and 0 deletions
|
@ -32,8 +32,10 @@ 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)
|
||||||
SignWeeklyReport(reportId int, projectManagerId int) error
|
SignWeeklyReport(reportId int, projectManagerId int) error
|
||||||
IsSiteAdmin(username string) (bool, error)
|
IsSiteAdmin(username string) (bool, error)
|
||||||
|
IsProjectManager(username string, projectname string) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This struct is a wrapper type that holds the database connection
|
// This struct is a wrapper type that holds the database connection
|
||||||
|
@ -402,6 +404,57 @@ func (d *Db) Migrate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWeeklyReportsUser retrieves weekly reports for a specific user and project.
|
||||||
|
func (d *Db) GetWeeklyReportsUser(username string, projectName string) ([]types.WeeklyReportList, error) {
|
||||||
|
query := `
|
||||||
|
SELECT
|
||||||
|
wr.week,
|
||||||
|
wr.development_time,
|
||||||
|
wr.meeting_time,
|
||||||
|
wr.admin_time,
|
||||||
|
wr.own_work_time,
|
||||||
|
wr.study_time,
|
||||||
|
wr.testing_time,
|
||||||
|
wr.signed_by
|
||||||
|
FROM
|
||||||
|
weekly_reports wr
|
||||||
|
INNER JOIN
|
||||||
|
users u ON wr.user_id = u.id
|
||||||
|
INNER JOIN
|
||||||
|
projects p ON wr.project_id = p.id
|
||||||
|
WHERE
|
||||||
|
u.username = ? AND p.name = ?
|
||||||
|
`
|
||||||
|
|
||||||
|
var reports []types.WeeklyReportList
|
||||||
|
if err := d.Select(&reports, query, username, projectName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
// MigrateSampleData applies sample data to the database.
|
||||||
func (d *Db) MigrateSampleData() error {
|
func (d *Db) MigrateSampleData() error {
|
||||||
// Insert sample data
|
// Insert sample data
|
||||||
|
|
|
@ -566,3 +566,94 @@ func TestGetProject(t *testing.T) {
|
||||||
t.Errorf("Expected Name to be testproject, got %s", project.Name)
|
t.Errorf("Expected Name to be testproject, got %s", project.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetWeeklyReportsUser(t *testing.T) {
|
||||||
|
db, err := setupState()
|
||||||
|
if err != nil {
|
||||||
|
t.Error("setupState failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.AddUser("testuser", "password")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("AddUser failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.AddProject("testproject", "description", "testuser")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("AddProject failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("AddWeeklyReport failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.AddWeeklyReport("testproject", "testuser", 2, 1, 1, 1, 1, 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("AddWeeklyReport failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
reports, err := db.GetWeeklyReportsUser("testuser", "testproject")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("GetWeeklyReportsUser failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the retrieved reports match the expected values
|
||||||
|
if len(reports) != 2 {
|
||||||
|
t.Errorf("Expected 1 report, got %d", len(reports))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,8 @@ type GlobalState interface {
|
||||||
GetProject(c *fiber.Ctx) error
|
GetProject(c *fiber.Ctx) error
|
||||||
AddUserToProjectHandler(c *fiber.Ctx) error
|
AddUserToProjectHandler(c *fiber.Ctx) error
|
||||||
PromoteToAdmin(c *fiber.Ctx) error
|
PromoteToAdmin(c *fiber.Ctx) error
|
||||||
|
GetWeeklyReportsUserHandler(c *fiber.Ctx) error
|
||||||
|
IsProjectManagerHandler(c *fiber.Ctx) error
|
||||||
// GetProject(c *fiber.Ctx) error // To get a specific project
|
// GetProject(c *fiber.Ctx) error // To get a specific project
|
||||||
// UpdateProject(c *fiber.Ctx) error // To update a project
|
// UpdateProject(c *fiber.Ctx) error // To update a project
|
||||||
// DeleteProject(c *fiber.Ctx) error // To delete a project
|
// DeleteProject(c *fiber.Ctx) error // To delete a project
|
||||||
|
|
|
@ -154,3 +154,20 @@ func (gs *GState) AddUserToProjectHandler(c *fiber.Ctx) error {
|
||||||
log.Info("User added to project successfully:", requestData.Username)
|
log.Info("User added to project successfully:", requestData.Username)
|
||||||
return c.SendStatus(fiber.StatusOK)
|
return c.SendStatus(fiber.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsProjectManagerHandler is a handler that checks if a user is a project manager for a given project
|
||||||
|
func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error {
|
||||||
|
// Extract necessary parameters from the request query string
|
||||||
|
username := c.Query("username")
|
||||||
|
projectName := c.Query("projectName")
|
||||||
|
|
||||||
|
// Check if the user is a project manager for the specified project
|
||||||
|
isManager, err := gs.Db.IsProjectManager(username, projectName)
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Error checking project manager status:", err)
|
||||||
|
return c.Status(500).SendString(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the result as JSON
|
||||||
|
return c.JSON(map[string]bool{"isProjectManager": isManager})
|
||||||
|
}
|
||||||
|
|
|
@ -114,3 +114,22 @@ func (gs *GState) SignReport(c *fiber.Ctx) error {
|
||||||
|
|
||||||
return c.Status(200).SendString("Weekly report signed successfully")
|
return c.Status(200).SendString("Weekly report signed successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWeeklyReportsUserHandler retrieves all weekly reports for a user in a specific project
|
||||||
|
func (gs *GState) GetWeeklyReportsUserHandler(c *fiber.Ctx) error {
|
||||||
|
// Extract necessary parameters from the request
|
||||||
|
username := c.Params("username")
|
||||||
|
projectName := c.Params("projectName")
|
||||||
|
|
||||||
|
// Retrieve weekly reports for the user in the project from the database
|
||||||
|
reports, err := gs.Db.GetWeeklyReportsUser(username, projectName)
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Error getting weekly reports for user:", 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
|
||||||
|
return c.JSON(reports)
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,27 @@ type NewWeeklyReport struct {
|
||||||
TestingTime int `json:"testingTime"`
|
TestingTime int `json:"testingTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WeeklyReportList struct {
|
||||||
|
// The name of the project, as it appears in the database
|
||||||
|
ProjectName string `json:"projectName" db:"project_name"`
|
||||||
|
// The week number
|
||||||
|
Week int `json:"week" db:"week"`
|
||||||
|
// Total time spent on development
|
||||||
|
DevelopmentTime int `json:"developmentTime" db:"development_time"`
|
||||||
|
// Total time spent in meetings
|
||||||
|
MeetingTime int `json:"meetingTime" db:"meeting_time"`
|
||||||
|
// Total time spent on administrative tasks
|
||||||
|
AdminTime int `json:"adminTime" db:"admin_time"`
|
||||||
|
// Total time spent on personal projects
|
||||||
|
OwnWorkTime int `json:"ownWorkTime" db:"own_work_time"`
|
||||||
|
// Total time spent on studying
|
||||||
|
StudyTime int `json:"studyTime" db:"study_time"`
|
||||||
|
// Total time spent on testing
|
||||||
|
TestingTime int `json:"testingTime" db:"testing_time"`
|
||||||
|
// The project manager who signed it
|
||||||
|
SignedBy *int `json:"signedBy" db:"signed_by"`
|
||||||
|
}
|
||||||
|
|
||||||
type WeeklyReport struct {
|
type WeeklyReport struct {
|
||||||
// The ID of the report
|
// The ID of the report
|
||||||
ReportId int `json:"reportId" db:"report_id"`
|
ReportId int `json:"reportId" db:"report_id"`
|
||||||
|
|
|
@ -94,6 +94,9 @@ func main() {
|
||||||
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
||||||
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
||||||
server.Get("/api/users/all", gs.ListAllUsers)
|
server.Get("/api/users/all", gs.ListAllUsers)
|
||||||
|
server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler)
|
||||||
|
server.Get("api/checkIfProjectManager", gs.IsProjectManagerHandler)
|
||||||
|
|
||||||
// Announce the port we are listening on and start the server
|
// Announce the port we are listening on and start the server
|
||||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -20,10 +20,17 @@ interface API {
|
||||||
registerUser(user: NewUser): Promise<APIResponse<User>>;
|
registerUser(user: NewUser): Promise<APIResponse<User>>;
|
||||||
/** Remove a user */
|
/** Remove a user */
|
||||||
removeUser(username: string, token: string): Promise<APIResponse<User>>;
|
removeUser(username: string, token: string): Promise<APIResponse<User>>;
|
||||||
|
/** Check if user is project manager */
|
||||||
|
checkIfProjectManager(
|
||||||
|
username: string,
|
||||||
|
projectName: string,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<boolean>>;
|
||||||
/** Login */
|
/** Login */
|
||||||
login(NewUser: NewUser): Promise<APIResponse<string>>;
|
login(NewUser: NewUser): Promise<APIResponse<string>>;
|
||||||
/** Renew the token */
|
/** Renew the token */
|
||||||
renewToken(token: string): Promise<APIResponse<string>>;
|
renewToken(token: string): Promise<APIResponse<string>>;
|
||||||
|
/** Promote user to admin */
|
||||||
/** Create a project */
|
/** Create a project */
|
||||||
createProject(
|
createProject(
|
||||||
project: NewProject,
|
project: NewProject,
|
||||||
|
@ -41,6 +48,10 @@ interface API {
|
||||||
week: string,
|
week: string,
|
||||||
token: string,
|
token: string,
|
||||||
): Promise<APIResponse<NewWeeklyReport>>;
|
): Promise<APIResponse<NewWeeklyReport>>;
|
||||||
|
getWeeklyReportsForProject(
|
||||||
|
projectName: string,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<NewWeeklyReport[]>>;
|
||||||
/** Gets all the projects of a user*/
|
/** Gets all the projects of a user*/
|
||||||
getUserProjects(token: string): Promise<APIResponse<Project[]>>;
|
getUserProjects(token: string): Promise<APIResponse<Project[]>>;
|
||||||
/** Gets a project from id*/
|
/** Gets a project from id*/
|
||||||
|
@ -101,6 +112,35 @@ export const api: API = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async checkIfProjectManager(
|
||||||
|
username: string,
|
||||||
|
projectName: string,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<boolean>> {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/checkIfProjectManager", {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: "Bearer " + token,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ username, projectName }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed to check if project manager",
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const data = (await response.json()) as boolean;
|
||||||
|
return { success: true, data };
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return { success: false, message: "fuck" };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
async createProject(
|
async createProject(
|
||||||
project: NewProject,
|
project: NewProject,
|
||||||
token: string,
|
token: string,
|
||||||
|
@ -232,6 +272,34 @@ export const api: API = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async getWeeklyReportsForProject(projectName: string, token: string) {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/getWeeklyReportsForProject", {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: "Bearer " + token,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ projectName }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed to get weekly reports for project",
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const data = (await response.json()) as NewWeeklyReport[];
|
||||||
|
return { success: true, data };
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed to get weekly reports for project",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
async login(NewUser: NewUser): Promise<APIResponse<string>> {
|
async login(NewUser: NewUser): Promise<APIResponse<string>> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/login", {
|
const response = await fetch("/api/login", {
|
||||||
|
|
38
testing.py
38
testing.py
|
@ -37,6 +37,8 @@ signReportPath = base_url + "/api/signReport"
|
||||||
addUserToProjectPath = base_url + "/api/addUserToProject"
|
addUserToProjectPath = base_url + "/api/addUserToProject"
|
||||||
promoteToAdminPath = base_url + "/api/promoteToAdmin"
|
promoteToAdminPath = base_url + "/api/promoteToAdmin"
|
||||||
getUserProjectsPath = base_url + "/api/getUserProjects"
|
getUserProjectsPath = base_url + "/api/getUserProjects"
|
||||||
|
getWeeklyReportsUserPath = base_url + "/api/getWeeklyReportsUser"
|
||||||
|
checkIfProjectManagerPath = base_url + "/api/checkIfProjectManager"
|
||||||
|
|
||||||
|
|
||||||
def test_get_user_projects():
|
def test_get_user_projects():
|
||||||
|
@ -275,6 +277,40 @@ def test_sign_report():
|
||||||
dprint(response.text)
|
dprint(response.text)
|
||||||
gprint("test_sign_report successful")
|
gprint("test_sign_report successful")
|
||||||
|
|
||||||
|
# Test function to get weekly reports for a user in a project
|
||||||
|
def test_get_weekly_reports_user():
|
||||||
|
# Log in as the user
|
||||||
|
token = login(username, "always_same").json()["token"]
|
||||||
|
|
||||||
|
# Get weekly reports for the user in the project
|
||||||
|
response = requests.get(
|
||||||
|
getWeeklyReportsUserPath,
|
||||||
|
headers={"Authorization": "Bearer " + token},
|
||||||
|
params={"username": username, "projectName": projectName},
|
||||||
|
)
|
||||||
|
|
||||||
|
dprint(response.text)
|
||||||
|
assert response.status_code == 200, "Get weekly reports for user failed"
|
||||||
|
gprint("test_get_weekly_reports_user successful")
|
||||||
|
|
||||||
|
# Test function to check if a user is a project manager
|
||||||
|
def test_check_if_project_manager():
|
||||||
|
# Log in as the user
|
||||||
|
token = login(username, "always_same").json()["token"]
|
||||||
|
|
||||||
|
# Check if the user is a project manager for the project
|
||||||
|
response = requests.get(
|
||||||
|
checkIfProjectManagerPath,
|
||||||
|
headers={"Authorization": "Bearer " + token},
|
||||||
|
params={"username": username, "projectName": projectName},
|
||||||
|
)
|
||||||
|
|
||||||
|
dprint(response.text)
|
||||||
|
assert response.status_code == 200, "Check if project manager failed"
|
||||||
|
gprint("test_check_if_project_manager successful")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_get_user_projects()
|
test_get_user_projects()
|
||||||
|
@ -286,3 +322,5 @@ if __name__ == "__main__":
|
||||||
test_get_project()
|
test_get_project()
|
||||||
test_sign_report()
|
test_sign_report()
|
||||||
test_add_user_to_project()
|
test_add_user_to_project()
|
||||||
|
test_get_weekly_reports_user()
|
||||||
|
test_check_if_project_manager()
|
||||||
|
|
Loading…
Reference in a new issue